fix: editor with args (#364)

* 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>
This commit is contained in:
Carlos Alexandro Becker 2022-10-25 15:35:12 -03:00 committed by GitHub
parent 44b078e44a
commit 5f14914b7e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -6,6 +6,7 @@ import (
"os"
"os/exec"
"path"
"strings"
"path/filepath"
"github.com/charmbracelet/charm/ui/common"
@ -32,8 +33,8 @@ var configCmd = &cobra.Command{
Example: formatBlock("glow config\nglow config --config path/to/config.yml"),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
editor := os.Getenv("EDITOR")
if editor == "" {
editor := strings.Fields(os.Getenv("EDITOR"))
if len(editor) == 0 {
return errors.New("no EDITOR environment variable set")
}
@ -48,7 +49,7 @@ var configCmd = &cobra.Command{
}
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")
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) {
@ -62,7 +63,7 @@ var configCmd = &cobra.Command{
if err != nil {
return err
}
defer f.Close()
defer func() { _ = f.Close() }()
if _, err := f.WriteString(defaultConfig); err != nil {
return err
@ -71,7 +72,11 @@ var configCmd = &cobra.Command{
return err
}
c := exec.Command(editor, configFile)
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