forked from Gitlink/gitlink-cli
92 lines
3.4 KiB
Go
92 lines
3.4 KiB
Go
package cmd
|
||
|
||
import (
|
||
"errors"
|
||
"fmt"
|
||
"os"
|
||
|
||
"github.com/spf13/cobra"
|
||
|
||
"github.com/gitlink-org/gitlink-cli/cmd/cmdutil"
|
||
authCmd "github.com/gitlink-org/gitlink-cli/cmd/auth"
|
||
apiCmd "github.com/gitlink-org/gitlink-cli/cmd/api"
|
||
configCmd "github.com/gitlink-org/gitlink-cli/cmd/config"
|
||
"github.com/gitlink-org/gitlink-cli/internal/compliance"
|
||
"github.com/gitlink-org/gitlink-cli/shortcuts"
|
||
)
|
||
|
||
var Version = "dev"
|
||
|
||
var rootCmd = &cobra.Command{
|
||
Use: "gitlink-cli",
|
||
Short: "GitLink CLI — command-line tool for gitlink.org.cn",
|
||
Long: `gitlink-cli is a command-line interface for the GitLink (确实开源) platform, providing repository management, issue tracking, pull requests, CI/CD, and AI-powered workflows.`,
|
||
SilenceUsage: true,
|
||
SilenceErrors: true,
|
||
}
|
||
|
||
func init() {
|
||
rootCmd.PersistentFlags().StringVar(&cmdutil.Owner, "owner", "", "Repository owner (auto-detected from git remote)")
|
||
rootCmd.PersistentFlags().StringVar(&cmdutil.Repo, "repo", "", "Repository name (auto-detected from git remote)")
|
||
rootCmd.PersistentFlags().StringVar(&cmdutil.Format, "format", "table", "Output format: json, table, yaml")
|
||
rootCmd.PersistentFlags().BoolVar(&cmdutil.Debug, "debug", false, "Enable debug output")
|
||
rootCmd.PersistentFlags().BoolVar(&cmdutil.NoTruncate, "no-truncate", false, "Disable value truncation in table output")
|
||
rootCmd.PersistentFlags().StringVar(&cmdutil.Columns, "columns", "", "Columns to show in table output (comma-separated)")
|
||
rootCmd.PersistentFlags().BoolVar(&cmdutil.NoColor, "no-color", false, "Disable colored output")
|
||
|
||
rootCmd.AddCommand(authCmd.NewAuthCmd())
|
||
rootCmd.AddCommand(apiCmd.NewAPICmd())
|
||
rootCmd.AddCommand(configCmd.NewConfigCmd())
|
||
rootCmd.AddCommand(versionCmd)
|
||
rootCmd.AddCommand(completionCmd)
|
||
rootCmd.AddCommand(compliance.NewCommand())
|
||
|
||
shortcuts.RegisterAll(rootCmd)
|
||
}
|
||
|
||
var versionCmd = &cobra.Command{
|
||
Use: "version",
|
||
Short: "Print version information",
|
||
Run: func(cmd *cobra.Command, args []string) {
|
||
fmt.Printf("gitlink-cli %s\n", Version)
|
||
},
|
||
}
|
||
|
||
var completionCmd = &cobra.Command{
|
||
Use: "completion [bash|zsh|fish|powershell]",
|
||
Short: "Generate shell completion script",
|
||
Long: "Generate shell autocompletion script for the specified shell.\n\nTo load completions:\n\n Bash:\n source <(gitlink-cli completion bash)\n\n Zsh:\n source <(gitlink-cli completion zsh)\n\n fish:\n gitlink-cli completion fish | source\n\n PowerShell:\n gitlink-cli completion powershell | Out-String | Invoke-Expression",
|
||
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
|
||
RunE: func(cmd *cobra.Command, args []string) error {
|
||
shell := "bash"
|
||
if len(args) > 0 {
|
||
shell = args[0]
|
||
}
|
||
switch shell {
|
||
case "bash":
|
||
return cmd.Root().GenBashCompletion(os.Stdout)
|
||
case "zsh":
|
||
return cmd.Root().GenZshCompletion(os.Stdout)
|
||
case "fish":
|
||
return cmd.Root().GenFishCompletion(os.Stdout, true)
|
||
case "powershell":
|
||
return cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
|
||
default:
|
||
return fmt.Errorf("unsupported shell: %s (valid: bash, zsh, fish, powershell)", shell)
|
||
}
|
||
},
|
||
}
|
||
|
||
func Execute() error {
|
||
if err := rootCmd.Execute(); err != nil {
|
||
// ErrSilent 表示错误已经按 envelope 格式输出到 stdout(如 API 错误),
|
||
// 这里只需保留非零退出码,不需要再 stderr 重复打印。
|
||
if errors.Is(err, cmdutil.ErrSilent) {
|
||
return err
|
||
}
|
||
fmt.Fprintln(os.Stderr, err)
|
||
return err
|
||
}
|
||
return nil
|
||
}
|