From ad01d78765481f299b8b1869eda68697267493fc Mon Sep 17 00:00:00 2001 From: isthisnagee Date: Fri, 21 Jan 2022 23:23:23 -0800 Subject: [PATCH 1/3] Fix issues with db_path config arg Part of https://git.nagee.dev/isthisnagee/diary/issues/6 --- cmd/util.go | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/cmd/util.go b/cmd/util.go index 83262a2..7182910 100644 --- a/cmd/util.go +++ b/cmd/util.go @@ -39,24 +39,22 @@ func initConfig() Cfg { // Use config file from the flag. viper.SetConfigFile(cfgFile) } else { - - // Search config in home directory with name ".diary" (without extension). - viper.AddConfigPath(home) + viper.AddConfigPath(path.Join(home, ".config", "diary")) viper.SetConfigType("toml") - viper.SetConfigName(".diary") + viper.SetConfigName("diary.toml") } - var db_path = path.Join(home, ".diary.sql") - viper.SetDefault("db_path", db_path) + if viper.Get("db_path") == nil { + var db_path = path.Join(home, ".diary.sql") + viper.SetDefault("db_path", db_path) + } viper.AutomaticEnv() // read in environment variables that match - // If a config file is found, read it in. - if err := viper.ReadInConfig(); err == nil { - fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) - } + err = viper.ReadInConfig() + cobra.CheckErr(err) - return Cfg{db_path} + return Cfg{viper.GetString("db_path")} } func InitApp() { @@ -64,9 +62,7 @@ func InitApp() { if _, err := os.Stat(cfg.DbPath); errors.Is(err, os.ErrNotExist) { _, err := os.Create(cfg.DbPath) - if err != nil { - log.Fatal(err.Error()) - } + cobra.CheckErr(err) } var app = model.NewApp(cfg.DbPath) -- 2.36.3 From fe5f8a68a1ef7c61c8c617abe91360071c8b2b99 Mon Sep 17 00:00:00 2001 From: isthisnagee Date: Fri, 21 Jan 2022 23:24:23 -0800 Subject: [PATCH 2/3] Add documentation for db_path config Closes https://git.nagee.dev/isthisnagee/diary/issues/6 --- README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/README.md b/README.md index 30e5be2..6a3c9d7 100644 --- a/README.md +++ b/README.md @@ -6,3 +6,33 @@ ``` go install git.nagee.dev/isthisnagee/diary@v0.0.1 ``` + + +## Configuration + +### Valid locations for the config file + + +The config file can be passed to the `diary` command through a command line +arg. For example: `diary --config=~/.diary.toml` + +When there is no config argument, `diary` looks for the config here: + +- `$HOME/.config/diary/diary.toml` + + +### Configuration options + + +#### `db_path` + +- Name: `db_path` +- Type: String +- Default: `~/.diary.sql` +- Description: The location of the sqlite database. If the file does not exist, + it will be created. + +``` +db_path = "path/to/wherever/you/want/the/db/file" +``` + -- 2.36.3 From d7b53d74c59fd8ce18cc7f8afd9466f1b4d28aa1 Mon Sep 17 00:00:00 2001 From: isthisnagee Date: Fri, 21 Jan 2022 23:24:39 -0800 Subject: [PATCH 3/3] Return error from NewApp Floating up the error seems like a best practice. Part of https://git.nagee.dev/isthisnagee/diary/issues/7 --- cmd/util.go | 6 ++++-- model/entry.go | 6 +++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/cmd/util.go b/cmd/util.go index 7182910..e1789a7 100644 --- a/cmd/util.go +++ b/cmd/util.go @@ -65,8 +65,10 @@ func InitApp() { cobra.CheckErr(err) } - var app = model.NewApp(cfg.DbPath) - App = &TApp{&app, &cfg} + var app, err = model.NewApp(cfg.DbPath) + cobra.CheckErr(err) + + App = &TApp{app, &cfg} } var fmt_str = "%-10s %-20s %s\n" diff --git a/model/entry.go b/model/entry.go index 1265485..862eb37 100644 --- a/model/entry.go +++ b/model/entry.go @@ -20,12 +20,12 @@ type App struct { *db.DbCtx } -func NewApp(db_path string) App { +func NewApp(db_path string) (*App, error) { app, err := db.Init(db_path) if err != nil { - log.Fatal(err.Error()) + return nil, err } - return App{app} + return &App{app}, nil } -- 2.36.3