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 (
|
2024-02-17 08:32:41 +00:00
|
|
|
name = "dotfiles"
|
|
|
|
usage = "managing dotfiles"
|
2021-04-22 10:07:19 +00:00
|
|
|
|
2024-02-17 08:32:41 +00:00
|
|
|
commandInstallName = "install"
|
|
|
|
commandInstallUsage = "install user configs from dotfiles"
|
2021-04-22 10:07:19 +00:00
|
|
|
|
2024-02-17 08:32:41 +00:00
|
|
|
commandUpdateName = "update"
|
|
|
|
commandUpdateUsage = "update dotfiles from user configs"
|
2021-04-23 03:21:48 +00:00
|
|
|
|
2024-02-17 08:32:41 +00:00
|
|
|
commandDownloadName = "download"
|
|
|
|
commandDownloadUsage = "download configs from internet (theme for example)"
|
2022-10-24 14:57:44 +00:00
|
|
|
|
2024-02-17 08:32:41 +00:00
|
|
|
commandCleanName = "clean"
|
|
|
|
commandCleanUsage = "clean unused dotfiles"
|
2022-10-24 14:57:44 +00:00
|
|
|
|
2024-02-17 08:32:41 +00:00
|
|
|
commandDiffName = "diff"
|
|
|
|
commandDiffUsage = "diff dotfiles with user configs"
|
2022-10-24 14:57:44 +00:00
|
|
|
|
2024-02-17 08:32:41 +00:00
|
|
|
commandValidateName = "validate"
|
|
|
|
commonValidateUsage = "validate config"
|
2023-02-25 15:30:20 +00:00
|
|
|
|
2024-02-17 08:32:41 +00:00
|
|
|
flagVerboseName = "verbose"
|
|
|
|
flagVerboseUsage = "show what is going on"
|
2022-10-24 14:57:44 +00:00
|
|
|
|
2024-02-17 08:32:41 +00:00
|
|
|
flagDryRunName = "dry-run"
|
|
|
|
flagDryRunUsage = "demo mode without actually changing anything"
|
2021-04-22 10:07:19 +00:00
|
|
|
|
2024-02-17 14:57:24 +00:00
|
|
|
flagAppName = "app"
|
|
|
|
flagAppUsage = "specific app to operate"
|
|
|
|
|
2021-04-22 10:07:19 +00:00
|
|
|
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"}
|
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{
|
2024-02-17 08:32:41 +00:00
|
|
|
Name: name,
|
|
|
|
Usage: usage,
|
2021-04-22 10:07:19 +00:00
|
|
|
Commands: []*cli.Command{
|
|
|
|
{
|
2024-02-17 08:32:41 +00:00
|
|
|
Name: commandInstallName,
|
2021-04-22 10:07:19 +00:00
|
|
|
Aliases: installAliases,
|
2024-02-17 08:32:41 +00:00
|
|
|
Usage: commandInstallUsage,
|
2021-04-22 10:07:19 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
2024-02-17 08:32:41 +00:00
|
|
|
Name: flagVerboseName,
|
|
|
|
Usage: flagVerboseUsage,
|
2021-04-22 10:07:19 +00:00
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
2024-02-17 08:32:41 +00:00
|
|
|
Name: flagDryRunName,
|
|
|
|
Usage: flagDryRunUsage,
|
2021-04-22 10:07:19 +00:00
|
|
|
},
|
2024-02-22 03:47:44 +00:00
|
|
|
&cli.StringSliceFlag{
|
2024-02-17 14:57:24 +00:00
|
|
|
Name: flagAppName,
|
|
|
|
Usage: flagAppUsage,
|
|
|
|
},
|
2021-04-22 10:07:19 +00:00
|
|
|
},
|
2021-05-05 09:42:59 +00:00
|
|
|
Action: a.runInstall,
|
2021-04-22 10:07:19 +00:00
|
|
|
},
|
|
|
|
{
|
2024-02-17 08:32:41 +00:00
|
|
|
Name: commandUpdateName,
|
2021-04-22 10:07:19 +00:00
|
|
|
Aliases: updateAliases,
|
2024-02-17 08:32:41 +00:00
|
|
|
Usage: commandUpdateUsage,
|
2021-04-22 10:07:19 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
2024-02-17 08:32:41 +00:00
|
|
|
Name: flagVerboseName,
|
|
|
|
Usage: flagVerboseUsage,
|
2021-04-22 10:07:19 +00:00
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
2024-02-17 08:32:41 +00:00
|
|
|
Name: flagDryRunName,
|
|
|
|
Usage: flagDryRunUsage,
|
2021-04-22 10:07:19 +00:00
|
|
|
},
|
2024-02-22 03:47:44 +00:00
|
|
|
&cli.StringSliceFlag{
|
2024-02-17 14:57:24 +00:00
|
|
|
Name: flagAppName,
|
|
|
|
Usage: flagAppUsage,
|
|
|
|
},
|
2021-04-22 10:07:19 +00:00
|
|
|
},
|
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
|
|
|
{
|
2024-02-17 08:32:41 +00:00
|
|
|
Name: commandDownloadName,
|
2022-10-24 14:57:44 +00:00
|
|
|
Aliases: downloadAliases,
|
2024-02-17 08:32:41 +00:00
|
|
|
Usage: commandDownloadUsage,
|
2022-10-24 14:57:44 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
2024-02-17 08:32:41 +00:00
|
|
|
Name: flagVerboseName,
|
|
|
|
Usage: flagVerboseUsage,
|
2022-10-24 14:57:44 +00:00
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
2024-02-17 08:32:41 +00:00
|
|
|
Name: flagDryRunName,
|
|
|
|
Usage: flagDryRunUsage,
|
2022-10-24 14:57:44 +00:00
|
|
|
},
|
2024-02-22 03:47:44 +00:00
|
|
|
&cli.StringSliceFlag{
|
2024-02-17 14:57:24 +00:00
|
|
|
Name: flagAppName,
|
|
|
|
Usage: flagAppUsage,
|
|
|
|
},
|
2022-10-24 14:57:44 +00:00
|
|
|
},
|
|
|
|
Action: a.runDownload,
|
|
|
|
},
|
2021-04-22 10:07:19 +00:00
|
|
|
{
|
2024-02-17 08:32:41 +00:00
|
|
|
Name: commandCleanName,
|
2021-04-22 10:07:19 +00:00
|
|
|
Aliases: cleanAliases,
|
2024-02-17 08:32:41 +00:00
|
|
|
Usage: commandCleanUsage,
|
2021-04-22 10:07:19 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
2024-02-17 08:32:41 +00:00
|
|
|
Name: flagVerboseName,
|
|
|
|
Usage: flagVerboseUsage,
|
2021-04-22 10:07:19 +00:00
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
2024-02-17 08:32:41 +00:00
|
|
|
Name: flagDryRunName,
|
|
|
|
Usage: flagDryRunUsage,
|
2021-04-22 10:07:19 +00:00
|
|
|
},
|
|
|
|
},
|
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
|
|
|
{
|
2024-02-17 08:32:41 +00:00
|
|
|
Name: commandDiffName,
|
2022-10-24 14:57:44 +00:00
|
|
|
Aliases: diffAliases,
|
2024-02-17 08:32:41 +00:00
|
|
|
Usage: commandDiffUsage,
|
2021-04-23 03:21:48 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
2024-02-17 08:32:41 +00:00
|
|
|
Name: flagVerboseName,
|
|
|
|
Usage: flagVerboseUsage,
|
2021-04-23 03:21:48 +00:00
|
|
|
},
|
2024-02-17 14:57:24 +00:00
|
|
|
&cli.StringSliceFlag{
|
|
|
|
Name: flagAppName,
|
|
|
|
Usage: flagAppUsage,
|
|
|
|
},
|
2021-04-23 03:21:48 +00:00
|
|
|
},
|
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
|
|
|
{
|
2024-02-17 08:32:41 +00:00
|
|
|
Name: commandValidateName,
|
2023-02-25 15:30:20 +00:00
|
|
|
Aliases: validateAliases,
|
2024-02-17 08:32:41 +00:00
|
|
|
Usage: commonValidateUsage,
|
2023-02-25 15:30:20 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
2024-02-17 08:32:41 +00:00
|
|
|
Name: flagVerboseName,
|
|
|
|
Usage: flagVerboseUsage,
|
2023-02-25 15:30:20 +00:00
|
|
|
},
|
2024-02-22 03:47:44 +00:00
|
|
|
&cli.StringSliceFlag{
|
2024-02-17 14:57:24 +00:00
|
|
|
Name: flagAppName,
|
|
|
|
Usage: flagAppUsage,
|
|
|
|
},
|
2023-02-25 15:30:20 +00:00
|
|
|
},
|
|
|
|
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 {
|
2024-02-17 08:32:41 +00:00
|
|
|
color.PrintAppError(name, fmt.Sprintf("OS [%s] is not supported right now", runtime.GOOS))
|
2021-04-22 10:07:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := a.cliApp.Run(os.Args); err != nil {
|
2024-02-17 08:32:41 +00:00
|
|
|
color.PrintAppError(name, err.Error())
|
2021-04-22 10:07:19 +00:00
|
|
|
}
|
|
|
|
}
|