diff --git a/config_cmd.go b/config_cmd.go index 917a773..800524f 100644 --- a/config_cmd.go +++ b/config_cmd.go @@ -4,22 +4,77 @@ import ( "errors" "fmt" "os" + "os/exec" + "path" "github.com/charmbracelet/charm/ui/common" + gap "github.com/muesli/go-app-paths" "github.com/spf13/cobra" ) +const defaultConfig = `# style name or JSON path (default "auto") +style: "light" +# show local files only; no network (TUI-mode only) +local: true +# word-wrap at width +width: 80` + var configCmd = &cobra.Command{ - Use: "config", - Hidden: false, - Short: "Edit the glow config file", - Long: formatBlock(fmt.Sprintf("\n%s the glow config file. We’ll use EDITOR to determine which editor to use. If the config file doesn't exist, it will be created.", common.Keyword("Edit"))), - Args: cobra.NoArgs, + Use: "config", + Hidden: false, + Short: "Edit the glow config file", + Long: formatBlock(fmt.Sprintf("\n%s the glow config file. We’ll use EDITOR to determine which editor to use. If the config file doesn't exist, it will be created.", common.Keyword("Edit"))), + Example: formatBlock("glow config\nglow config --config path/to/config.yml"), + Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - if os.Getenv("EDITORS") == "" { + if configFile == "" { + scope := gap.NewScope(gap.User, "glow") + + var err error + configFile, err = scope.ConfigPath("glow.yml") + if err != nil { + return err + } + } + + if ext := path.Ext(configFile); ext != ".yaml" && ext != ".yml" { + return fmt.Errorf("'%s' is not a supported config type: use '%s' or '%s'\n", ext, ".yaml", ".yml") + } + + if _, err := os.Stat(configFile); os.IsNotExist(err) { + // File doesn't exist yet, create all necessary directories and + // write the default config file + if err := os.MkdirAll(path.Dir(configFile), 0700); err != nil { + return err + } + + f, err := os.Create(configFile) + if err != nil { + return err + } + defer f.Close() + + if _, err := f.WriteString(defaultConfig); err != nil { + return err + } + } else if err != nil { // some other error occurred + return err + } + + editor := os.Getenv("EDITOR") + if editor == "" { return errors.New("no EDITOR environment variable set") } + c := exec.Command(editor, configFile) + c.Stdin = os.Stdin + c.Stdout = os.Stdout + c.Stderr = os.Stderr + if err := c.Run(); err != nil { + return err + } + + fmt.Println("Wrote config file to:", configFile) return nil }, } diff --git a/main.go b/main.go index e799ca3..4999ea8 100644 --- a/main.go +++ b/main.go @@ -163,6 +163,7 @@ func validateOptions(cmd *cobra.Command) { } func execute(cmd *cobra.Command, args []string) error { + initConfig() validateOptions(cmd) if len(args) == 0 { @@ -334,8 +335,6 @@ func init() { rootCmd.AddCommand(stashCmd) rootCmd.AddCommand(configCmd) - - cobra.OnInitialize(initConfig) } func initConfig() { diff --git a/stash_cmd.go b/stash_cmd.go index d897a49..e1d69a7 100644 --- a/stash_cmd.go +++ b/stash_cmd.go @@ -25,6 +25,7 @@ var ( Example: formatBlock("glow stash\nglow stash README.md\nglow stash -m \"secret notes\" path/to/notes.md"), Args: cobra.MaximumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + initConfig() if len(args) == 0 { return runTUI(true) }