Skip to content

Commit 70ed7d1

Browse files
committed
feat: config package revamp
1 parent 34cdbab commit 70ed7d1

File tree

15 files changed

+607
-187
lines changed

15 files changed

+607
-187
lines changed

cli/cmdx/cmdx.go

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package cmdx
22

33
import (
4-
"strings"
5-
64
"github.com/spf13/cobra"
5+
"strings"
76
)
87

9-
// Manager manages and configures features for a CLI tool.
10-
type Manager struct {
8+
// Commander manages and configures features for a CLI tool.
9+
type Commander struct {
1110
RootCmd *cobra.Command
1211
Help bool // Enable custom help.
1312
Reference bool // Enable reference command.
@@ -32,19 +31,19 @@ type HookBehavior struct {
3231
Behavior func(cmd *cobra.Command) // Function to apply to commands.
3332
}
3433

35-
// NewManager creates a new CLI Manager using the provided root command and optional configurations.
34+
// NewCommander creates a new CLI Commander using the provided root command and optional configurations.
3635
//
3736
// Parameters:
3837
// - rootCmd: The root Cobra command for the CLI.
39-
// - options: Functional options for configuring the Manager.
38+
// - options: Functional options for configuring the Commander.
4039
//
4140
// Example:
4241
//
4342
// rootCmd := &cobra.Command{Use: "mycli"}
44-
// manager := cmdx.NewManager(rootCmd, cmdx.WithTopics(...), cmdx.WithHooks(...))
45-
func NewManager(rootCmd *cobra.Command, options ...func(*Manager)) *Manager {
46-
// Create Manager with defaults
47-
manager := &Manager{
43+
// manager := cmdx.NewCommander(rootCmd, cmdx.WithTopics(...), cmdx.WithHooks(...))
44+
func NewCommander(rootCmd *cobra.Command, options ...func(*Commander)) *Commander {
45+
// Create Commander with defaults
46+
manager := &Commander{
4847
RootCmd: rootCmd,
4948
Help: true, // Default enabled
5049
Reference: true, // Default enabled
@@ -62,11 +61,11 @@ func NewManager(rootCmd *cobra.Command, options ...func(*Manager)) *Manager {
6261
return manager
6362
}
6463

65-
// Init sets up the CLI features based on the Manager's configuration.
64+
// Init sets up the CLI features based on the Commander's configuration.
6665
//
6766
// It enables or disables features like custom help, reference documentation,
68-
// shell completion, help topics, and client hooks based on the Manager's settings.
69-
func (m *Manager) Init() {
67+
// shell completion, help topics, and client hooks based on the Commander's settings.
68+
func (m *Commander) Init() {
7069
if m.Help {
7170
m.SetCustomHelp()
7271
}
@@ -88,16 +87,16 @@ func (m *Manager) Init() {
8887
}
8988
}
9089

91-
// WithTopics sets the help topics for the Manager.
92-
func WithTopics(topics []HelpTopic) func(*Manager) {
93-
return func(m *Manager) {
90+
// WithTopics sets the help topics for the Commander.
91+
func WithTopics(topics []HelpTopic) func(*Commander) {
92+
return func(m *Commander) {
9493
m.Topics = topics
9594
}
9695
}
9796

98-
// WithHooks sets the hook behaviors for the Manager.
99-
func WithHooks(hooks []HookBehavior) func(*Manager) {
100-
return func(m *Manager) {
97+
// WithHooks sets the hook behaviors for the Commander.
98+
func WithHooks(hooks []HookBehavior) func(*Commander) {
99+
return func(m *Commander) {
101100
m.Hooks = hooks
102101
}
103102
}

cli/cmdx/completion.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ import (
1414
//
1515
// Example:
1616
//
17-
// manager := cmdx.NewManager(rootCmd)
17+
// manager := cmdx.NewCommander(rootCmd)
1818
// manager.AddCompletionCommand()
1919
//
2020
// Usage:
2121
//
2222
// $ mycli completion bash
2323
// $ mycli completion zsh
24-
func (m *Manager) AddCompletionCommand() {
24+
func (m *Commander) AddCompletionCommand() {
2525
summary := m.generateCompletionSummary(m.RootCmd.Use)
2626

2727
completionCmd := &cobra.Command{
@@ -38,7 +38,7 @@ func (m *Manager) AddCompletionCommand() {
3838
}
3939

4040
// runCompletionCommand executes the appropriate shell completion generation logic.
41-
func (m *Manager) runCompletionCommand(cmd *cobra.Command, args []string) {
41+
func (m *Commander) runCompletionCommand(cmd *cobra.Command, args []string) {
4242
switch args[0] {
4343
case "bash":
4444
cmd.Root().GenBashCompletion(os.Stdout)
@@ -52,7 +52,7 @@ func (m *Manager) runCompletionCommand(cmd *cobra.Command, args []string) {
5252
}
5353

5454
// generateCompletionSummary creates the long description for the `completion` command.
55-
func (m *Manager) generateCompletionSummary(exec string) string {
55+
func (m *Commander) generateCompletionSummary(exec string) string {
5656
var execs []interface{}
5757
for i := 0; i < 12; i++ {
5858
execs = append(execs, exec)

cli/cmdx/doc.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@
5959
// },
6060
// }
6161
//
62-
// // Create the Manager with configurations
63-
// manager := cmdx.NewManager(
62+
// // Create the Commander with configurations
63+
// manager := cmdx.NewCommander(
6464
// rootCmd,
6565
// cmdx.WithTopics(helpTopics),
6666
// cmdx.WithHooks(hooks),

cli/cmdx/help.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ import (
1111

1212
// Section Titles for Help Output
1313
const (
14-
USAGE = "Usage"
15-
CORECMD = "Core commands"
16-
OTHERCMD = "Other commands"
17-
HELPCMD = "Help topics"
18-
FLAGS = "Flags"
19-
IFLAGS = "Inherited flags"
20-
ARGUMENTS = "Arguments"
21-
EXAMPLES = "Examples"
22-
ENVS = "Environment variables"
23-
LEARN = "Learn more"
24-
FEEDBACK = "Feedback"
14+
usage = "Usage"
15+
corecmd = "Core commands"
16+
othercmd = "Other commands"
17+
helpcmd = "Help topics"
18+
flags = "Flags"
19+
iflags = "Inherited flags"
20+
arguments = "Arguments"
21+
examples = "Examples"
22+
envs = "Environment variables"
23+
learn = "Learn more"
24+
feedback = "Feedback"
2525
)
2626

2727
// SetCustomHelp configures a custom help function for the CLI.
@@ -31,9 +31,9 @@ const (
3131
//
3232
// Example:
3333
//
34-
// manager := cmdx.NewManager(rootCmd)
34+
// manager := cmdx.NewCommander(rootCmd)
3535
// manager.SetCustomHelp()
36-
func (m *Manager) SetCustomHelp() {
36+
func (m *Commander) SetCustomHelp() {
3737
m.RootCmd.PersistentFlags().Bool("help", false, "Show help for command")
3838

3939
m.RootCmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
@@ -120,39 +120,39 @@ func buildHelpEntries(cmd *cobra.Command) []helpEntry {
120120
helpEntries = append(helpEntries, helpEntry{"", text})
121121
}
122122

123-
helpEntries = append(helpEntries, helpEntry{USAGE, cmd.UseLine()})
123+
helpEntries = append(helpEntries, helpEntry{usage, cmd.UseLine()})
124124
if len(coreCommands) > 0 {
125-
helpEntries = append(helpEntries, helpEntry{CORECMD, strings.Join(coreCommands, "\n")})
125+
helpEntries = append(helpEntries, helpEntry{corecmd, strings.Join(coreCommands, "\n")})
126126
}
127127
for group, cmds := range groupCommands {
128128
helpEntries = append(helpEntries, helpEntry{fmt.Sprintf("%s commands", toTitle(group)), strings.Join(cmds, "\n")})
129129
}
130130
if len(otherCommands) > 0 {
131-
helpEntries = append(helpEntries, helpEntry{OTHERCMD, strings.Join(otherCommands, "\n")})
131+
helpEntries = append(helpEntries, helpEntry{othercmd, strings.Join(otherCommands, "\n")})
132132
}
133133
if len(helpCommands) > 0 {
134-
helpEntries = append(helpEntries, helpEntry{HELPCMD, strings.Join(helpCommands, "\n")})
134+
helpEntries = append(helpEntries, helpEntry{helpcmd, strings.Join(helpCommands, "\n")})
135135
}
136136
if flagUsages := cmd.LocalFlags().FlagUsages(); flagUsages != "" {
137-
helpEntries = append(helpEntries, helpEntry{FLAGS, dedent(flagUsages)})
137+
helpEntries = append(helpEntries, helpEntry{flags, dedent(flagUsages)})
138138
}
139139
if inheritedFlagUsages := cmd.InheritedFlags().FlagUsages(); inheritedFlagUsages != "" {
140-
helpEntries = append(helpEntries, helpEntry{IFLAGS, dedent(inheritedFlagUsages)})
140+
helpEntries = append(helpEntries, helpEntry{iflags, dedent(inheritedFlagUsages)})
141141
}
142142
if argsAnnotation, ok := cmd.Annotations["help:arguments"]; ok {
143-
helpEntries = append(helpEntries, helpEntry{ARGUMENTS, argsAnnotation})
143+
helpEntries = append(helpEntries, helpEntry{arguments, argsAnnotation})
144144
}
145145
if cmd.Example != "" {
146-
helpEntries = append(helpEntries, helpEntry{EXAMPLES, cmd.Example})
146+
helpEntries = append(helpEntries, helpEntry{examples, cmd.Example})
147147
}
148148
if argsAnnotation, ok := cmd.Annotations["help:environment"]; ok {
149-
helpEntries = append(helpEntries, helpEntry{ENVS, argsAnnotation})
149+
helpEntries = append(helpEntries, helpEntry{envs, argsAnnotation})
150150
}
151151
if argsAnnotation, ok := cmd.Annotations["help:learn"]; ok {
152-
helpEntries = append(helpEntries, helpEntry{LEARN, argsAnnotation})
152+
helpEntries = append(helpEntries, helpEntry{learn, argsAnnotation})
153153
}
154154
if argsAnnotation, ok := cmd.Annotations["help:feedback"]; ok {
155-
helpEntries = append(helpEntries, helpEntry{FEEDBACK, argsAnnotation})
155+
helpEntries = append(helpEntries, helpEntry{feedback, argsAnnotation})
156156
}
157157
return helpEntries
158158
}

cli/cmdx/hooks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package cmdx
22

33
// AddClientHooks applies all configured hooks to commands annotated with `client:true`.
4-
func (m *Manager) AddClientHooks() {
4+
func (m *Commander) AddClientHooks() {
55
for _, cmd := range m.RootCmd.Commands() {
66
for _, hook := range m.Hooks {
77
if cmd.Annotations["client"] == "true" {

cli/cmdx/markdown.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
// AddMarkdownCommand integrates a hidden `markdown` command into the root command.
1313
// This command generates a Markdown documentation tree for all commands in the hierarchy.
14-
func (m *Manager) AddMarkdownCommand(outputPath string) {
14+
func (m *Commander) AddMarkdownCommand(outputPath string) {
1515
markdownCmd := &cobra.Command{
1616
Use: "markdown",
1717
Short: "Generate Markdown documentation for all commands",
@@ -35,7 +35,7 @@ func (m *Manager) AddMarkdownCommand(outputPath string) {
3535
//
3636
// Returns:
3737
// - An error if any part of the process (file creation, directory creation) fails.
38-
func (m *Manager) generateMarkdownTree(rootOutputPath string, cmd *cobra.Command) error {
38+
func (m *Commander) generateMarkdownTree(rootOutputPath string, cmd *cobra.Command) error {
3939
dirFilePath := filepath.Join(rootOutputPath, cmd.Name())
4040

4141
// Handle subcommands by creating a directory and iterating through subcommands.

cli/cmdx/reference.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import (
1818
//
1919
// Example:
2020
//
21-
// manager := cmdx.NewManager(rootCmd)
21+
// manager := cmdx.NewCommander(rootCmd)
2222
// manager.AddReferenceCommand()
23-
func (m *Manager) AddReferenceCommand() {
23+
func (m *Commander) AddReferenceCommand() {
2424
var isPlain bool
2525
refCmd := &cobra.Command{
2626
Use: "reference",
@@ -39,7 +39,7 @@ func (m *Manager) AddReferenceCommand() {
3939

4040
// runReferenceCommand handles the output generation for the `reference` command.
4141
// It renders the documentation either as plain markdown or with ANSI color.
42-
func (m *Manager) runReferenceCommand(isPlain *bool) func(cmd *cobra.Command, args []string) {
42+
func (m *Commander) runReferenceCommand(isPlain *bool) func(cmd *cobra.Command, args []string) {
4343
return func(cmd *cobra.Command, args []string) {
4444
var (
4545
output string
@@ -62,7 +62,7 @@ func (m *Manager) runReferenceCommand(isPlain *bool) func(cmd *cobra.Command, ar
6262

6363
// generateReferenceMarkdown generates a complete markdown representation
6464
// of the command tree for the `reference` command.
65-
func (m *Manager) generateReferenceMarkdown() string {
65+
func (m *Commander) generateReferenceMarkdown() string {
6666
buf := bytes.NewBufferString(fmt.Sprintf("# %s reference\n\n", m.RootCmd.Name()))
6767
for _, c := range m.RootCmd.Commands() {
6868
if c.Hidden {
@@ -75,7 +75,7 @@ func (m *Manager) generateReferenceMarkdown() string {
7575

7676
// generateCommandReference recursively generates markdown for a given command
7777
// and its subcommands.
78-
func (m *Manager) generateCommandReference(w io.Writer, cmd *cobra.Command, depth int) {
78+
func (m *Commander) generateCommandReference(w io.Writer, cmd *cobra.Command, depth int) {
7979
// Name + Description
8080
fmt.Fprintf(w, "%s `%s`\n\n", strings.Repeat("#", depth), cmd.UseLine())
8181
fmt.Fprintf(w, "%s\n\n", cmd.Short)

cli/cmdx/topics.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import (
1010
//
1111
// Help topics provide detailed information about specific subjects,
1212
// such as environment variables or configuration.
13-
func (m *Manager) AddHelpTopics() {
13+
func (m *Commander) AddHelpTopics() {
1414
for _, topic := range m.Topics {
1515
m.addHelpTopicCommand(topic)
1616
}
1717
}
1818

1919
// addHelpTopicCommand adds a single help topic command to the CLI.
20-
func (m *Manager) addHelpTopicCommand(topic HelpTopic) {
20+
func (m *Commander) addHelpTopicCommand(topic HelpTopic) {
2121
helpCmd := &cobra.Command{
2222
Use: topic.Name,
2323
Short: topic.Short,
File renamed without changes.

cli/config/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ func WithFlags(pfs *pflag.FlagSet) Opts {
4646

4747
// Load reads the configuration file into the Config's Data map.
4848
func (c *Config) Load(cfg interface{}) error {
49-
loaderOpts := []config.LoaderOption{config.WithFile(c.path)}
49+
loaderOpts := []config.Option{config.WithFile(c.path)}
5050

5151
if c.flags != nil {
52-
loaderOpts = append(loaderOpts, config.WithBindPFlags(c.flags, cfg))
52+
loaderOpts = append(loaderOpts, config.WithFlags(c.flags))
5353
}
5454

5555
loader := config.NewLoader(loaderOpts...)

0 commit comments

Comments
 (0)