Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions lib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Comment on lines +9 to +15
Copy link
Owner

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).

Copy link
Contributor Author

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:

dirname, _ := os.UserHomeDir()
return dirname + "/.mdrss/config.json"

you can use adrg/xdg:

configFilePath, err := xdg.ConfigFile("mdrss/config.json")
if err != nil {
 log.Fatal(err)
}
return configFilePath

}

func FileExists(filename string) bool {
Expand All @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The 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).

https://google.github.io/styleguide/go/guide#mixed-caps

if FileExists(configPath) {
configContent, _ := os.ReadFile(configPath)
jsonErr := json.Unmarshal(configContent, &config)
Expand Down
22 changes: 17 additions & 5 deletions mdrss.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
mdrss "github.com/TimoKats/mdrss/lib"
"flag"
"reflect"
"errors"
"os"
Expand Down Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The 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)
}
Expand Down
Loading