From 5843bd8443d27535d6c72f01c48fdf18a5697db9 Mon Sep 17 00:00:00 2001 From: Gustavo Miranda Date: Tue, 20 Oct 2026 23:54:30 -0300 Subject: [PATCH] fix(cli): reject arguments commands do not take Fifteen commands ran with any arguments and ignored them, so a mistyped subcommand such as "devlog sync schedule now" installed a daily sync instead of failing. Every runnable command now declares its arguments, and a test walks the command tree so new commands cannot skip it. --- cmd/entry/list.go | 1 + cmd/error_messages_test.go | 27 +++++++++++++++++++++++++++ cmd/migrate.go | 1 + cmd/remote.go | 3 +++ cmd/summary/create.go | 1 + cmd/summary/list.go | 1 + cmd/summary/show.go | 1 + cmd/sync.go | 1 + cmd/sync_schedule.go | 3 +++ cmd/update.go | 1 + cmd/version.go | 1 + 11 files changed, 41 insertions(+) diff --git a/cmd/entry/list.go b/cmd/entry/list.go index 320ff70..abb3e13 100644 --- a/cmd/entry/list.go +++ b/cmd/entry/list.go @@ -24,6 +24,7 @@ func NewListCmd() *cobra.Command { listCmd := &cobra.Command{ Use: "list", Short: "Display logged entries with optional filters", + Args: cobra.NoArgs, Long: `Displays logged entries with optional filters. Options: diff --git a/cmd/error_messages_test.go b/cmd/error_messages_test.go index 793f35b..ccceb05 100644 --- a/cmd/error_messages_test.go +++ b/cmd/error_messages_test.go @@ -4,6 +4,8 @@ import ( "context" "strings" "testing" + + "github.com/spf13/cobra" ) func TestRootDoesNotPrintUsageForRuntimeErrors(t *testing.T) { @@ -37,3 +39,28 @@ func TestInvalidConfigBooleanIncludesValueAndExpectedFormat(t *testing.T) { t.Fatalf("error = %q, want %q", got, want) } } + +// TestRunnableCommandsValidateArguments guards against commands that ignore +// stray arguments: "devlog sync schedule now" used to install a job instead of +// reporting that "now" is not a subcommand. +func TestRunnableCommandsValidateArguments(t *testing.T) { + var walk func(*cobra.Command) + walk = func(command *cobra.Command) { + if command.Runnable() && command.Args == nil { + t.Errorf("%s accepts any arguments; set Args, such as cobra.NoArgs", command.CommandPath()) + } + for _, child := range command.Commands() { + walk(child) + } + } + walk(RootCmd) +} + +func TestUnknownSubcommandIsRejected(t *testing.T) { + RootCmd.SetArgs([]string{"sync", "schedule", "now"}) + t.Cleanup(func() { RootCmd.SetArgs(nil) }) + err := RootCmd.Execute() + if err == nil || !strings.Contains(err.Error(), `unknown command "now"`) { + t.Fatalf("error = %v, want an unknown command error", err) + } +} diff --git a/cmd/migrate.go b/cmd/migrate.go index a3a0a67..80c1989 100644 --- a/cmd/migrate.go +++ b/cmd/migrate.go @@ -11,6 +11,7 @@ import ( var migrateCmd = &cobra.Command{ Use: "migrate", Short: "Migrate legacy daily logs to the current data format", + Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { _, result, err := store.MigrateRepository() if err != nil { diff --git a/cmd/remote.go b/cmd/remote.go index 5c965fc..a5c6de4 100644 --- a/cmd/remote.go +++ b/cmd/remote.go @@ -46,6 +46,7 @@ var remoteInitCmd = &cobra.Command{ var remoteStatusCmd = &cobra.Command{ Use: "status", Short: "Show remote synchronization status", + Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { repo, err := store.OpenRepository() if err != nil { @@ -70,6 +71,7 @@ var remoteStatusCmd = &cobra.Command{ var remoteSyncCmd = &cobra.Command{ Use: "sync", Short: "Pull, merge, and push DevLog data", + Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { repo, err := store.OpenRepository() if err != nil { @@ -86,6 +88,7 @@ var remoteSyncCmd = &cobra.Command{ var remoteRemoveCmd = &cobra.Command{ Use: "remove", Short: "Disconnect the remote without deleting local data", + Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { repo, err := store.OpenRepository() if err != nil { diff --git a/cmd/summary/create.go b/cmd/summary/create.go index a3933e3..b377392 100644 --- a/cmd/summary/create.go +++ b/cmd/summary/create.go @@ -20,6 +20,7 @@ import ( var createCmd = &cobra.Command{ Use: "create", Short: "Generate a structured summary from logged entries", + Args: cobra.NoArgs, Long: `Generates a structured summary from logged entries and saves it to ~/.devlog/summaries/. Options: diff --git a/cmd/summary/list.go b/cmd/summary/list.go index 9bbb903..c866ee1 100644 --- a/cmd/summary/list.go +++ b/cmd/summary/list.go @@ -17,6 +17,7 @@ import ( var listCmd = &cobra.Command{ Use: "list", Short: "List previously generated summaries", + Args: cobra.NoArgs, Long: `Lists previously generated summaries from ~/.devlog/summaries/. With no flags, summaries from the current week (Monday–Sunday) are shown. diff --git a/cmd/summary/show.go b/cmd/summary/show.go index 83c8e66..30e2ddf 100644 --- a/cmd/summary/show.go +++ b/cmd/summary/show.go @@ -22,6 +22,7 @@ var ( var showCmd = &cobra.Command{ Use: "show", Short: "Display the contents of a previously generated summary", + Args: cobra.NoArgs, Long: `Displays the contents of a previously generated summary from ~/.devlog/summaries/. Options: diff --git a/cmd/sync.go b/cmd/sync.go index 8ae3cdf..9497014 100644 --- a/cmd/sync.go +++ b/cmd/sync.go @@ -20,6 +20,7 @@ import ( var syncCmd = &cobra.Command{ Use: "sync", Short: "Import GitHub activity (commits, PRs, reviews) as entries", + Args: cobra.NoArgs, Long: `Collects your GitHub activity for a date and adds it to that day's log. It is the same as devlog collect --sources github, with --polish to rewrite new descriptions with the configured LLM: diff --git a/cmd/sync_schedule.go b/cmd/sync_schedule.go index f278a47..31af19e 100644 --- a/cmd/sync_schedule.go +++ b/cmd/sync_schedule.go @@ -16,6 +16,7 @@ import ( var syncScheduleCmd = &cobra.Command{ Use: "schedule", Short: "Install a daily automatic sync", + Args: cobra.NoArgs, Long: `Install a daily sync using launchd on macOS, the user's crontab on Linux, or Task Scheduler on Windows. @@ -58,6 +59,7 @@ its owner (for example, chmod 600 ~/.devlog/sync.env).`, var syncScheduleShowCmd = &cobra.Command{ Use: "show", Short: "Show the installed automatic sync definition", + Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { definition, installed, err := scheduler.Show("sync") if err != nil { @@ -75,6 +77,7 @@ var syncScheduleShowCmd = &cobra.Command{ var syncScheduleRemoveCmd = &cobra.Command{ Use: "remove", Short: "Remove the installed automatic sync", + Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { removed, err := scheduler.Remove("sync") if err != nil { diff --git a/cmd/update.go b/cmd/update.go index ec56038..38a5d38 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -54,6 +54,7 @@ func confirm(prompt string) bool { var updateCmd = &cobra.Command{ Use: "update", Short: "Update devlog to the latest release", + Args: cobra.NoArgs, Long: `Checks GitHub for a newer release and, if found, downloads it, verifies its checksum, and replaces the running binary in place. diff --git a/cmd/version.go b/cmd/version.go index d11e610..f0d5eee 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -44,6 +44,7 @@ func versionString() string { var versionCmd = &cobra.Command{ Use: "version", Short: "Print the devlog version, commit, and build date", + Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { fmt.Print(versionString()) if checkUpdate {