Skip to content

Commit

Permalink
feat: prompt before applying rules
Browse files Browse the repository at this point in the history
  • Loading branch information
doron-cohen committed Oct 13, 2020
1 parent 68835de commit 061d007
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
18 changes: 17 additions & 1 deletion cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/doron-cohen/antidot/internal/dotfile"
"github.com/doron-cohen/antidot/internal/rules"
"github.com/doron-cohen/antidot/internal/tui"
"github.com/doron-cohen/antidot/internal/utils"
)

Expand Down Expand Up @@ -37,14 +38,29 @@ var cleanCmd = &cobra.Command{

log.Printf("Found %d dotfiles in %s\n", len(dotfiles), userHomeDir)

foundRules := make([]*rules.Rule, 0)
for _, dotfile := range dotfiles {
rule := rules.MatchRule(&dotfile)
if rule == nil {
continue
}

// TODO: prompt before application
rule.Pprint()
foundRules = append(foundRules, rule)
}

confirmed, err := tui.Confirm("Apply rules?")
if err != nil {
log.Fatalf("Failed to read input from stdin: %v", err)
}

if !confirmed {
log.Println("User cancelled. No action was preformed")
return
}

log.Println("Applying rules")
for _, rule := range foundRules {
rule.Apply()
}
},
Expand Down
20 changes: 20 additions & 0 deletions internal/tui/prompt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package tui

import (
"bufio"
"fmt"
"os"
"strings"
)

func Confirm(text string) (bool, error) {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("%s - enter 'yes' to proceed: ", text)
answer, err := reader.ReadString('\n')
if err != nil {
return false, err
}

sanitized := strings.TrimSpace(strings.ToLower(answer))
return sanitized == "yes", nil
}

0 comments on commit 061d007

Please sign in to comment.