No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.go 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "io/ioutil"
  6. "jsh"
  7. )
  8. func catFiles(queue chan *jsh.JshFrame) {
  9. // Get all of the non-flags. The rest were processed by the main function
  10. fileNames := flag.Args()
  11. for _, fileName := range fileNames {
  12. file, err := ioutil.ReadFile(fileName)
  13. var jshFrame jsh.JshFrame
  14. // If there's an error, output an error frame to the queue and continue
  15. if err != nil {
  16. errText := make(map[string]string)
  17. errText[fileName] = fmt.Sprintf("%s", err.Error())
  18. jshFrame = jsh.JshFrame{"", errText}
  19. queue <- &jshFrame
  20. continue
  21. }
  22. // Put the file contents into a frame and send them to the output
  23. output := jsh.CatOutput{}
  24. output.Files = []string{fileName}
  25. output.Output = string(file)
  26. jshFrame = jsh.JshFrame{output, ""}
  27. queue <- &jshFrame
  28. }
  29. }
  30. func runJsonMode() {
  31. queue := make(chan *jsh.JshFrame)
  32. done := make(chan bool)
  33. go jsh.OutputFrames(queue, done)
  34. catFiles(queue)
  35. close(queue)
  36. <-done
  37. }
  38. func main() {
  39. // TODO: Support more flags
  40. jsonModePtr := flag.Bool("json", false, "whether to use json mode for input and output")
  41. flag.Parse()
  42. if !*jsonModePtr {
  43. fmt.Printf("%s", jsh.FallbackWithArgs("cat", flag.Args()))
  44. } else {
  45. runJsonMode()
  46. }
  47. }