Skip to content

Commit

Permalink
Merge pull request #9 from sigmonsays/unlink
Browse files Browse the repository at this point in the history
add unlink
  • Loading branch information
sigmonsays authored Nov 27, 2022
2 parents 2495617 + 8db835e commit f4782dd
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 0 deletions.
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func main() {

ctx := &Context{}
link := &Link{ctx}
unlink := &Unlink{ctx}
status := &Status{ctx}
cleanup := &Cleanup{ctx}

Expand Down Expand Up @@ -50,6 +51,13 @@ func main() {
Action: link.Run,
Flags: link.Flags(),
})
ctx.addCommand(&cli.Command{
Name: "unlink",
Aliases: []string{"u"},
Usage: "remove symlinks",
Action: unlink.Run,
Flags: unlink.Flags(),
})
ctx.addCommand(&cli.Command{
Name: "status",
Aliases: []string{"s"},
Expand Down
161 changes: 161 additions & 0 deletions unlink.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package main

import (
"os"

"github.com/urfave/cli/v2"
)

type Unlink struct {
ctx *Context
}

func (me *Unlink) Flags() []cli.Flag {
return []cli.Flag{
&cli.StringSliceFlag{
Name: "config",
Usage: "config file",
Aliases: []string{"c"},
},
&cli.BoolFlag{
Name: "pretend",
Usage: "pretend mode",
Aliases: []string{"p"},
},
&cli.BoolFlag{
Name: "auto",
Usage: "auto mode",
Aliases: []string{"a"},
},
}
return nil
}

type UnlinkOptions struct {
Pretend bool
AutoMode bool
}

func (me *Unlink) Run(c *cli.Context) error {
opts := &UnlinkOptions{}
configfiles := me.ctx.getConfigFiles(c)
opts.Pretend = c.Bool("pretend")
opts.AutoMode = c.Bool("auto")
log.Tracef("%d files to execute", len(configfiles))

if len(configfiles) == 0 && opts.AutoMode == false {
log.Warnf("Nothing to do, try passing -c dotbot.yaml ")
return nil
}

for _, filename := range configfiles {
err := me.RunFile(opts, filename)
if err != nil {
log.Warnf("RunFile %s: %s", filename, err)
}
}

if opts.AutoMode {
err := me.RunAutoMode(opts)
if err != nil {
log.Warnf("RunAutoMode: %s", err)
}
}

return nil
}

func (me *Unlink) RunFile(opts *UnlinkOptions, path string) error {
log.Tracef("runfile %s", path)
cfg := GetDefaultConfig()
err := cfg.LoadYaml(path)
if err != nil {
return err
}
if log.IsTrace() {
cfg.PrintConfig()
}

return me.RunConfig(opts, cfg)
}

func (me *Unlink) RunConfig(opts *UnlinkOptions, cfg *AppConfig) error {
run, err := CompileRun(cfg.Symlinks)
if err != nil {
return err
}

err = Mkdirs(run.HomeDir, cfg.Mkdirs)
if err != nil {
return err
}

err = DoUnlinks(opts, run)
if err != nil {
return err
}
return nil
}
func DoUnlinks(opts *UnlinkOptions, run *Run) error {
var (
err error
removed int
)

for _, li := range run.Links {
if opts.Pretend {
if li.NeedsCreate == false {
log.Infof("unlink %q %q", li.AbsLink, li.Target)
}
continue
}

if li.NeedsCreate == false {
log.Tracef("Remove %s", li.Target)
err = os.Remove(li.Target)
if err != nil {
log.Warnf("Remove %s: %s", li.Target, err)
continue
}

removed++
}
}
if removed > 0 {
log.Infof("removed %d links", removed)
}
return nil
}

func (me *Unlink) RunAutoMode(opts *UnlinkOptions) error {
cfg := GetDefaultConfig()
cwd, err := os.Getwd()
if err != nil {
return err
}

filenames, err := ListDir(cwd)
if err != nil {
return err
}

// build config using current directory listing
for _, filename := range filenames {
if filename == ".git" {
continue
}
cfg.Symlinks["~/"+filename] = filename
}

run, err := CompileRun(cfg.Symlinks)
if err != nil {
return err
}

err = DoUnlinks(opts, run)
if err != nil {
return err
}

return nil
}

0 comments on commit f4782dd

Please sign in to comment.