Consolidate CLI code into pkg/cli: - Add pkg/cli/app.go with Main() entry point and completionCmd - Move build variants to internal/variants/ (avoids import cycle) - Move i18n-validate tool to internal/tools/ - Update main.go to call cli.Main() - Remove cmd/ directory entirely Structure: - main.go imports internal/variants (triggers command registration) - main.go calls cli.Main() which runs the CLI - Build variants: go build, go build -tags ci/php/minimal Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
97 lines
2.6 KiB
Go
97 lines
2.6 KiB
Go
package cli
|
|
|
|
import (
|
|
"os"
|
|
|
|
"github.com/host-uk/core/pkg/framework"
|
|
"github.com/host-uk/core/pkg/log"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
const (
|
|
// AppName is the CLI application name.
|
|
AppName = "core"
|
|
// AppVersion is the CLI application version.
|
|
AppVersion = "0.1.0"
|
|
)
|
|
|
|
// Main initialises and runs the CLI application.
|
|
// This is the main entry point for the CLI.
|
|
func Main() error {
|
|
// Initialise CLI runtime with services
|
|
if err := Init(Options{
|
|
AppName: AppName,
|
|
Version: AppVersion,
|
|
Services: []framework.Option{
|
|
framework.WithName("i18n", NewI18nService(I18nOptions{})),
|
|
framework.WithName("log", NewLogService(log.Options{
|
|
Level: log.LevelInfo,
|
|
})),
|
|
},
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
defer Shutdown()
|
|
|
|
// Add completion command to the CLI's root
|
|
RootCmd().AddCommand(completionCmd)
|
|
|
|
return Execute()
|
|
}
|
|
|
|
// completionCmd generates shell completion scripts.
|
|
var completionCmd = &cobra.Command{
|
|
Use: "completion [bash|zsh|fish|powershell]",
|
|
Short: "Generate shell completion script",
|
|
Long: `Generate shell completion script for the specified shell.
|
|
|
|
To load completions:
|
|
|
|
Bash:
|
|
$ source <(core completion bash)
|
|
|
|
# To load completions for each session, execute once:
|
|
# Linux:
|
|
$ core completion bash > /etc/bash_completion.d/core
|
|
# macOS:
|
|
$ core completion bash > $(brew --prefix)/etc/bash_completion.d/core
|
|
|
|
Zsh:
|
|
# If shell completion is not already enabled in your environment,
|
|
# you will need to enable it. You can execute the following once:
|
|
$ echo "autoload -U compinit; compinit" >> ~/.zshrc
|
|
|
|
# To load completions for each session, execute once:
|
|
$ core completion zsh > "${fpath[1]}/_core"
|
|
|
|
# You will need to start a new shell for this setup to take effect.
|
|
|
|
Fish:
|
|
$ core completion fish | source
|
|
|
|
# To load completions for each session, execute once:
|
|
$ core completion fish > ~/.config/fish/completions/core.fish
|
|
|
|
PowerShell:
|
|
PS> core completion powershell | Out-String | Invoke-Expression
|
|
|
|
# To load completions for every new session, run:
|
|
PS> core completion powershell > core.ps1
|
|
# and source this file from your PowerShell profile.
|
|
`,
|
|
DisableFlagsInUseLine: true,
|
|
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
|
|
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
|
|
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)
|
|
}
|
|
},
|
|
}
|