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

@@ -7,39 +7,39 @@ import (
"github.com/yarlson/lnk/internal/core"
)
var initCmd = &cobra.Command{
Use: "init",
Short: "🎯 Initialize a new lnk repository",
Long: "Creates the lnk directory and initializes a Git repository for managing dotfiles.",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
remote, _ := cmd.Flags().GetString("remote")
func newInitCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Short: "🎯 Initialize a new lnk repository",
Long: "Creates the lnk directory and initializes a Git repository for managing dotfiles.",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
remote, _ := cmd.Flags().GetString("remote")
lnk := core.NewLnk()
if err := lnk.InitWithRemote(remote); err != nil {
return fmt.Errorf("failed to initialize lnk: %w", err)
}
lnk := core.NewLnk()
if err := lnk.InitWithRemote(remote); err != nil {
return fmt.Errorf("failed to initialize lnk: %w", err)
}
if remote != "" {
fmt.Printf("🎯 \033[1mInitialized lnk repository\033[0m\n")
fmt.Printf(" 📦 Cloned from: \033[36m%s\033[0m\n", remote)
fmt.Printf(" 📁 Location: \033[90m~/.config/lnk\033[0m\n")
fmt.Printf("\n💡 \033[33mNext steps:\033[0m\n")
fmt.Printf(" • Run \033[1mlnk pull\033[0m to restore symlinks\n")
fmt.Printf(" • Use \033[1mlnk add <file>\033[0m to manage new files\n")
} else {
fmt.Printf("🎯 \033[1mInitialized empty lnk repository\033[0m\n")
fmt.Printf(" 📁 Location: \033[90m~/.config/lnk\033[0m\n")
fmt.Printf("\n💡 \033[33mNext steps:\033[0m\n")
fmt.Printf(" • Run \033[1mlnk add <file>\033[0m to start managing dotfiles\n")
fmt.Printf(" • Add a remote with: \033[1mgit remote add origin <url>\033[0m\n")
}
if remote != "" {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "🎯 \033[1mInitialized lnk repository\033[0m\n")
_, _ = fmt.Fprintf(cmd.OutOrStdout(), " 📦 Cloned from: \033[36m%s\033[0m\n", remote)
_, _ = fmt.Fprintf(cmd.OutOrStdout(), " 📁 Location: \033[90m~/.config/lnk\033[0m\n")
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "\n💡 \033[33mNext steps:\033[0m\n")
_, _ = fmt.Fprintf(cmd.OutOrStdout(), " • Run \033[1mlnk pull\033[0m to restore symlinks\n")
_, _ = fmt.Fprintf(cmd.OutOrStdout(), " • Use \033[1mlnk add <file>\033[0m to manage new files\n")
} else {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "🎯 \033[1mInitialized empty lnk repository\033[0m\n")
_, _ = fmt.Fprintf(cmd.OutOrStdout(), " 📁 Location: \033[90m~/.config/lnk\033[0m\n")
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "\n💡 \033[33mNext steps:\033[0m\n")
_, _ = fmt.Fprintf(cmd.OutOrStdout(), " • Run \033[1mlnk add <file>\033[0m to start managing dotfiles\n")
_, _ = fmt.Fprintf(cmd.OutOrStdout(), " • Add a remote with: \033[1mgit remote add origin <url>\033[0m\n")
}
return nil
},
}
func init() {
initCmd.Flags().StringP("remote", "r", "", "Clone from remote URL instead of creating empty repository")
rootCmd.AddCommand(initCmd)
return nil
},
}
cmd.Flags().StringP("remote", "r", "", "Clone from remote URL instead of creating empty repository")
return cmd
}