Skip to content
This repository was archived by the owner on Jan 2, 2026. It is now read-only.

Commit 6a405bf

Browse files
committed
Sets DEBUG env var when -d/--debug is passed
1 parent ac84bc7 commit 6a405bf

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

cli/cli.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,5 +296,12 @@ func execCommand(cmd *cobra.Command, args []string) error {
296296
}
297297

298298
func makeEnv(cmd *cobra.Command) []string {
299-
return append(os.Environ(), fmt.Sprintf("SD_ALIAS=%s", cmd.Root().Use))
299+
out := os.Environ()
300+
out = append(out, fmt.Sprintf("SD_ALIAS=%s", cmd.Root().Use))
301+
302+
if debug, _ := cmd.Root().PersistentFlags().GetBool("debug"); debug {
303+
out = append(out, "DEBUG=true")
304+
}
305+
306+
return out
300307
}

cli/cli_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,3 +412,38 @@ func TestRunCompletionsZsh(t *testing.T) {
412412
assert.NoError(t, err)
413413
assert.Contains(t, out, "#compdef sd")
414414
}
415+
416+
func TestMakeEnv(t *testing.T) {
417+
t.Run("sets SD_ALIAS", func(t *testing.T) {
418+
t.Run("when not aliased", func(t *testing.T) {
419+
sd := New("1.0").(*sd)
420+
env := makeEnv(sd.root)
421+
assert.Equal(t, "SD_ALIAS=sd", env[len(env)-1])
422+
})
423+
424+
t.Run("when aliased", func(t *testing.T) {
425+
var restore []string
426+
copy(restore, os.Args)
427+
defer func() {
428+
copy(os.Args, restore)
429+
}()
430+
431+
os.Args = []string{"-a", "foo"}
432+
433+
sd := New("1.0").(*sd)
434+
env := makeEnv(sd.root)
435+
assert.Equal(t, "SD_ALIAS=foo", env[len(env)-1])
436+
})
437+
})
438+
439+
t.Run("sets DEBUG", func(t *testing.T) {
440+
root := &cobra.Command{}
441+
root.PersistentFlags().Bool("debug", false, "Toggle debugging")
442+
root.PersistentFlags().Set("debug", "true")
443+
child := &cobra.Command{}
444+
root.AddCommand(child)
445+
446+
env := makeEnv(child)
447+
assert.Equal(t, "DEBUG=true", env[len(env)-1])
448+
})
449+
}

0 commit comments

Comments
 (0)