Skip to content

Commit

Permalink
feat(cmd): updated completion funcs in all subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
luisnquin committed Feb 7, 2023
1 parent d24274c commit 9eb1354
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 41 deletions.
14 changes: 6 additions & 8 deletions internal/cmd/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ type CatCmd struct {
func BuildCat(log *zerolog.Logger, data *data.Buffer) CatCmd {
c := CatCmd{
Command: &cobra.Command{
Use: "cat",
Short: "Displays the note in the standard output",
Args: cobra.MinimumNArgs(1),
SilenceErrors: true,
SilenceUsage: true,
ValidArgsFunction: func(_ *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return note.SearchKeyTagsByPrefix(toComplete, data), cobra.ShellCompDirectiveNoFileComp
},
Use: "cat",
Short: "Displays the note in the standard output",
Args: cobra.MinimumNArgs(1),
SilenceErrors: true,
SilenceUsage: true,
ValidArgsFunction: KeyTagCompletions(data),
},
data: data,
log: log,
Expand Down
14 changes: 6 additions & 8 deletions internal/cmd/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@ type ModCmd struct {
func BuildMod(log *zerolog.Logger, config *config.Core, data *data.Buffer) ModCmd {
c := ModCmd{
Command: &cobra.Command{
Use: "mod [<id> | <tag>]",
Short: "Edit any file",
Args: cobra.MaximumNArgs(1),
ValidArgsFunction: func(_ *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return note.SearchKeyTagsByPrefix(toComplete, data), cobra.ShellCompDirectiveNoFileComp
},
SilenceUsage: true,
SilenceErrors: true,
Use: "mod [<id> | <tag>]",
Short: "Edit any file",
Args: cobra.MaximumNArgs(1),
ValidArgsFunction: KeyTagCompletions(data),
SilenceUsage: true,
SilenceErrors: true,
},
config: config,
data: data,
Expand Down
11 changes: 6 additions & 5 deletions internal/cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ type NewCmd struct {
func BuildNew(log *zerolog.Logger, config *config.Core, data *data.Buffer) NewCmd {
c := NewCmd{
Command: &cobra.Command{
Use: "new",
Short: "Creates a new nao file",
Args: cobra.MaximumNArgs(1),
SilenceErrors: true,
SilenceUsage: true,
Use: "new",
Short: "Creates a new nao file",
Args: cobra.MaximumNArgs(1),
SilenceErrors: true,
SilenceUsage: true,
ValidArgsFunction: cobra.NoFileCompletions,
},
config: config,
data: data,
Expand Down
14 changes: 6 additions & 8 deletions internal/cmd/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ type RmCmd struct {
func BuildRm(log *zerolog.Logger, config *config.Core, data *data.Buffer) *RmCmd {
c := &RmCmd{
Command: &cobra.Command{
Use: "rm [<id> | <tag>]...",
Short: "Removes a file",
Args: cobra.MinimumNArgs(1),
SilenceUsage: true,
SilenceErrors: true,
ValidArgsFunction: func(_ *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return note.SearchKeyTagsByPrefix(toComplete, data), cobra.ShellCompDirectiveNoFileComp
},
Use: "rm [<id> | <tag>]...",
Short: "Removes a file",
Args: cobra.MinimumNArgs(1),
SilenceUsage: true,
SilenceErrors: true,
ValidArgsFunction: KeyTagCompletions(data),
},
config: config,
data: data,
Expand Down
14 changes: 6 additions & 8 deletions internal/cmd/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ type TagCmd struct {
func BuildTag(log *zerolog.Logger, config *config.Core, data *data.Buffer) TagCmd {
c := TagCmd{
Command: &cobra.Command{
Use: "tag <old> <new>",
Short: "Rename the tag of any file",
Args: cobra.ExactArgs(2),
SilenceUsage: true,
SilenceErrors: true,
ValidArgsFunction: func(_ *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return note.SearchKeyTagsByPrefix(toComplete, data), cobra.ShellCompDirectiveNoFileComp
},
Use: "tag <old> <new>",
Short: "Rename the tag of any file",
Args: cobra.ExactArgs(2),
SilenceUsage: true,
SilenceErrors: true,
ValidArgsFunction: KeyTagCompletions(data),
},
config: config,
data: data,
Expand Down
9 changes: 9 additions & 0 deletions internal/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (

"github.com/luisnquin/nao/v3/internal"
"github.com/luisnquin/nao/v3/internal/config"
"github.com/luisnquin/nao/v3/internal/data"
"github.com/luisnquin/nao/v3/internal/note"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -53,6 +56,12 @@ func NewFileCached(config *config.Core, key, content string) (string, error) {
return f.Name(), f.Close()
}

func KeyTagCompletions(data *data.Buffer) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return note.SearchKeyTagsByPrefix(toComplete, data), cobra.ShellCompDirectiveNoFileComp
}
}

func NavigateMapAndSet(m map[string]any, path string, value any) error {
parts := strings.Split(path, ".")

Expand Down
8 changes: 4 additions & 4 deletions internal/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ const tagsUrl = "https://api.github.com/repos/luisnquin/nao/tags"
func BuildVersion(log *zerolog.Logger, config *config.Core) VersionCmd {
c := VersionCmd{
Command: &cobra.Command{
Use: "version",
Short: "Print the nao version number",
Args: cobra.NoArgs,
PreRunE: nil,
Use: "version",
Short: "Print the nao version number",
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
},
config: config,
log: log,
Expand Down

0 comments on commit 9eb1354

Please sign in to comment.