summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
Diffstat (limited to 'cmd')
-rw-r--r--cmd/krino/check.go3
-rw-r--r--cmd/krino/common.go24
-rw-r--r--cmd/krino/history_test.go36
-rw-r--r--cmd/krino/init.go3
-rw-r--r--cmd/krino/log.go3
-rw-r--r--cmd/krino/new.go3
-rw-r--r--cmd/krino/undo.go3
7 files changed, 75 insertions, 0 deletions
diff --git a/cmd/krino/check.go b/cmd/krino/check.go
index d4aee01..ec08672 100644
--- a/cmd/krino/check.go
+++ b/cmd/krino/check.go
@@ -22,6 +22,9 @@ func cmdCheck(g *globals, args []string, stdout, stderr io.Writer) int {
if g.minAgeSet {
return usageError(stderr, "--min-age applies only to sorting and explain")
}
+ if code, refused := refuseUnusedFlags(g, stderr, "check", false); refused {
+ return code
+ }
e, errs := engine.Load(mainFile(g), fs.Args()...)
if len(errs) > 0 {
printDiags(stderr, errs)
diff --git a/cmd/krino/common.go b/cmd/krino/common.go
index 15c32f6..5b6869a 100644
--- a/cmd/krino/common.go
+++ b/cmd/krino/common.go
@@ -33,6 +33,30 @@ func minAgeOverride(g *globals) (d time.Duration, set bool, err error) {
return d, true, nil
}
+// refuseUnusedFlags refuses the global flags a command does not use -
+// -y and -n unless usesYesDry, and --json and -v - instead of silently
+// ignoring them, so "krino -n new ...", meant as a preview, cannot write
+// config (re-review cli F4). It reports whether it refused.
+func refuseUnusedFlags(g *globals, stderr io.Writer, cmd string, usesYesDry bool) (int, bool) {
+ var given []string
+ if !usesYesDry && g.yes {
+ given = append(given, "-y")
+ }
+ if !usesYesDry && g.dry {
+ given = append(given, "-n")
+ }
+ if g.json {
+ given = append(given, "--json")
+ }
+ if g.verbose {
+ given = append(given, "-v")
+ }
+ if len(given) == 0 {
+ return 0, false
+ }
+ return usageError(stderr, fmt.Sprintf("%s does not take %s", cmd, strings.Join(given, ", "))), true
+}
+
// cacheDir is where every directory's keyword cache lives (spec ยง6.1).
func cacheDir() string {
return filepath.Join(xdg.CacheHome(), "krino")
diff --git a/cmd/krino/history_test.go b/cmd/krino/history_test.go
index fce38ad..6d8c1e4 100644
--- a/cmd/krino/history_test.go
+++ b/cmd/krino/history_test.go
@@ -516,3 +516,39 @@ func TestMinAgeRejectedOutsideSortAndExplain(t *testing.T) {
t.Errorf("-n --min-age 0: exit %d %s", code, errOut)
}
}
+
+// TestIgnoredGlobalFlagsAreRefused: a global flag a command does not use is
+// refused instead of silently ignored, so "krino -n new ..." or "krino -n
+// init" - meant as a preview - cannot write config (re-review cli F4).
+func TestIgnoredGlobalFlagsAreRefused(t *testing.T) {
+ h := home(t)
+ if code, _, errOut := runCLI(t, "-n", "init"); code != 2 || !strings.Contains(errOut, "-n") {
+ t.Errorf("-n init: exit %d %q", code, errOut)
+ }
+ if _, err := os.Stat(filepath.Join(h, ".config", "krino", "krino.conf")); !os.IsNotExist(err) {
+ t.Fatalf("-n init wrote the config: %v", err)
+ }
+ if code, _, errOut := runCLI(t, "init"); code != 0 {
+ t.Fatal(errOut)
+ }
+ dl := filepath.Join(h, "dl")
+ os.MkdirAll(dl, 0o755)
+ for _, args := range [][]string{
+ {"-n", "new", "dl", dl},
+ {"-y", "check"},
+ {"--json", "log"},
+ {"-v", "log"},
+ {"--json", "undo", "-n"},
+ {"-v", "undo", "-n"},
+ } {
+ if code, _, errOut := runCLI(t, args...); code != 2 || !strings.Contains(errOut, "does not take") {
+ t.Errorf("krino %q: exit %d, stderr %q; want 2, refused", args, code, errOut)
+ }
+ }
+ if _, err := os.Stat(filepath.Join(h, ".config", "krino", "dirs", "dl.conf")); !os.IsNotExist(err) {
+ t.Errorf("-n new wrote a directory file: %v", err)
+ }
+ if code, _, errOut := runCLI(t, "log", "-n", "3"); code != 0 {
+ t.Errorf("log -n 3 (its own count flag) was refused: %d %q", code, errOut)
+ }
+}
diff --git a/cmd/krino/init.go b/cmd/krino/init.go
index 1806ab9..6feb6d0 100644
--- a/cmd/krino/init.go
+++ b/cmd/krino/init.go
@@ -22,6 +22,9 @@ func cmdInit(g *globals, args []string, stdout, stderr io.Writer) int {
if g.minAgeSet {
return usageError(stderr, "--min-age applies only to sorting and explain")
}
+ if code, refused := refuseUnusedFlags(g, stderr, "init", false); refused {
+ return code
+ }
if fs.NArg() != 0 {
return usageError(stderr, "init takes no arguments")
}
diff --git a/cmd/krino/log.go b/cmd/krino/log.go
index 2394ec2..903f8e8 100644
--- a/cmd/krino/log.go
+++ b/cmd/krino/log.go
@@ -57,6 +57,9 @@ func cmdLog(g *globals, args []string, stdout, stderr io.Writer) int {
if g.minAgeSet {
return usageError(stderr, "--min-age applies only to sorting and explain")
}
+ if code, refused := refuseUnusedFlags(g, stderr, "log", false); refused {
+ return code
+ }
if fs.NArg() > 0 {
return usageError(stderr, "usage: krino log [-n N]")
}
diff --git a/cmd/krino/new.go b/cmd/krino/new.go
index ced9dc2..0390e82 100644
--- a/cmd/krino/new.go
+++ b/cmd/krino/new.go
@@ -21,6 +21,9 @@ func cmdNew(g *globals, args []string, stdout, stderr io.Writer) int {
if g.minAgeSet {
return usageError(stderr, "--min-age applies only to sorting and explain")
}
+ if code, refused := refuseUnusedFlags(g, stderr, "new", false); refused {
+ return code
+ }
if fs.NArg() != 2 {
return usageError(stderr, "usage: krino new NAME PATH")
}
diff --git a/cmd/krino/undo.go b/cmd/krino/undo.go
index 4cefc82..1b5e6ae 100644
--- a/cmd/krino/undo.go
+++ b/cmd/krino/undo.go
@@ -50,6 +50,9 @@ func cmdUndo(g *globals, args []string, stdout, stderr io.Writer) int {
if g.minAgeSet {
return usageError(stderr, "--min-age applies only to sorting and explain")
}
+ if code, refused := refuseUnusedFlags(g, stderr, "undo", true); refused {
+ return code
+ }
if g.yes && g.dry {
return usageError(stderr, "-y and -n cannot be used together")
}