diff --git a/cmd/edit.go b/cmd/edit.go new file mode 100644 index 0000000..bc9da0d --- /dev/null +++ b/cmd/edit.go @@ -0,0 +1,57 @@ +/* +Copyright © 2025 NAME HERE +*/ +package cmd + +import ( + "envy/cmd/utils" + "os" + "os/exec" + "path" + "runtime" + + "github.com/spf13/cobra" +) + +// editCmd represents the edit command +var editCmd = &cobra.Command{ + Use: "edit", + Short: "Open .env file in the default editor", + Run: func(cmd *cobra.Command, args []string) { + editor := os.Getenv("EDITOR") + if editor == "" { + switch runtime.GOOS { + case "windows": + editor = "notepad" + default: + editor = "nano" + } + } + + cwd, err := os.Getwd() + utils.StopIfErr(err) + + edit := exec.Command(editor, path.Join(cwd, ".env")) + + edit.Stdin = os.Stdin + edit.Stdout = os.Stdout + edit.Stderr = os.Stderr + + err = edit.Run() + utils.StopIfErr(err) + }, +} + +func init() { + rootCmd.AddCommand(editCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // editCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // editCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +}