56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"jsh"
|
|
)
|
|
|
|
func catFiles(queue chan *jsh.JshFrame) {
|
|
// Get all of the non-flags. The rest were processed by the main function
|
|
fileNames := flag.Args()
|
|
for _, fileName := range fileNames {
|
|
file, err := ioutil.ReadFile(fileName)
|
|
|
|
var jshFrame jsh.JshFrame
|
|
|
|
// If there's an error, output an error frame to the queue and continue
|
|
if err != nil {
|
|
errText := make(map[string]string)
|
|
errText[fileName] = fmt.Sprintf("%s", err.Error())
|
|
jshFrame = jsh.JshFrame{"", errText}
|
|
queue <- &jshFrame
|
|
continue
|
|
}
|
|
|
|
// Put the file contents into a frame and send them to the output
|
|
output := jsh.CatOutput{}
|
|
output.Files = []string{fileName}
|
|
output.Output = string(file)
|
|
jshFrame = jsh.JshFrame{output, ""}
|
|
queue <- &jshFrame
|
|
}
|
|
}
|
|
|
|
func runJsonMode() {
|
|
queue := make(chan *jsh.JshFrame)
|
|
done := make(chan bool)
|
|
go jsh.OutputFrames(queue, done)
|
|
catFiles(queue)
|
|
close(queue)
|
|
<-done
|
|
}
|
|
|
|
func main() {
|
|
// TODO: Support more flags
|
|
jsonModePtr := flag.Bool("json", false, "whether to use json mode for input and output")
|
|
flag.Parse()
|
|
|
|
if !*jsonModePtr {
|
|
fmt.Printf("%s", jsh.FallbackWithArgs("/usr/bin/cat", flag.Args()))
|
|
} else {
|
|
runJsonMode()
|
|
}
|
|
}
|