2020-10-26 16:21:23 +00:00
package main
import (
2024-07-09 19:13:05 +00:00
"errors"
2020-10-26 16:21:23 +00:00
"fmt"
2024-07-09 19:13:05 +00:00
"io/fs"
2020-10-26 16:21:23 +00:00
"os"
2020-10-26 19:21:38 +00:00
"path"
2022-09-15 23:41:28 +00:00
"path/filepath"
2020-10-26 16:21:23 +00:00
2023-11-17 12:02:52 +00:00
"github.com/charmbracelet/x/editor"
2020-10-26 16:21:23 +00:00
"github.com/spf13/cobra"
2024-07-09 01:33:49 +00:00
"github.com/spf13/viper"
2020-10-26 16:21:23 +00:00
)
2020-10-26 19:21:38 +00:00
const defaultConfig = ` # style name or JSON path ( default "auto" )
2020-10-26 20:02:54 +00:00
style : "auto"
2021-02-04 00:38:52 +00:00
# mouse support ( TUI - mode only )
mouse : false
# use pager to display markdown
pager : false
2020-10-26 19:21:38 +00:00
# word - wrap at width
2024-07-03 15:11:29 +00:00
width : 80
2024-10-01 17:23:20 +00:00
# show all files , including hidden and ignored .
all : true
2024-07-03 15:11:29 +00:00
`
2020-10-26 19:21:38 +00:00
2024-07-03 15:11:29 +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. 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" ) ,
Args : cobra . NoArgs ,
2024-07-03 16:05:46 +00:00
RunE : func ( * cobra . Command , [ ] string ) error {
2024-07-03 15:11:29 +00:00
if err := ensureConfigFile ( ) ; err != nil {
2020-10-26 19:21:38 +00:00
return err
}
2023-11-17 12:02:52 +00:00
c , err := editor . Cmd ( "Glow" , configFile )
2023-08-28 12:05:12 +00:00
if err != nil {
2024-07-03 15:11:29 +00:00
return err
2023-08-28 12:05:12 +00:00
}
2020-10-26 19:21:38 +00: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 16:21:23 +00:00
return nil
} ,
}
2024-07-03 15:11:29 +00:00
func ensureConfigFile ( ) error {
if configFile == "" {
2024-07-09 01:33:49 +00:00
configFile = viper . GetViper ( ) . ConfigFileUsed ( )
2024-07-03 15:11:29 +00:00
if err := os . MkdirAll ( filepath . Dir ( configFile ) , 0 o755 ) ; err != nil {
2024-07-09 13:47:44 +00:00
return fmt . Errorf ( "could not write configuration file: %w" , err )
2024-07-03 15:11:29 +00:00
}
}
if ext := path . Ext ( configFile ) ; ext != ".yaml" && ext != ".yml" {
2024-07-09 13:47:44 +00:00
return fmt . Errorf ( "'%s' is not a supported configuration type: use '%s' or '%s'" , ext , ".yaml" , ".yml" )
2024-07-03 15:11:29 +00:00
}
2024-07-09 19:13:05 +00:00
if _ , err := os . Stat ( configFile ) ; errors . Is ( err , fs . ErrNotExist ) {
2024-07-03 15:11:29 +00:00
// File doesn't exist yet, create all necessary directories and
// write the default config file
if err := os . MkdirAll ( filepath . Dir ( configFile ) , 0 o700 ) ; 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
}
return nil
}