mirror of
https://github.com/silicoflare/envy.git
synced 2026-05-26 11:49:52 +05:30
feat: implement edit command
This commit is contained in:
57
cmd/edit.go
Normal file
57
cmd/edit.go
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
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")
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user