mirror of
https://github.com/yarlson/lnk.git
synced 2025-08-29 17:49:47 +02:00
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
76 lines
2.0 KiB
Go
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
|
|
}
|