mirror of
https://github.com/sbrow/envr.git
synced 2026-02-27 20:11:45 -05:00
refactor: Rewrote in golang.
This commit is contained in:
55
cmd/edit_config.go
Normal file
55
cmd/edit_config.go
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var editConfigCmd = &cobra.Command{
|
||||
Use: "edit-config",
|
||||
Short: "Edit your config with your default editor",
|
||||
// Long: ``,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
editor := os.Getenv("EDITOR")
|
||||
if editor == "" {
|
||||
fmt.Println("Error: $EDITOR environment variable is not set")
|
||||
return
|
||||
}
|
||||
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
fmt.Printf("Error getting home directory: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
configPath := filepath.Join(homeDir, ".envr", "config.json")
|
||||
|
||||
// Check if config file exists
|
||||
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
||||
fmt.Printf("Config file does not exist at %s. Run 'envr init' first.\n", configPath)
|
||||
return
|
||||
}
|
||||
|
||||
// Execute the editor
|
||||
execCmd := exec.Command(editor, configPath)
|
||||
execCmd.Stdin = os.Stdin
|
||||
execCmd.Stdout = os.Stdout
|
||||
execCmd.Stderr = os.Stderr
|
||||
|
||||
if err := execCmd.Run(); err != nil {
|
||||
fmt.Printf("Error running editor: %v\n", err)
|
||||
return
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(editConfigCmd)
|
||||
}
|
||||
Reference in New Issue
Block a user