dotfiles/internal/cli/app.go

178 lines
3.3 KiB
Go
Raw Normal View History

package cli
import (
"fmt"
"os"
"runtime"
"github.com/urfave/cli/v2"
"github.com/make-go-great/color-go"
)
const (
appName = "dotfiles"
appUsage = "managing dotfiles"
installCommand = "install"
2022-10-24 14:57:44 +00:00
installUsage = "install user configs from dotfiles"
2022-10-24 14:57:44 +00:00
updateCommand = "update"
updateUsage = "update dotfiles from user configs"
2022-10-24 14:57:44 +00:00
downloadCommand = "download"
downloadUsage = "download configs from internet (theme for example)"
cleanCommand = "clean"
cleanUsage = "clean unused dotfiles"
2022-10-24 14:57:44 +00:00
diffCommand = "diff"
diffUsage = "diff dotfiles with user configs"
2023-02-25 15:30:20 +00:00
validateCommand = "validate"
validateUsage = "validate config"
2022-10-24 14:57:44 +00:00
verboseFlag = "verbose"
verboseUsage = "show what is going on"
dryRunFlag = "dry-run"
dryRunUsage = "demo mode without actually changing anything"
currentDir = "."
)
var (
2023-02-25 15:52:55 +00:00
installAliases = []string{"ins"}
updateAliases = []string{"upd"}
downloadAliases = []string{"dl"}
cleanAliases = []string{"cl"}
2022-10-24 14:57:44 +00:00
diffAliases = []string{"df"}
2023-02-25 15:30:20 +00:00
validateAliases = []string{"vl"}
)
// denyOSes contains OS which is not supported
// go tool dist list
var denyOSes = map[string]struct{}{
2021-05-05 09:32:42 +00:00
"windows": {},
}
type App struct {
cliApp *cli.App
}
func NewApp() *App {
a := &action{}
cliApp := &cli.App{
Name: appName,
Usage: appUsage,
Commands: []*cli.Command{
{
Name: installCommand,
Aliases: installAliases,
Usage: installUsage,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: verboseFlag,
Usage: verboseUsage,
},
&cli.BoolFlag{
Name: dryRunFlag,
Usage: dryRunUsage,
},
},
2021-05-05 09:42:59 +00:00
Action: a.runInstall,
},
{
Name: updateCommand,
Aliases: updateAliases,
Usage: updateUsage,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: verboseFlag,
Usage: verboseUsage,
},
&cli.BoolFlag{
Name: dryRunFlag,
Usage: dryRunUsage,
},
},
2021-05-05 09:42:59 +00:00
Action: a.runUpdate,
},
2022-10-24 14:57:44 +00:00
{
Name: downloadCommand,
Aliases: downloadAliases,
Usage: downloadUsage,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: verboseFlag,
Usage: verboseUsage,
},
&cli.BoolFlag{
Name: dryRunFlag,
Usage: dryRunUsage,
},
},
Action: a.runDownload,
},
{
Name: cleanCommand,
Aliases: cleanAliases,
Usage: cleanUsage,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: verboseFlag,
Usage: verboseUsage,
},
&cli.BoolFlag{
Name: dryRunFlag,
Usage: dryRunUsage,
},
},
2021-05-05 09:42:59 +00:00
Action: a.runClean,
},
{
2022-10-24 14:57:44 +00:00
Name: diffCommand,
Aliases: diffAliases,
Usage: diffUsage,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: verboseFlag,
Usage: verboseUsage,
},
},
2022-05-31 09:27:06 +00:00
Action: a.runDiff,
},
2023-02-25 15:30:20 +00:00
{
Name: validateCommand,
Aliases: validateAliases,
Usage: validateUsage,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: verboseFlag,
Usage: verboseUsage,
},
},
Action: a.runValidate,
},
},
2021-05-05 09:42:59 +00:00
Action: a.runHelp,
}
return &App{
cliApp: cliApp,
}
}
func (a *App) Run() {
// Prevent running at runtime
if _, ok := denyOSes[runtime.GOOS]; ok {
2023-02-25 15:44:26 +00:00
color.PrintAppError(appName, fmt.Sprintf("OS [%s] is not supported right now", runtime.GOOS))
return
}
if err := a.cliApp.Run(os.Args); err != nil {
color.PrintAppError(appName, err.Error())
}
}