glow/config_cmd.go

83 lines
2.1 KiB
Go
Raw Permalink Normal View History

2020-10-26 16:21:23 +00:00
package main
import (
"fmt"
"os"
"path"
"path/filepath"
2020-10-26 16:21:23 +00:00
"github.com/charmbracelet/x/editor"
gap "github.com/muesli/go-app-paths"
2020-10-26 16:21:23 +00:00
"github.com/spf13/cobra"
)
const defaultConfig = `# style name or JSON path (default "auto")
style: "auto"
# show local files only; no network (TUI-mode only)
local: false
# mouse support (TUI-mode only)
mouse: false
# use pager to display markdown
pager: false
# word-wrap at width
width: 80`
2020-10-26 16:21:23 +00:00
var configCmd = &cobra.Command{
Use: "config",
Hidden: false,
Short: "Edit the glow config file",
Long: paragraph(fmt.Sprintf("\n%s the glow config file. Well use EDITOR to determine which editor to use. If the config file doesn't exist, it will be created.", keyword("Edit"))),
Example: paragraph("glow config\nglow config --config path/to/config.yml"),
Args: cobra.NoArgs,
SilenceUsage: true,
RunE: func(_ *cobra.Command, _ []string) error {
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'", 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
2022-10-25 14:40:51 +00:00
if err := os.MkdirAll(filepath.Dir(configFile), 0o700); err != nil {
return err
}
f, err := os.Create(configFile)
if err != nil {
return err
}
defer func() { _ = f.Close() }()
if _, err := f.WriteString(defaultConfig); err != nil {
return err
}
} else if err != nil { // some other error occurred
return err
}
c, err := editor.Cmd("Glow", configFile)
if err != nil {
return fmt.Errorf("could not edit %s: %w", configFile, err)
}
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)
2020-10-26 16:21:23 +00:00
return nil
},
}