Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ Check the man page nixos-cli-apply(1) for more details on what options are avail

cmdUtils.SetHelpFlagText(&cmd)
cmd.SetHelpTemplate(helpTemplate)
cmdUtils.SetUsageHideNixFlags(&cmd)

return &cmd
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,12 @@ Arguments:
}
helpTemplate += `
This command also forwards Nix options passed here to all relevant Nix invocations.
Check the Nix manual page for more details on what options are available.
Check the man page nixos-cli-install(1) for more details on what options are available.
`

cmd.SetHelpTemplate(helpTemplate)
cmdUtils.SetHelpFlagText(&cmd)
cmdUtils.SetUsageHideNixFlags(&cmd)

return &cmd
}
Expand Down
12 changes: 7 additions & 5 deletions internal/cmd/nixopts/nixopts.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import (
"github.com/spf13/pflag"
)

const NixFlagAnnotation = "nixos_cli_nix_flag"

func addNixOptionBool(cmd *cobra.Command, dest *bool, name string, shorthand string, desc string) {
flag := string(name)
if shorthand != "" {
cmd.Flags().BoolVarP(dest, flag, shorthand, false, desc)
} else {
cmd.Flags().BoolVar(dest, flag, false, desc)
}
cmd.Flags().Lookup(flag).Hidden = true
_ = cmd.Flags().SetAnnotation(flag, NixFlagAnnotation, nil)
}

// func addNixOptionInt(cmd *cobra.Command, dest *int, name string, shorthand string, desc string) {
Expand All @@ -36,7 +38,7 @@ func addNixOptionString(cmd *cobra.Command, dest *string, name string, shorthand
} else {
cmd.Flags().StringVar(dest, flag, "", desc)
}
cmd.Flags().Lookup(flag).Hidden = true
_ = cmd.Flags().SetAnnotation(flag, NixFlagAnnotation, nil)
}

func addNixOptionStringSlice(cmd *cobra.Command, dest *[]string, name string, shorthand string, desc string) {
Expand All @@ -46,7 +48,7 @@ func addNixOptionStringSlice(cmd *cobra.Command, dest *[]string, name string, sh
} else {
cmd.Flags().StringSliceVar(dest, flag, nil, desc)
}
cmd.Flags().Lookup(flag).Hidden = true
_ = cmd.Flags().SetAnnotation(flag, NixFlagAnnotation, nil)
}

func addNixOptionStringMap(cmd *cobra.Command, dest *map[string]string, name string, shorthand string, desc string) {
Expand All @@ -56,7 +58,7 @@ func addNixOptionStringMap(cmd *cobra.Command, dest *map[string]string, name str
} else {
cmd.Flags().StringToStringVar(dest, flag, nil, desc)
}
cmd.Flags().Lookup(flag).Hidden = true
_ = cmd.Flags().SetAnnotation(flag, NixFlagAnnotation, nil)
}

func addNixOptionVar(cmd *cobra.Command, dest pflag.Value, name string, shorthand string, desc string) {
Expand All @@ -66,7 +68,7 @@ func addNixOptionVar(cmd *cobra.Command, dest pflag.Value, name string, shorthan
} else {
cmd.Flags().Var(dest, flag, desc)
}
cmd.Flags().Lookup(flag).Hidden = true
_ = cmd.Flags().SetAnnotation(flag, NixFlagAnnotation, nil)
}

type NixCommand string
Expand Down
20 changes: 20 additions & 0 deletions internal/cmd/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,33 @@ import (
"sort"

tea "github.com/charmbracelet/bubbletea"
"github.com/nix-community/nixos-cli/internal/cmd/nixopts"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

func SetHelpFlagText(cmd *cobra.Command) {
cmd.Flags().BoolP("help", "h", false, "Show this help menu")
}

// Set a usage function that hides any nix flags before returning
// the default usage function. This is needed as hiding the flags
// outside of the usage function also hides them from completion
// output.
func SetUsageHideNixFlags(cmd *cobra.Command) {
defaultUsageFunc := cmd.UsageFunc()

cmd.SetUsageFunc(func(cmd *cobra.Command) error {
cmd.LocalFlags().VisitAll(func(f *pflag.Flag) {
if _, ok := f.Annotations[nixopts.NixFlagAnnotation]; ok {
f.Hidden = true
}
})

return defaultUsageFunc(cmd)
})
}

var ErrCommand = errors.New("command error")

// Replace a returned error with the generic `ErrCommand`, and.
Expand Down
Loading