github.com/GGP1/kure@v0.8.4/commands/config/create/create.go (about) 1 package create 2 3 import ( 4 "os" 5 "os/exec" 6 7 cmdutil "github.com/GGP1/kure/commands" 8 "github.com/GGP1/kure/config" 9 10 "github.com/pkg/errors" 11 "github.com/spf13/cobra" 12 ) 13 14 const example = ` 15 kure config create -p path/to/file` 16 17 type createOptions struct { 18 path string 19 } 20 21 // NewCmd returns a new command. 22 func NewCmd() *cobra.Command { 23 opts := createOptions{} 24 cmd := &cobra.Command{ 25 Use: "create", 26 Short: "Create a configuration file", 27 Example: example, 28 RunE: runCreate(&opts), 29 PostRun: func(cmd *cobra.Command, args []string) { 30 // Reset variables (session) 31 opts = createOptions{} 32 }, 33 } 34 35 f := cmd.Flags() 36 f.StringVarP(&opts.path, "path", "p", "", "destination file path") 37 38 return cmd 39 } 40 41 func runCreate(opts *createOptions) cmdutil.RunEFunc { 42 return func(cmd *cobra.Command, args []string) error { 43 if opts.path == "" { 44 return cmdutil.ErrInvalidPath 45 } 46 47 if err := config.WriteStruct(opts.path); err != nil { 48 return err 49 } 50 51 editor := cmdutil.SelectEditor() 52 bin, err := exec.LookPath(editor) 53 if err != nil { 54 return errors.Errorf("%q executable not found", editor) 55 } 56 57 edit := exec.Command(bin, opts.path) 58 edit.Stdin = os.Stdin 59 edit.Stdout = os.Stdout 60 61 if err := edit.Run(); err != nil { 62 return errors.Wrap(err, "executing command") 63 } 64 65 return nil 66 } 67 }