Add --group option to `zk new` to override the config group

pull/6/head
Mickaël Menu 3 years ago
parent 7a1ba21a64
commit 5968547381
No known key found for this signature in database
GPG Key ID: 53D73664CD359895

@ -14,6 +14,7 @@ type New struct {
Directory string `arg optional type:"path" default:"." help:"Directory in which to create the note."`
Title string `short:t placeholder:TITLE help:"Title of the new note."`
Group string `short:g placeholder:NAME help:"Name of the config group this note belongs to. Takes precedence over the config of the directory."`
Extra map[string]string ` help:"Extra variables passed to the templates."`
Template string `type:path placeholder:PATH help:"Custom template used to render the note."`
PrintPath bool `short:p help:"Print the path of the created note instead of editing it."`
@ -21,6 +22,7 @@ type New struct {
func (cmd *New) ConfigOverrides() zk.ConfigOverrides {
return zk.ConfigOverrides{
Group: opt.NewNotEmptyString(cmd.Group),
BodyTemplatePath: opt.NewNotEmptyString(cmd.Template),
Extra: cmd.Extra,
}

@ -59,6 +59,7 @@ type GroupConfig struct {
// ConfigOverrides holds user configuration overriden values, for example fed
// from CLI flags.
type ConfigOverrides struct {
Group opt.String
BodyTemplatePath opt.String
Extra map[string]string
}

@ -292,7 +292,12 @@ func (zk *Zk) DirAt(path string, overrides ...ConfigOverrides) (*Dir, error) {
return nil, err
}
config := zk.findConfigForDirNamed(name).Clone()
config, err := zk.findConfigForDirNamed(name, overrides)
if err != nil {
return nil, err
}
config = config.Clone()
for _, v := range overrides {
config.Override(v)
}
@ -304,16 +309,31 @@ func (zk *Zk) DirAt(path string, overrides ...ConfigOverrides) (*Dir, error) {
}, nil
}
func (zk *Zk) findConfigForDirNamed(name string) GroupConfig {
func (zk *Zk) findConfigForDirNamed(name string, overrides []ConfigOverrides) (GroupConfig, error) {
// If there's a Group overrides, attempt to find a matching group.
overridenGroup := ""
for _, o := range overrides {
if !o.Group.IsNull() {
overridenGroup = o.Group.Unwrap()
if group, ok := zk.Config.Groups[overridenGroup]; ok {
return group, nil
}
}
}
if overridenGroup != "" {
return GroupConfig{}, fmt.Errorf("%s: group not find in the config file", overridenGroup)
}
for _, group := range zk.Config.Groups {
for _, path := range group.Paths {
if path == name {
return group
return group, nil
}
}
}
// Fallback on root config.
return zk.Config.RootGroupConfig()
return zk.Config.RootGroupConfig(), nil
}
// RequiredDirAt is the same as DirAt, but checks that the directory exists

@ -228,6 +228,15 @@ func TestDirAtWithOverrides(t *testing.T) {
Extra: map[string]string{
"hello": "world",
},
Groups: map[string]GroupConfig{
"group": {
Paths: []string{"group-path"},
Note: NoteConfig{
BodyTemplatePath: opt.NewString("group.note"),
},
Extra: map[string]string{},
},
},
},
}
@ -266,6 +275,21 @@ func TestDirAtWithOverrides(t *testing.T) {
},
})
// Overriding the group will select a different group config.
dir, err = zk.DirAt(".", ConfigOverrides{Group: opt.NewString("group")})
assert.Nil(t, err)
assert.Equal(t, dir.Config, GroupConfig{
Paths: []string{"group-path"},
Note: NoteConfig{
BodyTemplatePath: opt.NewString("group.note"),
},
Extra: map[string]string{},
})
// An unknown group override returns an error.
_, err = zk.DirAt(".", ConfigOverrides{Group: opt.NewString("foobar")})
assert.Err(t, err, "foobar: group not find in the config file")
// Check that the original config was not modified.
assert.Equal(t, zk.Config.RootGroupConfig(), GroupConfig{
Paths: []string{},

Loading…
Cancel
Save