Merge branch 'feature/standards-compliant' into 'master'

Feature/standards compliant

See merge request !1
This commit is contained in:
Fredric Silberberg 2014-09-18 12:09:30 -04:00
commit 5122c83609
7 changed files with 211 additions and 174 deletions

View File

@ -11,7 +11,7 @@ COVER_DIR=cover
# Package lists # Package lists
TOPLEVEL_PKG := jsh TOPLEVEL_PKG := jsh
CMD_LIST := jsh/ps CMD_LIST := jsh/ps jsh/free jsh/ls
# List building # List building
ALL_LIST = $(TOPLEVEL_PKG) $(CMD_LIST) ALL_LIST = $(TOPLEVEL_PKG) $(CMD_LIST)

View File

@ -4,15 +4,17 @@ package jsh
import ( import (
"encoding/json" "encoding/json"
"fmt"
) )
type JshOutput struct { type JshFrame struct {
StdOut interface{} StdOut interface{}
StdErr interface{} StdErr interface{}
} }
// Size prefixes as integers, using binary representation // Size prefixes as integers, using binary representation
type Unit int type Unit int
const ( const (
B Unit = 2 ^ 0 B Unit = 2 ^ 0
KB Unit = 2 ^ 10 KB Unit = 2 ^ 10
@ -21,8 +23,8 @@ const (
TB Unit = 2 ^ 40 TB Unit = 2 ^ 40
) )
// Converts a Jshoutput into a JSON string // Converts a JshFrame into a JSON string
func (j JshOutput) ToJson() *string { func (j JshFrame) ToJson() *string {
jsonOut, err := json.Marshal(j) jsonOut, err := json.Marshal(j)
if err != nil { if err != nil {
panic(err) panic(err)
@ -30,3 +32,25 @@ func (j JshOutput) ToJson() *string {
jsonString := string(jsonOut) jsonString := string(jsonOut)
return &jsonString return &jsonString
} }
// goroutine for outputing frames. Pass it a channel of pointers to JshFrames,
// and it will send "true" to the done channel once you close the queue channel.
func OutputFrames(queue chan *JshFrame, done chan bool) {
fmt.Printf("[")
isFirst := true
for {
frame, more := <-queue
if more {
if !isFirst {
fmt.Printf(",")
} else {
isFirst = false
}
fmt.Printf(*frame.ToJson())
} else {
fmt.Printf("]\n")
done <- true
return
}
}
}

View File

@ -6,8 +6,8 @@ import (
"flag" "flag"
"fmt" "fmt"
"jsh" "jsh"
"regexp"
"os" "os"
"regexp"
"strconv" "strconv"
) )
@ -23,8 +23,10 @@ func convertUnit(stringUnit string) (jsh.Unit, error) {
return jsh.GB, nil return jsh.GB, nil
case "tB": case "tB":
return jsh.TB, nil return jsh.TB, nil
case "":
return jsh.B, nil
default: default:
return 0, errors.New(fmt.Sprintln("Unknown unit %s", stringUnit)) return 0, errors.New(fmt.Sprintf("Unknown unit: %s\n", stringUnit))
} }
} }
@ -53,10 +55,10 @@ func parseLine(line string) (string, jsh.MemStat, error) {
return key, jsh.MemStat{val, unit}, nil return key, jsh.MemStat{val, unit}, nil
} }
func parseMemInfo() jsh.JshOutput { func parseMemInfo() jsh.JshFrame {
file, err := os.Open("/proc/meminfo") file, err := os.Open("/proc/meminfo")
if err != nil { if err != nil {
return jsh.JshOutput{[]string{}, []error{err}} return jsh.JshFrame{[]string{}, []error{err}}
} }
defer file.Close() defer file.Close()
@ -72,13 +74,20 @@ func parseMemInfo() jsh.JshOutput {
memInfo[key] = val memInfo[key] = val
} }
finalOut := jsh.JshOutput{memInfo, errors} // Wrap with array
stdOut := []map[string]jsh.MemStat{memInfo}
finalOut := jsh.JshFrame{stdOut, errors}
return finalOut return finalOut
} }
func runJsonMode() { func runJsonMode() {
output := parseMemInfo() output := parseMemInfo()
fmt.Println("%s", *output.ToJson()) queue := make(chan *jsh.JshFrame)
done := make(chan bool)
go jsh.OutputFrames(queue, done)
queue <- &output
close(queue)
<-done
} }
func main() { func main() {

View File

@ -1,32 +1,31 @@
package main package main
import ( import (
"io/ioutil"
"fmt"
"flag" "flag"
"fmt"
"io/ioutil"
"log" "log"
"syscall"
"strconv" "strconv"
"syscall"
) )
func get_fileinfo(f string, size bool, mode bool, inode bool) string { func get_fileinfo(f string, size bool, mode bool, inode bool) string {
var stat syscall.Stat_t var stat syscall.Stat_t
var ret string var ret string
ret = "{\"name\":\"" + f + "\"" ret = "{\"files\": [{\"name\":\"" + f + "\""
if err := syscall.Stat(f, &stat); err != nil { if err := syscall.Stat(f, &stat); err != nil {
log.Fatal(err) log.Fatal(err)
} }
if(size){ if size {
ret = ret + ", \"size\":" + strconv.FormatInt(stat.Size, 10) ret = ret + ", \"size\":" + strconv.FormatInt(stat.Size, 10)
} }
if(mode){ if mode {
ret = ret + ", \"mode\":" + strconv.Itoa(int(stat.Mode)) ret = ret + ", \"mode\":" + strconv.Itoa(int(stat.Mode))
} }
if(inode){ if inode {
ret = ret + ", \"inode\":" + strconv.FormatUint(stat.Ino, 10) ret = ret + ", \"inode\":" + strconv.FormatUint(stat.Ino, 10)
} }
ret = ret + "}" ret = ret + "}]}"
return ret return ret
} }
@ -49,12 +48,12 @@ func main() {
root := "." //flag.Arg(0) root := "." //flag.Arg(0)
dir, _ := ioutil.ReadDir(root) dir, _ := ioutil.ReadDir(root)
fmt.Printf("[\n") fmt.Printf("[{\"StdOut\": [\n")
if(!a_flag){ if !a_flag {
for _, entry := range dir { for _, entry := range dir {
if(entry.Name()[0]!='.'){ if entry.Name()[0] != '.' {
if(!first){ if !first {
fmt.Printf(",") fmt.Printf(",")
} else { } else {
first = false first = false
@ -64,7 +63,7 @@ func main() {
} }
} else { } else {
for _, entry := range dir { for _, entry := range dir {
if(!first){ if !first {
fmt.Printf(",") fmt.Printf(",")
} else { } else {
first = false first = false
@ -72,5 +71,5 @@ func main() {
fmt.Printf("%s\n", get_fileinfo(entry.Name(), size_flag, mode_flag, inode_flag)) fmt.Printf("%s\n", get_fileinfo(entry.Name(), size_flag, mode_flag, inode_flag))
} }
} }
fmt.Printf("]\n") fmt.Printf("], \"StdErr\": []}]\n")
} }

View File

@ -26,10 +26,15 @@ func PsOutputToProcesses(out string) *[]jsh.Process {
func runJsonMode() { func runJsonMode() {
// Run procps-ng "ps" with full output // Run procps-ng "ps" with full output
psOut := string(*jsh.FallbackWithArgs("/usr/bin/ps", []string{"auxww"})) psOut := string(*jsh.FallbackWithArgs("/usr/bin/ps", []string{"auxww"}))
processesPtr := PsOutputToProcesses(psOut) processesPtr := PsOutputToProcesses(psOut)
finalOut := jsh.JshOutput{*processesPtr, []string{}} frame := jsh.JshFrame{*processesPtr, []string{}}
fmt.Printf("%s", *finalOut.ToJson())
queue := make(chan *jsh.JshFrame)
done := make(chan bool)
go jsh.OutputFrames(queue, done)
queue <- &frame
close(queue)
<-done
} }
func main() { func main() {

View File

@ -2,9 +2,9 @@ package jsh
import ( import (
"math" "math"
"unicode"
"os" "os"
"os/exec" "os/exec"
"unicode"
) )
// FieldsN slices s into substrings after each instance of a whitespace // FieldsN slices s into substrings after each instance of a whitespace