sane default config file path, better cli doc

This commit is contained in:
blob42 2023-09-25 22:03:31 +02:00
parent 06d4d91d83
commit 8ff815a826
7 changed files with 93 additions and 18 deletions

View File

@ -43,12 +43,12 @@ var globalFirefoxFlags = []cli.Flag{
&cli.StringFlag{
Name: FirefoxProfileFlag,
Category: "firefox",
Usage: "Set the default firefox `PROFILE` to use",
Usage: "set the default firefox `PROFILE` to use",
},
&cli.BoolFlag{
Name: "ff-watch-all-profiles",
Category: "firefox",
Usage: "TOFIX! Watch all detected firefox profiles at the same time.",
Usage: "watch all firefox profiles for changes",
Aliases: []string{"ff-watch-all"},
},
}

View File

@ -23,7 +23,6 @@ package main
import (
"git.blob42.xyz/gosuki/gosuki/internal/config"
"git.blob42.xyz/gosuki/gosuki/internal/utils"
)
func initDefaultConfig() {
@ -40,7 +39,7 @@ func initConfig() {
log.Debugf("gosuki init config")
// Check if config file exists
exists, err := utils.CheckFileExists(config.ConfigFile)
exists, err := config.ConfigExists()
if err != nil {
log.Fatal(err)
}

View File

@ -53,22 +53,35 @@ func main() {
flags := []cli.Flag{
//TODO!: load config file provided by user
&cli.StringFlag{
Name: "config-file",
Value: config.ConfigFile,
Usage: "TOML config `FILE` path",
Name: "config",
Aliases: []string{"c"},
Value: config.ConfigFile(),
Usage: "load config from `FILE`",
DefaultText: "~/.config/gosuki/config.toml",
Category: "_",
},
&cli.IntFlag{
Name: "debug",
Category: "_",
Aliases: []string{"d"},
DefaultText: "0",
Usage: "set debug level. (`0`-3)",
EnvVars: []string{logging.EnvGosukiDebug},
Action: func (c *cli.Context, val int) error {
Action: func (_ *cli.Context, val int) error {
logging.SetMode(val)
return nil
},
},
// &cli.BoolFlag{
// Name: "help-more-options",
// Usage: "show more options",
// Aliases: []string{"H"},
// Category: "_",
// },
}
flags = append(flags, config.SetupGlobalFlags()...)
@ -152,7 +165,7 @@ func main() {
log.Fatal(err)
}
// log.Debugf("flags: %s", app.Flags)
log.Debugf("flags: %s", app.Flags)
}

View File

@ -66,6 +66,7 @@ var ModuleCmds = &cli.Command {
var listModulesCmd = &cli.Command{
Name: "list",
Aliases: []string{"l"},
Usage: "list available browsers and modules",
Action: func(_ *cli.Context) error {

View File

@ -42,7 +42,6 @@ var (
)
const (
ConfigFile = "config.toml"
GlobalConfigName = "global"
)

View File

@ -18,16 +18,80 @@
// You should have received a copy of the GNU Affero General Public License
// along with gosuki. If not, see <http://www.gnu.org/licenses/>.
package config
//TODO: load config path from cli flag/env var
import (
"errors"
"fmt"
"os"
"path"
"github.com/BurntSushi/toml"
"git.blob42.xyz/gosuki/gosuki/internal/utils"
)
const (
ConfigFileName = "config.toml"
ConfigDirName = "gosuki"
)
func getConfigDir() (string, error) {
configDir, err := os.UserConfigDir()
if err != nil {
return "", fmt.Errorf("could not get config dir: %s", err)
}
if configDir == "" {
return "", errors.New("could not get config dir")
}
configDir = path.Join(configDir, ConfigDirName)
return configDir, nil
}
func getConfigFile() (string, error) {
if configDir, err := getConfigDir(); err != nil {
return "", err
} else {
return path.Join(configDir, ConfigFileName), nil
}
}
func ConfigFile() string {
configFile, err := getConfigFile()
if err != nil {
log.Fatal(err)
}
return configFile
}
func ConfigExists() (bool, error) {
configFile, err := getConfigFile()
if err != nil {
return false, err
}
return utils.CheckFileExists(configFile)
}
// Create a toml config file
func InitConfigFile() error {
configFile, err := os.Create(ConfigFile)
var configDir string
var err error
if configDir, err = getConfigDir(); err != nil {
return err
}
if err = os.MkdirAll(configDir, 0755); err != nil {
return fmt.Errorf("could not create config dir: %s", err)
}
configFilePath := path.Join(configDir, ConfigFileName)
configFile, err := os.Create(configFilePath)
if err != nil {
return err
}
@ -45,8 +109,13 @@ func InitConfigFile() error {
}
func LoadFromTomlFile() error {
configFile, err := getConfigFile()
if err != nil {
return err
}
dest := make(Config)
_, err := toml.DecodeFile(ConfigFile, &dest)
_, err = toml.DecodeFile(configFile, &dest)
for k, val := range dest {

View File

@ -25,7 +25,6 @@ import (
"errors"
"fmt"
"os"
"os/user"
"path"
"path/filepath"
)
@ -89,11 +88,6 @@ func CheckWriteable(dir string) error {
return err
}
func GetHomeDir() string {
user, _ := user.Current()
return user.HomeDir
}
// ExpandPath expands a path with environment variables and tilde
// Symlinks are followed by default
func ExpandPath(paths ...string) (string, error) {