Skip to content

Commit

Permalink
feat: custom config file path
Browse files Browse the repository at this point in the history
  • Loading branch information
pluveto committed Apr 13, 2022
1 parent aab67fe commit 7f601d6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/xapp/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type CLIOptions struct {
Verbose bool `arg:"-V,--verbose" help:"when set, output more details to help developers"`
SizeLimit *int64 `arg:"-s,--size-limit" help:"in bytes. overwrite default size limit (5MiB). 0 means no limit"`
Wait bool `arg:"-w,--wait" help:"when set, not exit after upload, util user press any key"`
ConfigFile string `arg:"-c,--config-file" help:"when set, will use specific config file"`
Clean bool `arg:"-C,--clean" help:"when set, remove local file after upload"`
Raw bool `arg:"-r,--raw" help:"when set, output non-replaced raw url"`
NoLog bool `arg:"-n,--no-log" help:"when set, disable logging"`
Expand Down
22 changes: 17 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,21 +184,29 @@ func validArgs() {
}
}

// loadConfig loads config from config file to xapp.AppCfg
func loadConfig(cfg *xapp.Config) {

homeDir, err := os.UserHomeDir()
xlog.AbortErr(err)

appDir := xpath.MustGetApplicationPath("")

var configFiles = []string{
filepath.Join(homeDir, ".upgit.config.toml"),
filepath.Join(homeDir, ".upgit.toml"),
filepath.Join(appDir, "config.toml"),
var configFiles = map[string]bool{
filepath.Join(homeDir, ".upgit.config.toml"): false,
filepath.Join(homeDir, ".upgit.toml"): false,
filepath.Join(appDir, "config.toml"): false,
}

for _, configFile := range configFiles {
if xapp.AppOpt.ConfigFile != "" {
configFiles[xapp.AppOpt.ConfigFile] = true
}

for configFile, required := range configFiles {
if _, err := os.Stat(configFile); err != nil {
if required {
xlog.AbortErr(fmt.Errorf("config file %s not found", configFile))
}
continue
}
optRawBytes, err := ioutil.ReadFile(configFile)
Expand All @@ -212,6 +220,10 @@ func loadConfig(cfg *xapp.Config) {
break
}

if xapp.ConfigFilePath == "" {
xlog.AbortErr(fmt.Errorf("no config file found"))
}

// fill config
xapp.AppCfg.Rename = strings.Trim(xapp.AppCfg.Rename, "/")
xapp.AppCfg.Rename = xstrings.RemoveFmtUnderscore(xapp.AppCfg.Rename)
Expand Down

0 comments on commit 7f601d6

Please sign in to comment.