mirror of
https://codeberg.org/scip/epuppy.git
synced 2026-02-04 09:40:57 +01:00
fix #2: add --create-config to create a default config file
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
Copyright © 2025 Thomas von Dein
|
Copyright © 2025-2026 Thomas von Dein
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
This program is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -27,6 +27,7 @@ import (
|
|||||||
"github.com/knadh/koanf/parsers/toml"
|
"github.com/knadh/koanf/parsers/toml"
|
||||||
"github.com/knadh/koanf/providers/file"
|
"github.com/knadh/koanf/providers/file"
|
||||||
"github.com/knadh/koanf/providers/posflag"
|
"github.com/knadh/koanf/providers/posflag"
|
||||||
|
"github.com/knadh/koanf/providers/structs"
|
||||||
"github.com/knadh/koanf/v2"
|
"github.com/knadh/koanf/v2"
|
||||||
flag "github.com/spf13/pflag"
|
flag "github.com/spf13/pflag"
|
||||||
)
|
)
|
||||||
@@ -42,6 +43,7 @@ Options:
|
|||||||
-s --store-progress remember reading position
|
-s --store-progress remember reading position
|
||||||
-n --line-numbers add line numbers
|
-n --line-numbers add line numbers
|
||||||
-c --config <file> use config <file>
|
-c --config <file> use config <file>
|
||||||
|
--create-config create a default config file
|
||||||
-i --cover-image display cover image
|
-i --cover-image display cover image
|
||||||
-t --txt dump readable content to STDOUT
|
-t --txt dump readable content to STDOUT
|
||||||
-x --xml dump source xml to STDOUT
|
-x --xml dump source xml to STDOUT
|
||||||
@@ -51,6 +53,31 @@ Options:
|
|||||||
-v --version show program version`
|
-v --version show program version`
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
DefaultDark = ColorSetting{
|
||||||
|
Title: "#ff4500",
|
||||||
|
Chapter: "#ff4500",
|
||||||
|
Body: "#cdb79e",
|
||||||
|
}
|
||||||
|
|
||||||
|
DefaultLight = ColorSetting{
|
||||||
|
Title: "#ff0000",
|
||||||
|
Chapter: "#8b0000",
|
||||||
|
Body: "#696969",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// used for config writing, it's a subset of the Config struct
|
||||||
|
// add fields as you add user configurable fields to Config
|
||||||
|
type UserConfig struct {
|
||||||
|
StoreProgress bool `koanf:"store-progress"`
|
||||||
|
Darkmode bool `koanf:"dark"`
|
||||||
|
LineNumbers bool `koanf:"line-numbers"`
|
||||||
|
NoColor bool `koanf:"no-color"`
|
||||||
|
ColorDark ColorSetting `koanf:"colordark"`
|
||||||
|
ColorLight ColorSetting `koanf:"colorlight"`
|
||||||
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Showversion bool `koanf:"version"` // -v
|
Showversion bool `koanf:"version"` // -v
|
||||||
Debug bool `koanf:"debug"` // -d
|
Debug bool `koanf:"debug"` // -d
|
||||||
@@ -64,13 +91,15 @@ type Config struct {
|
|||||||
ColorDark ColorSetting `koanf:"colordark"` // comes from config file only
|
ColorDark ColorSetting `koanf:"colordark"` // comes from config file only
|
||||||
ColorLight ColorSetting `koanf:"colorlight"` // comes from config file only
|
ColorLight ColorSetting `koanf:"colorlight"` // comes from config file only
|
||||||
ShowHelp bool `koanf:"help"`
|
ShowHelp bool `koanf:"help"`
|
||||||
ShowCover bool `koanf:"cover-image"` // -i
|
ShowCover bool `koanf:"cover-image"` // -i
|
||||||
|
CreateConfig bool `koanf:"create-config"` // --create-config
|
||||||
Colors Colors // generated from user config file or internal defaults, respects dark mode
|
Colors Colors // generated from user config file or internal defaults, respects dark mode
|
||||||
Document string
|
Document string
|
||||||
InitialProgress int // lines
|
InitialProgress int // lines
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitConfig(output io.Writer) (*Config, error) {
|
func InitConfig(output io.Writer) (*Config, error) {
|
||||||
|
conf := &Config{}
|
||||||
var kloader = koanf.New(".")
|
var kloader = koanf.New(".")
|
||||||
|
|
||||||
// setup custom usage
|
// setup custom usage
|
||||||
@@ -93,6 +122,7 @@ func InitConfig(output io.Writer) (*Config, error) {
|
|||||||
flagset.BoolP("no-color", "N", false, "disable colors")
|
flagset.BoolP("no-color", "N", false, "disable colors")
|
||||||
flagset.BoolP("cover-image", "i", false, "show cover image")
|
flagset.BoolP("cover-image", "i", false, "show cover image")
|
||||||
flagset.BoolP("help", "h", false, "show help")
|
flagset.BoolP("help", "h", false, "show help")
|
||||||
|
flagset.BoolP("create-config", "", false, "create a config file with default settings")
|
||||||
flagset.StringP("config", "c", "", "read config from file")
|
flagset.StringP("config", "c", "", "read config from file")
|
||||||
|
|
||||||
if err := flagset.Parse(os.Args[1:]); err != nil {
|
if err := flagset.Parse(os.Args[1:]); err != nil {
|
||||||
@@ -104,14 +134,13 @@ func InitConfig(output io.Writer) (*Config, error) {
|
|||||||
var configfiles []string
|
var configfiles []string
|
||||||
|
|
||||||
configfile, _ := flagset.GetString("config")
|
configfile, _ := flagset.GetString("config")
|
||||||
home, _ := os.UserHomeDir()
|
|
||||||
|
|
||||||
if configfile != "" {
|
if configfile != "" {
|
||||||
configfiles = []string{configfile}
|
configfiles = []string{configfile}
|
||||||
} else {
|
} else {
|
||||||
configfiles = []string{
|
configfiles = []string{
|
||||||
"/etc/epuppy.toml", "/usr/local/etc/epuppy.toml", // unix variants
|
"/etc/epuppy.toml", "/usr/local/etc/epuppy.toml", // unix variants
|
||||||
filepath.Join(home, ".config", "epuppy", "config.toml"),
|
filepath.Join(conf.GetConfigDir(), "config.toml"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,7 +161,6 @@ func InitConfig(output io.Writer) (*Config, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// fetch values
|
// fetch values
|
||||||
conf := &Config{}
|
|
||||||
if err := kloader.Unmarshal("", &conf); err != nil {
|
if err := kloader.Unmarshal("", &conf); err != nil {
|
||||||
return nil, fmt.Errorf("error unmarshalling: %w", err)
|
return nil, fmt.Errorf("error unmarshalling: %w", err)
|
||||||
}
|
}
|
||||||
@@ -141,7 +169,7 @@ func InitConfig(output io.Writer) (*Config, error) {
|
|||||||
if len(flagset.Args()) > 0 {
|
if len(flagset.Args()) > 0 {
|
||||||
conf.Document = flagset.Args()[0]
|
conf.Document = flagset.Args()[0]
|
||||||
} else {
|
} else {
|
||||||
if !conf.Showversion && !conf.ShowHelp {
|
if !conf.Showversion && !conf.ShowHelp && !conf.CreateConfig {
|
||||||
flagset.Usage()
|
flagset.Usage()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
@@ -152,18 +180,7 @@ func InitConfig(output io.Writer) (*Config, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// setup color config
|
// setup color config
|
||||||
conf.Colors = SetColorconfig(
|
conf.Colors = SetColorconfig(DefaultDark, DefaultLight, conf)
|
||||||
ColorSetting{ // Dark
|
|
||||||
Title: "#ff4500",
|
|
||||||
Chapter: "#ff4500",
|
|
||||||
Body: "#cdb79e",
|
|
||||||
},
|
|
||||||
ColorSetting{ // Light
|
|
||||||
Title: "#ff0000",
|
|
||||||
Chapter: "#8b0000",
|
|
||||||
Body: "#696969",
|
|
||||||
},
|
|
||||||
conf)
|
|
||||||
|
|
||||||
// disable colors if requested by command line
|
// disable colors if requested by command line
|
||||||
if conf.NoColor {
|
if conf.NoColor {
|
||||||
@@ -173,7 +190,41 @@ func InitConfig(output io.Writer) (*Config, error) {
|
|||||||
return conf, nil
|
return conf, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) GetConfigDir() string {
|
func (conf *Config) GetConfigDir() string {
|
||||||
home, _ := os.UserHomeDir()
|
home, _ := os.UserHomeDir()
|
||||||
return filepath.Join(home, ".config", "epuppy")
|
return filepath.Join(home, ".config", "epuppy")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (conf *Config) WriteConfigFile() (string, error) {
|
||||||
|
cfgfile := filepath.Join(conf.GetConfigDir(), "config.toml")
|
||||||
|
|
||||||
|
if FileExists(cfgfile) {
|
||||||
|
return "", fmt.Errorf("config file %s already exists.", cfgfile)
|
||||||
|
}
|
||||||
|
|
||||||
|
var kloader = koanf.New(".")
|
||||||
|
|
||||||
|
userconf := UserConfig{
|
||||||
|
StoreProgress: conf.StoreProgress,
|
||||||
|
Darkmode: conf.Darkmode,
|
||||||
|
LineNumbers: conf.LineNumbers,
|
||||||
|
NoColor: conf.NoColor,
|
||||||
|
ColorDark: DefaultDark,
|
||||||
|
ColorLight: DefaultLight,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := kloader.Load(structs.Provider(userconf, "koanf"), nil); err != nil {
|
||||||
|
return "", fmt.Errorf("error loading config struct: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
toml, err := kloader.Marshal(toml.Parser())
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("error unmarshalling config: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := WriteFile(cfgfile, toml); err != nil {
|
||||||
|
return "", fmt.Errorf("error writing config file %s: %w", cfgfile, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("new config file %s created.", cfgfile), nil
|
||||||
|
}
|
||||||
|
|||||||
10
cmd/root.go
10
cmd/root.go
@@ -52,6 +52,16 @@ func Execute(output io.Writer) int {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if conf.CreateConfig {
|
||||||
|
msg, err := conf.WriteConfigFile()
|
||||||
|
if err != nil {
|
||||||
|
return Die(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintln(output, msg)
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
if conf.StoreProgress {
|
if conf.StoreProgress {
|
||||||
progress, err := GetProgress(conf)
|
progress, err := GetProgress(conf)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -44,10 +44,12 @@ require (
|
|||||||
github.com/blacktop/go-termimg v0.1.20 // indirect
|
github.com/blacktop/go-termimg v0.1.20 // indirect
|
||||||
github.com/charmbracelet/x/mosaic v0.0.0-20250702191427-5bdfc8f2e4ff // indirect
|
github.com/charmbracelet/x/mosaic v0.0.0-20250702191427-5bdfc8f2e4ff // indirect
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
|
github.com/fatih/structs v1.1.0 // indirect
|
||||||
github.com/fsnotify/fsnotify v1.9.0 // indirect
|
github.com/fsnotify/fsnotify v1.9.0 // indirect
|
||||||
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
|
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
|
||||||
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
|
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
|
||||||
github.com/knadh/koanf/maps v0.1.2 // indirect
|
github.com/knadh/koanf/maps v0.1.2 // indirect
|
||||||
|
github.com/knadh/koanf/providers/structs v1.0.0 // indirect
|
||||||
github.com/makeworld-the-better-one/dither/v2 v2.4.0 // indirect
|
github.com/makeworld-the-better-one/dither/v2 v2.4.0 // indirect
|
||||||
github.com/mattn/go-sixel v0.0.5 // indirect
|
github.com/mattn/go-sixel v0.0.5 // indirect
|
||||||
github.com/mitchellh/copystructure v1.2.0 // indirect
|
github.com/mitchellh/copystructure v1.2.0 // indirect
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -33,6 +33,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
|
|||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
|
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
|
||||||
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
|
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
|
||||||
|
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
|
||||||
|
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
|
||||||
github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
|
github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
|
||||||
github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
|
github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
|
||||||
github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs=
|
github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs=
|
||||||
@@ -48,6 +50,8 @@ github.com/knadh/koanf/providers/file v1.2.0 h1:hrUJ6Y9YOA49aNu/RSYzOTFlqzXSCpmY
|
|||||||
github.com/knadh/koanf/providers/file v1.2.0/go.mod h1:bp1PM5f83Q+TOUu10J/0ApLBd9uIzg+n9UgthfY+nRA=
|
github.com/knadh/koanf/providers/file v1.2.0/go.mod h1:bp1PM5f83Q+TOUu10J/0ApLBd9uIzg+n9UgthfY+nRA=
|
||||||
github.com/knadh/koanf/providers/posflag v1.0.1 h1:EnMxHSrPkYCFnKgBUl5KBgrjed8gVFrcXDzaW4l/C6Y=
|
github.com/knadh/koanf/providers/posflag v1.0.1 h1:EnMxHSrPkYCFnKgBUl5KBgrjed8gVFrcXDzaW4l/C6Y=
|
||||||
github.com/knadh/koanf/providers/posflag v1.0.1/go.mod h1:3Wn3+YG3f4ljzRyCUgIwH7G0sZ1pMjCOsNBovrbKmAk=
|
github.com/knadh/koanf/providers/posflag v1.0.1/go.mod h1:3Wn3+YG3f4ljzRyCUgIwH7G0sZ1pMjCOsNBovrbKmAk=
|
||||||
|
github.com/knadh/koanf/providers/structs v1.0.0 h1:DznjB7NQykhqCar2LvNug3MuxEQsZ5KvfgMbio+23u4=
|
||||||
|
github.com/knadh/koanf/providers/structs v1.0.0/go.mod h1:kjo5TFtgpaZORlpoJqcbeLowM2cINodv8kX+oFAeQ1w=
|
||||||
github.com/knadh/koanf/v2 v2.3.0 h1:Qg076dDRFHvqnKG97ZEsi9TAg2/nFTa9hCdcSa1lvlM=
|
github.com/knadh/koanf/v2 v2.3.0 h1:Qg076dDRFHvqnKG97ZEsi9TAg2/nFTa9hCdcSa1lvlM=
|
||||||
github.com/knadh/koanf/v2 v2.3.0/go.mod h1:gRb40VRAbd4iJMYYD5IxZ6hfuopFcXBpc9bbQpZwo28=
|
github.com/knadh/koanf/v2 v2.3.0/go.mod h1:gRb40VRAbd4iJMYYD5IxZ6hfuopFcXBpc9bbQpZwo28=
|
||||||
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
||||||
|
|||||||
Reference in New Issue
Block a user