feat: add update cmd

main
Tran Hau 2021-01-18 15:37:40 +07:00
parent bbb84fe09f
commit 59f3a888c5
3 changed files with 21 additions and 4 deletions

View File

@ -48,7 +48,7 @@ func LoadConfig(path string) (result Config, err error) {
return
}
if err := json.Unmarshal(bytes, &result); err != nil {
if err = json.Unmarshal(bytes, &result); err != nil {
err = fmt.Errorf("failed to unmarshal: %w", err)
return
}

2
go.mod
View File

@ -5,7 +5,7 @@ go 1.15
require (
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/fatih/color v1.10.0
github.com/haunt98/copy-go v0.1.0 // indirect
github.com/haunt98/copy-go v0.1.0
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/urfave/cli/v2 v2.3.0
)

21
main.go
View File

@ -27,12 +27,14 @@ func main() {
{
Name: installCommand,
Aliases: []string{"i"},
Usage: "install configs from dotfiles",
Usage: "install user configs from dotfiles",
Action: a.RunInstall,
},
{
Name: updateCommand,
Aliases: []string{"u"},
Usage: "update dotfiles from configs",
Usage: "update dotfiles from user configs",
Action: a.RunUpdate,
},
},
Flags: []cli.Flag{
@ -78,6 +80,21 @@ func (a *action) RunInstall(c *cli.Context) error {
return nil
}
func (a *action) RunUpdate(c *cli.Context) error {
a.getFlags(c)
cfg, err := LoadConfig(a.flags.path)
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
if err := cfg.Update(); err != nil {
return fmt.Errorf("failed to update config: %w", err)
}
return nil
}
func (a *action) getFlags(c *cli.Context) {
a.flags.path = c.String(pathFlag)
}