Skip to content

Commit

Permalink
feat: add -rm flag
Browse files Browse the repository at this point in the history
  • Loading branch information
gszr committed Jul 14, 2023
1 parent b0115d9 commit 89bf681
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ type Dots struct {
Files map[string]Attributes `yaml:"files"`
}

var logger *log.Logger

var rcFile string
var verbose bool
var logger *log.Logger
var rmOnly bool

func initFlags() {
flag.StringVar(&rcFile, "rc", "dots.yml", "the dots config file")
flag.BoolVar(&verbose, "verbose", false, "verbose output")
flag.BoolVar(&rmOnly, "rm", false, "only remove targets, do not create")
}

func init() {
Expand Down Expand Up @@ -71,9 +74,6 @@ func validateDots(dots *Dots) (bool, []error) {
if ! pathExists(file) {
errs = append(errs, fmt.Errorf("%s: path does not exist", file))
}
if pathExists(attr.Dst) {
errs = append(errs, fmt.Errorf("%s: destination already exists", attr.Dst))
}
if isDirectory(file) && attr.Typ == "copy" {
errs = append(errs, fmt.Errorf("%s: cannot use copy type with directory", file))
}
Expand Down Expand Up @@ -171,6 +171,22 @@ func doDot(file string, attr Attributes) error {
return nil
}

func remove(file string, attr Attributes) error {
if exists, err := pathExists(file), os.Remove(attr.Dst); !exists && err != nil {
logger.Printf("failed removing file %s, %v", attr.Dst, err)
return err
}
if verbose {
switch typ:= attr.Typ; typ {
case "link":
logger.Printf("removing link %s (from %s)", attr.Dst, file)
case "copy":
logger.Printf("removing link %s (from %s)", attr.Dst, file)
}
}
return nil
}

func main() {
flag.Parse()
dots := readDots()
Expand All @@ -182,7 +198,9 @@ func main() {
"": runtime.GOOS,
}
for file, attr := range dots.Files {
if osMap[attr.Os] == runtime.GOOS {
if rmOnly {
remove(file, attr)
} else if osMap[attr.Os] == runtime.GOOS {
doDot(file, attr)
}
}
Expand Down

0 comments on commit 89bf681

Please sign in to comment.