Files
lnk/cmd/pull.go
Yar Kravtsov 7f10e1ce8a feat(output): implement configurable color and emoji output
Add new output formatting system with flags for color and emoji control:
- Introduce OutputConfig and Writer structs for flexible output handling
- Add --colors and --emoji/--no-emoji global flags
- Refactor commands to use new Writer for consistent formatting
- Separate error content from presentation for better flexibility
2025-08-03 14:33:44 +03:00

76 lines
2.0 KiB
Go

package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/yarlson/lnk/internal/core"
)
func newPullCmd() *cobra.Command {
cmd := &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,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
host, _ := cmd.Flags().GetString("host")
lnk := core.NewLnk(core.WithHost(host))
w := GetWriter(cmd)
restored, err := lnk.Pull()
if err != nil {
return err
}
if len(restored) > 0 {
var successMsg string
if host != "" {
successMsg = fmt.Sprintf("Successfully pulled changes (host: %s)", host)
} else {
successMsg = "Successfully pulled changes"
}
symlinkText := fmt.Sprintf("Restored %d symlink", len(restored))
if len(restored) > 1 {
symlinkText += "s"
}
symlinkText += ":"
w.Writeln(Message{Text: successMsg, Emoji: "⬇️", Color: ColorBrightGreen, Bold: true}).
WriteString(" ").
Writeln(Link(symlinkText))
for _, file := range restored {
w.WriteString(" ").
Writeln(Sparkles(file))
}
w.WritelnString("").
WriteString(" ").
Writeln(Message{Text: "Your dotfiles are synced and ready!", Emoji: "🎉"})
} else {
var successMsg string
if host != "" {
successMsg = fmt.Sprintf("Successfully pulled changes (host: %s)", host)
} else {
successMsg = "Successfully pulled changes"
}
w.Writeln(Message{Text: successMsg, Emoji: "⬇️", Color: ColorBrightGreen, Bold: true}).
WriteString(" ").
Writeln(Success("All symlinks already in place")).
WriteString(" ").
Writeln(Message{Text: "Everything is up to date!", Emoji: "🎉"})
}
return w.Err()
},
}
cmd.Flags().StringP("host", "H", "", "Pull and restore symlinks for specific host (default: common configuration)")
return cmd
}