refactor(cmd): improve testability and error handling in CLI commands

This commit is contained in:
Yar Kravtsov
2025-05-24 11:28:16 +03:00
parent 61a9cc8c88
commit fc0b567e9f
10 changed files with 837 additions and 899 deletions

View File

@@ -12,10 +12,12 @@ var (
buildTime = "unknown"
)
var rootCmd = &cobra.Command{
Use: "lnk",
Short: "🔗 Dotfiles, linked. No fluff.",
Long: `🔗 Lnk - Git-native dotfiles management that doesn't suck.
// NewRootCommand creates a new root command (testable)
func NewRootCommand() *cobra.Command {
rootCmd := &cobra.Command{
Use: "lnk",
Short: "🔗 Dotfiles, linked. No fluff.",
Long: `🔗 Lnk - Git-native dotfiles management that doesn't suck.
Move your dotfiles to ~/.config/lnk, symlink them back, and use Git like normal.
That's it.
@@ -28,17 +30,29 @@ That's it.
lnk pull # Get latest changes
🎯 Simple, fast, and Git-native.`,
SilenceUsage: true,
SilenceUsage: true,
Version: fmt.Sprintf("%s (built %s)", version, buildTime),
}
// Add subcommands
rootCmd.AddCommand(newInitCmd())
rootCmd.AddCommand(newAddCmd())
rootCmd.AddCommand(newRemoveCmd())
rootCmd.AddCommand(newStatusCmd())
rootCmd.AddCommand(newPushCmd())
rootCmd.AddCommand(newPullCmd())
return rootCmd
}
// SetVersion sets the version information for the CLI
func SetVersion(v, bt string) {
version = v
buildTime = bt
rootCmd.Version = fmt.Sprintf("%s (built %s)", version, buildTime)
}
func Execute() {
rootCmd := NewRootCommand()
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)