dotfiles/main.go

103 lines
1.9 KiB
Go
Raw Normal View History

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
installCommand = "install"
updateCommand = "update"
2021-01-18 17:07:20 +00:00
cleanCommand = "clean"
2021-01-18 09:09:39 +00:00
curentDir = "."
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"},
2021-01-18 08:37:40 +00:00
Usage: "install user configs from dotfiles",
Action: a.RunInstall,
2021-01-12 03:17:27 +00:00
},
{
Name: updateCommand,
Aliases: []string{"u"},
2021-01-18 08:37:40 +00:00
Usage: "update dotfiles from user configs",
Action: a.RunUpdate,
2021-01-12 03:17:27 +00:00
},
2021-01-18 17:07:20 +00:00
{
Name: cleanCommand,
Aliases: []string{"c"},
Usage: "clean unused dotfiles",
Action: a.RunClean,
},
2021-01-12 03:17:27 +00:00
},
2021-01-18 04:28:06 +00:00
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
2021-01-18 09:09:39 +00:00
type action struct{}
2021-01-18 04:28:06 +00:00
// Show help by default
func (a *action) Run(c *cli.Context) error {
return cli.ShowAppHelp(c)
}
func (a *action) RunInstall(c *cli.Context) error {
2021-01-18 09:09:39 +00:00
cfg, err := LoadConfig(curentDir)
2021-01-18 04:28:06 +00:00
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
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
}
2021-01-18 08:37:40 +00:00
func (a *action) RunUpdate(c *cli.Context) error {
2021-01-18 09:09:39 +00:00
cfg, err := LoadConfig(curentDir)
2021-01-18 08:37:40 +00:00
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
}
2021-01-18 17:07:20 +00:00
func (a *action) RunClean(c *cli.Context) error {
2021-01-19 11:06:13 +00:00
cfg, err := LoadConfig(curentDir)
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
if err := cfg.Clean(); err != nil {
return fmt.Errorf("failed to clean config: %w", err)
}
2021-01-18 17:07:20 +00:00
return nil
}