Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cli config management in config package #72

Merged
merged 3 commits into from
Dec 12, 2024
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
82 changes: 0 additions & 82 deletions cli/cmdx/doc.go

This file was deleted.

63 changes: 0 additions & 63 deletions cli/cmdx/utils.go

This file was deleted.

8 changes: 4 additions & 4 deletions cli/cmdx/markdown.go → cli/commander/codex.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmdx
package commander

import (
"fmt"
Expand All @@ -9,9 +9,9 @@ import (
"github.com/spf13/cobra/doc"
)

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

// Handle subcommands by creating a directory and iterating through subcommands.
Expand Down
46 changes: 18 additions & 28 deletions cli/cmdx/completion.go → cli/commander/completion.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmdx
package commander

import (
"os"
Expand All @@ -7,21 +7,14 @@ import (
"github.com/spf13/cobra"
)

// AddCompletionCommand adds a `completion` command to the CLI.
//
// The completion command generates shell completion scripts for Bash, Zsh,
// Fish, and PowerShell.
//
// Example:
//
// manager := cmdx.NewCommander(rootCmd)
// manager.AddCompletionCommand()
//
// addCompletionCommand adds a `completion` command to the CLI.
// The `completion` command generates shell completion scripts
// for Bash, Zsh, Fish, and PowerShell.
// Usage:
//
// $ mycli completion bash
// $ mycli completion zsh
func (m *Commander) AddCompletionCommand() {
func (m *Manager) addCompletionCommand() {
summary := m.generateCompletionSummary(m.RootCmd.Use)

completionCmd := &cobra.Command{
Expand All @@ -31,28 +24,25 @@ func (m *Commander) AddCompletionCommand() {
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
Args: cobra.ExactValidArgs(1),
Run: m.runCompletionCommand,
Run: func(cmd *cobra.Command, args []string) {
switch args[0] {
case "bash":
cmd.Root().GenBashCompletion(os.Stdout)
case "zsh":
cmd.Root().GenZshCompletion(os.Stdout)
case "fish":
cmd.Root().GenFishCompletion(os.Stdout, true)
case "powershell":
cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
}
},
}

m.RootCmd.AddCommand(completionCmd)
}

// runCompletionCommand executes the appropriate shell completion generation logic.
func (m *Commander) runCompletionCommand(cmd *cobra.Command, args []string) {
switch args[0] {
case "bash":
cmd.Root().GenBashCompletion(os.Stdout)
case "zsh":
cmd.Root().GenZshCompletion(os.Stdout)
case "fish":
cmd.Root().GenFishCompletion(os.Stdout, true)
case "powershell":
cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
}
}

// generateCompletionSummary creates the long description for the `completion` command.
func (m *Commander) generateCompletionSummary(exec string) string {
func (m *Manager) generateCompletionSummary(exec string) string {
var execs []interface{}
for i := 0; i < 12; i++ {
execs = append(execs, exec)
Expand Down
6 changes: 3 additions & 3 deletions cli/cmdx/hooks.go → cli/commander/hooks.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cmdx
package commander

// AddClientHooks applies all configured hooks to commands annotated with `client:true`.
func (m *Commander) AddClientHooks() {
// addClientHooks applies all configured hooks to commands annotated with `client:true`.
func (m *Manager) addClientHooks() {
for _, cmd := range m.RootCmd.Commands() {
for _, hook := range m.Hooks {
if cmd.Annotations["client"] == "true" {
Expand Down
69 changes: 60 additions & 9 deletions cli/cmdx/help.go → cli/commander/layout.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package cmdx
package commander

import (
"bytes"
"errors"
"fmt"
"regexp"
"strings"

"github.com/muesli/termenv"
"golang.org/x/text/cases"
"golang.org/x/text/language"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
Expand All @@ -24,16 +30,10 @@ const (
feedback = "Feedback"
)

// SetCustomHelp configures a custom help function for the CLI.
//
// setCustomHelp configures a custom help function for the CLI.
// The custom help function organizes commands into sections and provides
// detailed error messages for incorrect flag usage.
//
// Example:
//
// manager := cmdx.NewCommander(rootCmd)
// manager.SetCustomHelp()
func (m *Commander) SetCustomHelp() {
func (m *Manager) setCustomHelp() {
m.RootCmd.PersistentFlags().Bool("help", false, "Show help for command")

m.RootCmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -206,3 +206,54 @@ type helpEntry struct {
Title string
Body string
}

// rpad adds padding to the right of a string.
func rpad(s string, padding int) string {
template := fmt.Sprintf("%%-%ds ", padding)
return fmt.Sprintf(template, s)
}

func dedent(s string) string {
lines := strings.Split(s, "\n")
minIndent := -1

for _, l := range lines {
if len(l) == 0 {
continue
}

indent := len(l) - len(strings.TrimLeft(l, " "))
if minIndent == -1 || indent < minIndent {
minIndent = indent
}
}

if minIndent <= 0 {
return s
}

var buf bytes.Buffer
for _, l := range lines {
fmt.Fprintln(&buf, strings.TrimPrefix(l, strings.Repeat(" ", minIndent)))
}
return strings.TrimSuffix(buf.String(), "\n")
}

var lineRE = regexp.MustCompile(`(?m)^`)

func indent(s, indent string) string {
if len(strings.TrimSpace(s)) == 0 {
return s
}
return lineRE.ReplaceAllLiteralString(s, indent)
}

func toTitle(text string) string {
heading := cases.Title(language.Und).String(text)
return heading
}

func bold(text string) termenv.Style {
h := termenv.String(text).Bold()
return h
}
Loading
Loading