Skip to content

Commit 25f7681

Browse files
authored
Fix precedence and interference issues in config file
Resolves the issue from #396: - The `klog config` subcommand now strictly only outputs values that are specified in the file. There is no interference anymore with environment variables (`$EDITOR` or `$NO_COLOR`). - The `editor` entry from the config file now takes precedence over the `$EDITOR` environment variable. - Note: the `$NO_COLOR` environment variable, in contrast, still does **not** take precedence over the `colour_scheme` entry from the config file. That is to disable output colouring on the fly, for programmatic invocations in scripted contexts.
1 parent 4469ffe commit 25f7681

File tree

6 files changed

+219
-298
lines changed

6 files changed

+219
-298
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ klog is a plain-text file format and a command line tool for time tracking.
2323
If you have questions, feedback, feature ideas, or want to report something that’s not working properly,
2424
feel invited to [start a discussion](https://github.com/jotaen/klog/discussions).
2525

26-
If you’d like to contribute code, please discuss your intended change beforehand. Please refrain from submitting unsolicited pull requests.
26+
If you’d like to contribute code, please discuss your intended change beforehand. Please refrain from submitting pull requests proactively.
2727

2828
## About
2929

klog.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package main
33
import (
44
_ "embed"
55
"fmt"
6+
"os"
7+
"runtime"
8+
69
"github.com/jotaen/klog/klog/app"
710
"github.com/jotaen/klog/klog/app/cli/util"
811
"github.com/jotaen/klog/klog/app/main"
9-
"os"
10-
"runtime"
1112
)
1213

1314
//go:embed Specification.md
@@ -41,11 +42,7 @@ func main() {
4142
}()
4243

4344
config := func() app.Config {
44-
c, err := app.NewConfig(
45-
app.FromDeterminedValues{NumCpus: runtime.NumCPU()},
46-
app.FromEnvVars{GetVar: os.Getenv},
47-
app.FromConfigFile{FileContents: configFile},
48-
)
45+
c, err := app.NewConfig(runtime.NumCPU(), os.Getenv, configFile)
4946
if err != nil {
5047
fail(util.PrettifyAppError(err, false), app.CONFIG_ERROR.ToInt())
5148
}

klog/app/cli/config.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package cli
22

33
import (
4+
"path/filepath"
5+
"strings"
6+
47
"github.com/jotaen/klog/klog/app"
58
tf "github.com/jotaen/klog/klog/app/cli/terminalformat"
69
"github.com/jotaen/klog/klog/app/cli/util"
7-
"path/filepath"
8-
"strings"
910
)
1011

1112
type Config struct {
@@ -27,7 +28,7 @@ klog relies on file-based configuration to customise some of its default behavio
2728
2829
Run 'klog config --location' to print the path of the folder where klog looks for the configuration.
2930
The config folder can contain one or both of the following files:
30-
- '` + app.CONFIG_FILE_NAME + `': you can create this file manually to override some of klog’s default behaviour. You may use the output of the 'klog config' command as template for setting up this file, as its output is in valid syntax.
31+
- '` + app.CONFIG_FILE_NAME + `': you can create this file manually to override some of klog’s default behaviour. You may use the output of the 'klog config' command as template for setting up this file, as its output is valid .ini syntax.
3132
- '` + app.BOOKMARKS_FILE_NAME + `': if you use the bookmarks functionality, then klog uses this file as database. You are not supposed to edit this file by hand! Instead, use the 'klog bookmarks' command to manage your bookmarks.
3233
3334
You can customise the location of the config folder via environment variables. klog uses the following lookup precedence:

klog/app/cli/testcontext_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package cli
22

33
import (
4+
gotime "time"
5+
46
"github.com/jotaen/klog/klog"
57
"github.com/jotaen/klog/klog/app"
68
"github.com/jotaen/klog/klog/app/cli/command"
79
tf "github.com/jotaen/klog/klog/app/cli/terminalformat"
810
"github.com/jotaen/klog/klog/parser"
911
"github.com/jotaen/klog/klog/parser/reconciling"
1012
"github.com/jotaen/klog/klog/parser/txt"
11-
gotime "time"
1213
)
1314

1415
func NewTestingContext() TestingContext {
@@ -63,11 +64,11 @@ func (ctx TestingContext) _SetFileExplorers(cs []command.Command) TestingContext
6364
}
6465

6566
func (ctx TestingContext) _SetFileConfig(configFile string) TestingContext {
66-
fileCfg := app.FromConfigFile{FileContents: configFile}
67-
err := fileCfg.Apply(ctx.config)
67+
cfg, err := app.NewConfig(1, func(_ string) string { return "" }, configFile)
6868
if err != nil {
6969
panic(err)
7070
}
71+
ctx.config = &cfg
7172
return ctx
7273
}
7374

0 commit comments

Comments
 (0)