feat: allow config from CHARM_CONFIG_HOME and XDG_CONFIG_HOME (#621)

* feat: allow config from CHARM_CONFIG_HOME

closes #594
closes #495
closes #328
closes #475

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* fix: improvements on config handling

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* chore: log

---------

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
This commit is contained in:
Carlos Alexandro Becker 2024-07-08 22:33:49 -03:00 committed by GitHub
commit fe066f2140
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 18 additions and 15 deletions

View file

@ -7,8 +7,8 @@ import (
"path/filepath"
"github.com/charmbracelet/x/editor"
gap "github.com/muesli/go-app-paths"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const defaultConfig = `# style name or JSON path (default "auto")
@ -21,12 +21,6 @@ pager: false
width: 80
`
func defaultConfigFile() string {
scope := gap.NewScope(gap.User, "glow")
path, _ := scope.ConfigPath("glow.yml")
return path
}
var configCmd = &cobra.Command{
Use: "config",
Hidden: false,
@ -57,7 +51,7 @@ var configCmd = &cobra.Command{
func ensureConfigFile() error {
if configFile == "" {
configFile = defaultConfigFile()
configFile = viper.GetViper().ConfigFileUsed()
if err := os.MkdirAll(filepath.Dir(configFile), 0o755); err != nil {
return fmt.Errorf("Could not write config file: %w", err)
}

1
go.mod
View file

@ -3,7 +3,6 @@ module github.com/charmbracelet/glow
go 1.21.4
require (
github.com/adrg/xdg v0.4.0
github.com/atotto/clipboard v0.1.4
github.com/caarlos0/env/v11 v11.0.1
github.com/charmbracelet/bubbles v0.18.0

3
go.sum
View file

@ -38,8 +38,6 @@ cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3f
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls=
github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E=
github.com/alecthomas/assert/v2 v2.2.1 h1:XivOgYcduV98QCahG8T5XTezV5bylXe+lBxLG2K2ink=
github.com/alecthomas/assert/v2 v2.2.1/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ=
github.com/alecthomas/chroma/v2 v2.8.0 h1:w9WJUjFFmHHB2e8mRpL9jjy3alYDlU0QLDezj1xE264=
@ -426,7 +424,6 @@ golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=

9
log.go
View file

@ -2,13 +2,18 @@ package main
import (
"os"
"path/filepath"
"github.com/adrg/xdg"
"github.com/charmbracelet/log"
gap "github.com/muesli/go-app-paths"
)
func getLogFilePath() (string, error) {
return xdg.CacheFile("glow/glow.log")
dir, err := gap.NewScope(gap.User, "glow").CacheDir()
if err != nil {
return "", err
}
return filepath.Join(dir, "glow.log"), nil
}
func setupLog() (func() error, error) {

10
main.go
View file

@ -367,7 +367,7 @@ func init() {
rootCmd.InitDefaultCompletionCmd()
// "Glow Classic" cli arguments
rootCmd.PersistentFlags().StringVar(&configFile, "config", "", fmt.Sprintf("config file (default %s)", defaultConfigFile()))
rootCmd.PersistentFlags().StringVar(&configFile, "config", "", fmt.Sprintf("config file (default %s)", viper.GetViper().ConfigFileUsed()))
rootCmd.Flags().BoolVarP(&pager, "pager", "p", false, "display with pager")
rootCmd.Flags().StringVarP(&style, "style", "s", glamour.AutoStyle, "style name or JSON path")
rootCmd.Flags().UintVarP(&width, "width", "w", 0, "word-wrap at width")
@ -395,6 +395,14 @@ func tryLoadConfigFromDefaultPlaces() {
os.Exit(1)
}
if c := os.Getenv("CHARM_CONFIG_HOME"); c != "" {
viper.AddConfigPath(c)
}
if c := os.Getenv("XDG_CONFIG_HOME"); c != "" {
viper.AddConfigPath(filepath.Join(c, "glow"))
}
for _, v := range dirs {
viper.AddConfigPath(v)
}