feat: add compare command (#10)

* feat: add compare commands

* feat: implete compare config

* chore(config): correct config store

Co-authored-by: Tran Hau <ngtranhau@gmail.com>
main
sudo pacman -Syu 2021-04-23 10:21:48 +07:00 committed by GitHub
parent cd085a4b54
commit 3434986e67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 58 additions and 6 deletions

View File

@ -3,7 +3,7 @@
"nvim": { "nvim": {
"paths": [ "paths": [
{ {
"internal": "config/nvim/init.vim", "internal": "data/nvim/init.vim",
"external": "~/.config/nvim/init.vim" "external": "~/.config/nvim/init.vim"
} }
] ]

2
go.mod
View File

@ -5,7 +5,7 @@ go 1.16
require ( require (
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/haunt98/color v0.1.0 github.com/haunt98/color v0.1.0
github.com/haunt98/copy-go v0.5.0 github.com/haunt98/copy-go v0.7.0
github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/urfave/cli/v2 v2.3.0 github.com/urfave/cli/v2 v2.3.0
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 // indirect golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 // indirect

6
go.sum
View File

@ -6,12 +6,14 @@ github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/haunt98/color v0.1.0 h1:qfP5oNI3aoUC8T+bH/JNVAg79ljyhTGpgfqSKWhkiQQ= github.com/haunt98/color v0.1.0 h1:qfP5oNI3aoUC8T+bH/JNVAg79ljyhTGpgfqSKWhkiQQ=
github.com/haunt98/color v0.1.0/go.mod h1:V4BPVUSuiOItuVZHRHUTkpxO7OYQiP0DSgIWMpC/2qs= github.com/haunt98/color v0.1.0/go.mod h1:V4BPVUSuiOItuVZHRHUTkpxO7OYQiP0DSgIWMpC/2qs=
github.com/haunt98/copy-go v0.5.0 h1:8yy7Dx47BBtlHIFIXxcCIECZRoQB/JSgLN9yunqtLAQ= github.com/haunt98/copy-go v0.7.0 h1:rmEGI3KEmtjPPDd97bPW3IsAXMfLXQoetk8I83Rf37U=
github.com/haunt98/copy-go v0.5.0/go.mod h1:cK1mRlW7QXPHhe5YI1VtL/U4OjUbRLAtZnO/oucrwRI= github.com/haunt98/copy-go v0.7.0/go.mod h1:6t+cjKYf8mjMrEV+iHhNlLrqRWbiDehm4jsRBeqg+3A=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8= github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e h1:aoZm08cpOy4WuID//EZDgcC4zIxODThtZNPirFr42+A=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=

View File

@ -68,6 +68,22 @@ func (a *action) RunClean(c *cli.Context) error {
return nil return nil
} }
func (a *action) RunCompare(c *cli.Context) error {
a.getFlags(c)
a.log("start %s\n", compareCommand)
cfg, err := a.loadConfig()
if err != nil {
return err
}
if err := cfg.Compare(); err != nil {
return fmt.Errorf("failed to compare config: %w", err)
}
return nil
}
func (a *action) loadConfig() (config.Config, error) { func (a *action) loadConfig() (config.Config, error) {
cfgReal, cfgDemo, err := config.LoadConfig(currentDir) cfgReal, cfgDemo, err := config.LoadConfig(currentDir)
if err != nil { if err != nil {

View File

@ -21,22 +21,27 @@ const (
installCommand = "install" installCommand = "install"
updateCommand = "update" updateCommand = "update"
cleanCommand = "clean" cleanCommand = "clean"
compareCommand = "compare"
// usages // flag usages
verboseUsage = "show what is going on" verboseUsage = "show what is going on"
dryRunUsage = "demo mode without actually changing anything" dryRunUsage = "demo mode without actually changing anything"
// command usages
installUsage = "install user configs from dotfiles" installUsage = "install user configs from dotfiles"
updateUsage = "update dotfiles from user configs" updateUsage = "update dotfiles from user configs"
cleanUsage = "clean unused dotfiles" cleanUsage = "clean unused dotfiles"
compareUsage = "compare dotfiles with user configs"
currentDir = "." currentDir = "."
) )
var ( var (
// aliases // command aliases
installAliases = []string{"i"} installAliases = []string{"i"}
updateAliases = []string{"u"} updateAliases = []string{"u"}
cleanAliases = []string{"c"} cleanAliases = []string{"c"}
compareAliases = []string{"cmp"}
) )
// denyOSes contains OS which is not supported // denyOSes contains OS which is not supported
@ -104,6 +109,18 @@ func NewApp() *App {
}, },
Action: a.RunClean, Action: a.RunClean,
}, },
{
Name: compareCommand,
Aliases: compareAliases,
Usage: compareUsage,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: verboseFlag,
Usage: verboseUsage,
},
},
Action: a.RunCompare,
},
}, },
Action: a.RunHelp, Action: a.RunHelp,
} }

View File

@ -4,4 +4,5 @@ type Config interface {
Install() error Install() error
Update() error Update() error
Clean() error Clean() error
Compare() error
} }

View File

@ -40,3 +40,7 @@ func (cd *configDemo) Clean() error {
return nil return nil
} }
func (cd *configDemo) Compare() error {
return nil
}

View File

@ -107,6 +107,18 @@ func (c *config) Clean() error {
return nil return nil
} }
func (c *config) Compare() error {
for _, app := range c.Apps {
for _, p := range app.Paths {
if err := copy.Compare(p.Internal, p.External); err != nil {
return fmt.Errorf("failed to compare %s with %s: %w", p.Internal, p.External, err)
}
}
}
return nil
}
func getConfigPath(path string) string { func getConfigPath(path string) string {
return filepath.Join(path, configDirPath, configFile) return filepath.Join(path, configDirPath, configFile)
} }