2021-04-22 10:07:19 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"github.com/urfave/cli/v2"
|
2022-12-01 02:47:19 +00:00
|
|
|
|
|
|
|
"github.com/make-go-great/color-go"
|
2021-04-22 10:07:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
appName = "dotfiles"
|
|
|
|
appUsage = "managing dotfiles"
|
|
|
|
|
|
|
|
installCommand = "install"
|
2022-10-24 14:57:44 +00:00
|
|
|
installUsage = "install user configs from dotfiles"
|
2021-04-22 10:07:19 +00:00
|
|
|
|
2022-10-24 14:57:44 +00:00
|
|
|
updateCommand = "update"
|
|
|
|
updateUsage = "update dotfiles from user configs"
|
2021-04-23 03:21:48 +00:00
|
|
|
|
2022-10-24 14:57:44 +00:00
|
|
|
downloadCommand = "download"
|
|
|
|
downloadUsage = "download configs from internet (theme for example)"
|
|
|
|
|
|
|
|
cleanCommand = "clean"
|
2021-04-22 10:07:19 +00:00
|
|
|
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"
|
2021-04-22 10:07:19 +00:00
|
|
|
|
|
|
|
currentDir = "."
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-10-24 14:57:44 +00:00
|
|
|
installAliases = []string{"i"}
|
|
|
|
updateAliases = []string{"u"}
|
|
|
|
downloadAliases = []string{"d"}
|
|
|
|
cleanAliases = []string{"c"}
|
|
|
|
diffAliases = []string{"df"}
|
2023-02-25 15:30:20 +00:00
|
|
|
validateAliases = []string{"vl"}
|
2021-04-22 10:07:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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": {},
|
2021-04-22 10:07:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2021-04-22 10:07:19 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
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,
|
2021-04-22 10:07:19 +00:00
|
|
|
},
|
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,
|
|
|
|
},
|
2021-04-22 10:07:19 +00:00
|
|
|
{
|
|
|
|
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,
|
2021-04-22 10:07:19 +00:00
|
|
|
},
|
2021-04-23 03:21:48 +00:00
|
|
|
{
|
2022-10-24 14:57:44 +00:00
|
|
|
Name: diffCommand,
|
|
|
|
Aliases: diffAliases,
|
|
|
|
Usage: diffUsage,
|
2021-04-23 03:21:48 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: verboseFlag,
|
|
|
|
Usage: verboseUsage,
|
|
|
|
},
|
|
|
|
},
|
2022-05-31 09:27:06 +00:00
|
|
|
Action: a.runDiff,
|
2021-04-23 03:21:48 +00:00
|
|
|
},
|
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-04-22 10:07:19 +00:00
|
|
|
},
|
2021-05-05 09:42:59 +00:00
|
|
|
Action: a.runHelp,
|
2021-04-22 10:07:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &App{
|
|
|
|
cliApp: cliApp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) Run() {
|
|
|
|
// Prevent running at runtime
|
|
|
|
if _, ok := denyOSes[runtime.GOOS]; ok {
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|