()
| 16 | } |
| 17 | |
| 18 | func newEditCmd() *editCmd { |
| 19 | cmd := &cobra.Command{ |
| 20 | Use: "edit", |
| 21 | Short: "Syntactic sugar for to-json | $EDITOR | from-json", |
| 22 | Aliases: []string{"e"}, |
| 23 | Args: cobra.NoArgs, |
| 24 | RunE: func(cmd *cobra.Command, args []string) error { |
| 25 | tmp := filepath.Join(os.TempDir(), fmt.Sprintf("tt-%d.json", time.Now().Unix())) |
| 26 | |
| 27 | if err := newToJSONCmd().cmd.RunE(cmd, []string{tmp}); err != nil { |
| 28 | return err |
| 29 | } |
| 30 | |
| 31 | editor := strings.Fields(os.Getenv("EDITOR")) |
| 32 | if len(editor) == 0 { |
| 33 | return fmt.Errorf("no $EDITOR set") |
| 34 | } |
| 35 | |
| 36 | editorCmd := editor[0] |
| 37 | var editorArgs []string |
| 38 | if len(editor) > 1 { |
| 39 | editorArgs = append(editorArgs, editor[1:]...) |
| 40 | } |
| 41 | editorArgs = append(editorArgs, tmp) |
| 42 | |
| 43 | edit := exec.Command(editorCmd, editorArgs...) |
| 44 | edit.Stderr = os.Stderr |
| 45 | edit.Stdout = os.Stdout |
| 46 | edit.Stdin = os.Stdin |
| 47 | if err := edit.Run(); err != nil { |
| 48 | return err |
| 49 | } |
| 50 | |
| 51 | return newFromJSONCmd().cmd.RunE(cmd, []string{tmp}) |
| 52 | }, |
| 53 | } |
| 54 | |
| 55 | return &editCmd{cmd: cmd} |
| 56 | } |
no test coverage detected