Add
Copies a dotfile to the repository and replaces it with a symlink.
var addCmd = &cobra.Command{
Use: "add [FILE]...",
Short: "add dotfiles",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
for _, arg := range args {
freckle := freckles.Freckle{Path: arg}
if err := freckle.Add(false); err != nil {
println(err.Error())
}
}
},
}
Completion is provided with ActionFiles and a couple of tricks.
carapace.Gen(addCmd).PositionalAnyCompletion(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
batch := carapace.Batch(
carapace.ActionFiles(),
)
if c.Value == "" {
batch = append(batch, carapace.ActionCallback(func(c carapace.Context) carapace.Action {
c.Value = "."
return carapace.ActionFiles().Invoke(c).ToA()
}))
}
return batch.ToA().ChdirF(traverse.UserHomeDir)
}),
)
}
First of all, dotfiles are located in your home folder. For convenience, the workdir in Context is changed using ChdirF and
traverse.UserHomeDir
.
Then dotfiles are usually hidden. But ActionFiles only shows them when the
.
prefix is already present. By altering the value in Context and explicitly invoking ActionFiles with it we can force this behaviour.
Batch not only enables concurrent invocation of Actions. It also provides a neat way to add them conditionally. See ActionRefs for a complex example.