mirror of
https://github.com/yarlson/lnk.git
synced 2025-08-31 18:01:41 +02:00
refactor(cmd): improve testability and error handling in CLI commands
This commit is contained in:
62
cmd/pull.go
62
cmd/pull.go
@@ -7,39 +7,37 @@ import (
|
||||
"github.com/yarlson/lnk/internal/core"
|
||||
)
|
||||
|
||||
var pullCmd = &cobra.Command{
|
||||
Use: "pull",
|
||||
Short: "⬇️ Pull changes from remote and restore symlinks",
|
||||
Long: "Fetches changes from remote repository and automatically restores symlinks for all managed files.",
|
||||
SilenceUsage: true,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
lnk := core.NewLnk()
|
||||
restored, err := lnk.Pull()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to pull changes: %w", err)
|
||||
}
|
||||
|
||||
if len(restored) > 0 {
|
||||
fmt.Printf("⬇️ \033[1;32mSuccessfully pulled changes\033[0m\n")
|
||||
fmt.Printf(" 🔗 Restored \033[1m%d symlink", len(restored))
|
||||
if len(restored) > 1 {
|
||||
fmt.Printf("s")
|
||||
func newPullCmd() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "pull",
|
||||
Short: "⬇️ Pull changes from remote and restore symlinks",
|
||||
Long: "Fetches changes from remote repository and automatically restores symlinks for all managed files.",
|
||||
SilenceUsage: true,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
lnk := core.NewLnk()
|
||||
restored, err := lnk.Pull()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to pull changes: %w", err)
|
||||
}
|
||||
fmt.Printf("\033[0m:\n")
|
||||
for _, file := range restored {
|
||||
fmt.Printf(" ✨ \033[36m%s\033[0m\n", file)
|
||||
|
||||
if len(restored) > 0 {
|
||||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "⬇️ \033[1;32mSuccessfully pulled changes\033[0m\n")
|
||||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), " 🔗 Restored \033[1m%d symlink", len(restored))
|
||||
if len(restored) > 1 {
|
||||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "s")
|
||||
}
|
||||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "\033[0m:\n")
|
||||
for _, file := range restored {
|
||||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), " ✨ \033[36m%s\033[0m\n", file)
|
||||
}
|
||||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "\n 🎉 Your dotfiles are synced and ready!\n")
|
||||
} else {
|
||||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "⬇️ \033[1;32mSuccessfully pulled changes\033[0m\n")
|
||||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), " ✅ All symlinks already in place\n")
|
||||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), " 🎉 Everything is up to date!\n")
|
||||
}
|
||||
fmt.Printf("\n 🎉 Your dotfiles are synced and ready!\n")
|
||||
} else {
|
||||
fmt.Printf("⬇️ \033[1;32mSuccessfully pulled changes\033[0m\n")
|
||||
fmt.Printf(" ✅ All symlinks already in place\n")
|
||||
fmt.Printf(" 🎉 Everything is up to date!\n")
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(pullCmd)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user