Skip to content

Commit 7f601d6

Browse files
committed
feat: custom config file path
1 parent aab67fe commit 7f601d6

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

lib/xapp/cli.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type CLIOptions struct {
1515
Verbose bool `arg:"-V,--verbose" help:"when set, output more details to help developers"`
1616
SizeLimit *int64 `arg:"-s,--size-limit" help:"in bytes. overwrite default size limit (5MiB). 0 means no limit"`
1717
Wait bool `arg:"-w,--wait" help:"when set, not exit after upload, util user press any key"`
18+
ConfigFile string `arg:"-c,--config-file" help:"when set, will use specific config file"`
1819
Clean bool `arg:"-C,--clean" help:"when set, remove local file after upload"`
1920
Raw bool `arg:"-r,--raw" help:"when set, output non-replaced raw url"`
2021
NoLog bool `arg:"-n,--no-log" help:"when set, disable logging"`

main.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,21 +184,29 @@ func validArgs() {
184184
}
185185
}
186186

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

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

192193
appDir := xpath.MustGetApplicationPath("")
193194

194-
var configFiles = []string{
195-
filepath.Join(homeDir, ".upgit.config.toml"),
196-
filepath.Join(homeDir, ".upgit.toml"),
197-
filepath.Join(appDir, "config.toml"),
195+
var configFiles = map[string]bool{
196+
filepath.Join(homeDir, ".upgit.config.toml"): false,
197+
filepath.Join(homeDir, ".upgit.toml"): false,
198+
filepath.Join(appDir, "config.toml"): false,
198199
}
199200

200-
for _, configFile := range configFiles {
201+
if xapp.AppOpt.ConfigFile != "" {
202+
configFiles[xapp.AppOpt.ConfigFile] = true
203+
}
204+
205+
for configFile, required := range configFiles {
201206
if _, err := os.Stat(configFile); err != nil {
207+
if required {
208+
xlog.AbortErr(fmt.Errorf("config file %s not found", configFile))
209+
}
202210
continue
203211
}
204212
optRawBytes, err := ioutil.ReadFile(configFile)
@@ -212,6 +220,10 @@ func loadConfig(cfg *xapp.Config) {
212220
break
213221
}
214222

223+
if xapp.ConfigFilePath == "" {
224+
xlog.AbortErr(fmt.Errorf("no config file found"))
225+
}
226+
215227
// fill config
216228
xapp.AppCfg.Rename = strings.Trim(xapp.AppCfg.Rename, "/")
217229
xapp.AppCfg.Rename = xstrings.RemoveFmtUnderscore(xapp.AppCfg.Rename)

0 commit comments

Comments
 (0)