Skip to content

Commit

Permalink
feat: rules printing and application
Browse files Browse the repository at this point in the history
  • Loading branch information
doron-cohen committed Sep 17, 2020
1 parent f69d443 commit 269479a
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 10 deletions.
13 changes: 12 additions & 1 deletion cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,18 @@ var cleanCmd = &cobra.Command{
log.Printf("Found %d dotfiles in %s\n", len(dotfiles), userHomeDir)

for _, dotfile := range dotfiles {
action.MatchActions(&dotfile)
rule := action.MatchRule(&dotfile)
if rule == nil {
continue
}

rule.Pprint()
// TODO: move to Rule.Apply()
if !rule.Ignore {
for _, action := range rule.Actions {
action.Apply()
}
}
}
},
}
47 changes: 47 additions & 0 deletions internal/action/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ package action
import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"reflect"

"github.com/doron-cohen/antidot/internal/utils.go"

"github.com/mitchellh/mapstructure"
)

type Action interface {
Apply() error
Pprint()
}

type Migrate struct {
Expand All @@ -17,6 +24,46 @@ type Migrate struct {
Symlink bool
}

func (m Migrate) Apply() error {
source := os.ExpandEnv(m.Source)
_, err := os.Stat(source)
if os.IsNotExist(err) {
log.Printf("File %s doesn't exist. Skipping action", source)
return nil
} else if err != nil {
return err
}

dest := os.ExpandEnv(m.Dest)
if utils.FileExists(dest) {
errMessage := fmt.Sprintf("Destination file %s exists", dest)
return errors.New(errMessage)
}

err = os.MkdirAll(filepath.Dir(dest), os.FileMode(0o755))
if err != nil {
return err
}

err = os.Rename(source, dest)
if err != nil {
return err
}

if m.Symlink {
err = os.Symlink(source, dest)
if err != nil {
return err
}
}

return nil
}

func (m Migrate) Pprint() {
log.Printf("Move %s to %s. Symlink: %v", m.Source, m.Dest, m.Symlink)
}

func getActionByName(name string) (Action, error) {
switch name {
case "migrate":
Expand Down
20 changes: 15 additions & 5 deletions internal/action/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ type Rule struct {
Actions []Action
}

// TODO: use some colors
func (r Rule) Pprint() {
log.Printf("Rule %s:", r.Name)
for _, action := range r.Actions {
action.Pprint()
}

if r.Ignore {
log.Println("Rule ignored")
}
}

type RulesConfig struct {
Version int
Rules []Rule
Expand Down Expand Up @@ -58,14 +70,12 @@ func LoadRulesConfig(filepath string) error {
return nil
}

func MatchActions(dotfile *dotfile.Dotfile) {
func MatchRule(dotfile *dotfile.Dotfile) *Rule {
for _, rule := range rulesConfig.Rules {
if cmp.Equal(dotfile, rule.Dotfile) {
log.Printf("Matched rule %s with dotfile %s", rule.Name, dotfile.Name)
if rule.Ignore {
log.Printf("Ignoring dotfile %s", dotfile.Name)
}
break
return &rule
}
}
return nil
}
11 changes: 11 additions & 0 deletions internal/utils/files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package utils

import "os"

func FileExists(filename string) bool {
_, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return true
}
9 changes: 5 additions & 4 deletions rules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ rules:
name: .aria2
is_dir: true
actions:
# TODO: remove empty ${HOME}/.aria2 directory after migrations
- type: migrate
source: $HOME/.aria2/aria.conf
dest: $XDG_CONFIG_HOME/aria2/aria2.conf
source: ${HOME}/.aria2/aria.conf
dest: ${XDG_CONFIG_HOME}/aria2/aria2.conf
symlink: false
- type: migrate
source: $HOME/.aria2/dht*.dat
dest: $XDG_CACHE_HOME/aria2/
source: ${HOME}/.aria2/dht.dat
dest: ${XDG_CACHE_HOME}/aria2/dht.dat
symlink: false

0 comments on commit 269479a

Please sign in to comment.