-
-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add -config flag #18
Add -config flag #18
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,9 +6,13 @@ import ( | |
"os" | ||
) | ||
|
||
func getConfigPath() string { | ||
dirname, _ := os.UserHomeDir() | ||
return dirname + "/.mdrss/config.json" | ||
func getConfigPath(config_path string) string { | ||
if FileExists(config_path) { | ||
return config_path | ||
} else { | ||
dirname, _ := os.UserHomeDir() | ||
return dirname + "/.mdrss/config.json" | ||
} | ||
} | ||
|
||
func FileExists(filename string) bool { | ||
|
@@ -18,9 +22,9 @@ func FileExists(filename string) bool { | |
return true | ||
} | ||
|
||
func ReadConfig() (Config, error) { | ||
func ReadConfig(config_path string) (Config, error) { | ||
var config Config | ||
configPath := getConfigPath() | ||
configPath := getConfigPath(config_path) | ||
Comment on lines
+25
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In Golang I use camelCase instead of underscores for variables (as is mentioned in Google's style guide). |
||
if FileExists(configPath) { | ||
configContent, _ := os.ReadFile(configPath) | ||
jsonErr := json.Unmarshal(configContent, &config) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package main | |
|
||
import ( | ||
mdrss "github.com/TimoKats/mdrss/lib" | ||
"flag" | ||
"reflect" | ||
"errors" | ||
"os" | ||
|
@@ -58,16 +59,27 @@ func parseCommand(command string, config mdrss.Config) error { | |
} | ||
|
||
func main() { | ||
if len(os.Args) != 2 { | ||
mdrss.Error.Println("mdrss <<update, ls, conf >>") | ||
return | ||
var config_path string | ||
flag.StringVar(&config_path, "config", "~/.mdrss/config.json", "path to config.json") | ||
flag.Parse() | ||
|
||
var cmd string | ||
switch len(os.Args) { | ||
case 2: | ||
cmd = os.Args[1] | ||
case 4: | ||
cmd = os.Args[3] | ||
Comment on lines
+68
to
+71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is something I'm not a huge fan of; because it's guessing whether the config flag comes before or after the command. |
||
default: | ||
mdrss.Error.Println("mdrss <<update, ls, conf >>") | ||
return | ||
} | ||
config, configErr := mdrss.ReadConfig() | ||
|
||
config, configErr := mdrss.ReadConfig(config_path) | ||
if configErr != nil { | ||
mdrss.Error.Println(configErr) | ||
return | ||
} | ||
commandErr := parseCommand(os.Args[1], config) | ||
commandErr := parseCommand(cmd, config) | ||
if commandErr != nil { | ||
mdrss.Error.Println(commandErr) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I check for the existence of the config path later in the code. Here I just select / construct the config path that I want to use. Hence. the first if statement should only check whether the config path from the flag is enabled.
(PS: it should not be called configPath yet, because we don't know whether it is even used at this point. It's really the config path flag).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd also add that XDG should be used. So this should be
~/.config/mdrss/config.json
.Instead of:
you can use adrg/xdg: