2021-01-11 18:57:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/fatih/color"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
appName = "dotfiles"
|
2021-01-12 03:17:27 +00:00
|
|
|
|
|
|
|
pathFlag = "path"
|
|
|
|
|
|
|
|
installCommand = "install"
|
|
|
|
updateCommand = "update"
|
2021-01-11 18:57:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2021-01-18 04:28:06 +00:00
|
|
|
a := &action{}
|
|
|
|
|
2021-01-11 18:57:56 +00:00
|
|
|
app := &cli.App{
|
|
|
|
Name: appName,
|
|
|
|
Usage: "managing dotfiles",
|
2021-01-12 03:17:27 +00:00
|
|
|
Commands: []*cli.Command{
|
|
|
|
{
|
|
|
|
Name: installCommand,
|
|
|
|
Aliases: []string{"i"},
|
|
|
|
Usage: "install configs from dotfiles",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: updateCommand,
|
|
|
|
Aliases: []string{"u"},
|
|
|
|
Usage: "update dotfiles from configs",
|
|
|
|
},
|
|
|
|
},
|
2021-01-18 04:28:06 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "pathFlag",
|
|
|
|
Usage: "path to `DOTFILES`",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: a.Run,
|
2021-01-11 18:57:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := app.Run(os.Args); err != nil {
|
|
|
|
// Highlight error
|
|
|
|
fmtErr := color.New(color.FgRed)
|
|
|
|
fmtErr.Printf("[%s error]: ", appName)
|
|
|
|
fmt.Printf("%s\n", err.Error())
|
|
|
|
}
|
|
|
|
}
|
2021-01-18 04:28:06 +00:00
|
|
|
|
|
|
|
type action struct {
|
|
|
|
flags struct {
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show help by default
|
|
|
|
func (a *action) Run(c *cli.Context) error {
|
|
|
|
return cli.ShowAppHelp(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *action) RunInstall(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)
|
|
|
|
}
|
|
|
|
|
2021-01-18 08:29:01 +00:00
|
|
|
if err := cfg.Install(); err != nil {
|
|
|
|
return fmt.Errorf("failed to install config: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-01-18 04:28:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *action) getFlags(c *cli.Context) {
|
|
|
|
a.flags.path = c.String(pathFlag)
|
|
|
|
}
|