mirror of
https://github.com/charmbracelet/glow
synced 2024-11-10 06:04:18 +00:00
5f14914b7e
* fix: editor with args Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * Update config_cmd.go Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
90 lines
2.2 KiB
Go
90 lines
2.2 KiB
Go
package main
|
||
|
||
import (
|
||
"errors"
|
||
"fmt"
|
||
"os"
|
||
"os/exec"
|
||
"path"
|
||
"strings"
|
||
"path/filepath"
|
||
|
||
"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: "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`
|
||
|
||
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"))),
|
||
Example: formatBlock("glow config\nglow config --config path/to/config.yml"),
|
||
Args: cobra.NoArgs,
|
||
RunE: func(cmd *cobra.Command, args []string) error {
|
||
editor := strings.Fields(os.Getenv("EDITOR"))
|
||
if len(editor) == 0 {
|
||
return errors.New("no EDITOR environment variable set")
|
||
}
|
||
|
||
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
|
||
if err := os.MkdirAll(filepath.Dir(configFile), 0700); 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
|
||
}
|
||
|
||
var eargs []string
|
||
if len(editor) > 1 {
|
||
eargs = editor[1:]
|
||
}
|
||
c := exec.Command(editor[0], append(eargs, configFile)...) // nolint: gosec
|
||
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
|
||
},
|
||
}
|