2020-10-26 12:21:23 -04:00
package main
import (
"errors"
"fmt"
"os"
2020-10-26 20:21:38 +01:00
"os/exec"
"path"
2022-09-15 17:41:28 -06:00
"path/filepath"
2022-10-25 11:40:51 -03:00
"strings"
2020-10-26 12:21:23 -04:00
2020-10-26 20:21:38 +01:00
gap "github.com/muesli/go-app-paths"
2020-10-26 12:21:23 -04:00
"github.com/spf13/cobra"
)
2020-10-26 20:21:38 +01:00
const defaultConfig = ` # style name or JSON path ( default "auto" )
2020-10-26 21:02:54 +01:00
style : "auto"
2020-10-26 20:21:38 +01:00
# show local files only ; no network ( TUI - mode only )
2020-10-26 21:02:54 +01:00
local : false
2021-02-04 01:38:52 +01:00
# mouse support ( TUI - mode only )
mouse : false
# use pager to display markdown
pager : false
2020-10-26 20:21:38 +01:00
# word - wrap at width
width : 80 `
2020-10-26 12:21:23 -04:00
var configCmd = & cobra . Command {
2020-10-26 20:21:38 +01:00
Use : "config" ,
Hidden : false ,
Short : "Edit the glow config file" ,
2021-06-14 16:52:00 -04:00
Long : paragraph ( 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." , keyword ( "Edit" ) ) ) ,
Example : paragraph ( "glow config\nglow config --config path/to/config.yml" ) ,
2020-10-26 20:21:38 +01:00
Args : cobra . NoArgs ,
2020-10-26 12:21:23 -04:00
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2022-10-25 15:35:12 -03:00
editor := strings . Fields ( os . Getenv ( "EDITOR" ) )
if len ( editor ) == 0 {
2020-10-26 21:02:09 +01:00
return errors . New ( "no EDITOR environment variable set" )
}
2020-10-26 20:21:38 +01:00
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" {
2022-10-25 15:35:12 -03:00
return fmt . Errorf ( "'%s' is not a supported config type: use '%s' or '%s'" , ext , ".yaml" , ".yml" )
2020-10-26 20:21:38 +01:00
}
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 11:40:51 -03:00
if err := os . MkdirAll ( filepath . Dir ( configFile ) , 0 o700 ) ; err != nil {
2020-10-26 20:21:38 +01:00
return err
}
f , err := os . Create ( configFile )
if err != nil {
return err
}
2022-10-25 15:35:12 -03:00
defer func ( ) { _ = f . Close ( ) } ( )
2020-10-26 20:21:38 +01:00
if _ , err := f . WriteString ( defaultConfig ) ; err != nil {
return err
}
} else if err != nil { // some other error occurred
return err
}
2022-10-25 15:35:12 -03:00
var eargs [ ] string
if len ( editor ) > 1 {
eargs = editor [ 1 : ]
}
c := exec . Command ( editor [ 0 ] , append ( eargs , configFile ) ... ) // nolint: gosec
2020-10-26 20:21:38 +01:00
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 12:21:23 -04:00
return nil
} ,
}