Files
envy/cmd/edit.go

58 lines
1.2 KiB
Go

/*
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
*/
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")
}