dotfiles/internal/cli/app.go

201 lines
4.0 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 (
2024-02-17 08:32:41 +00:00
name = "dotfiles"
usage = "managing dotfiles"
2024-02-17 08:32:41 +00:00
commandInstallName = "install"
commandInstallUsage = "install user configs from dotfiles"
2024-02-17 08:32:41 +00:00
commandUpdateName = "update"
commandUpdateUsage = "update dotfiles from user configs"
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"
flagAppName = "app"
flagAppUsage = "specific app to operate"
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{
2024-02-17 08:32:41 +00:00
Name: name,
Usage: usage,
Commands: []*cli.Command{
{
2024-02-17 08:32:41 +00:00
Name: commandInstallName,
Aliases: installAliases,
2024-02-17 08:32:41 +00:00
Usage: commandInstallUsage,
Flags: []cli.Flag{
&cli.BoolFlag{
2024-02-17 08:32:41 +00:00
Name: flagVerboseName,
Usage: flagVerboseUsage,
},
&cli.BoolFlag{
2024-02-17 08:32:41 +00:00
Name: flagDryRunName,
Usage: flagDryRunUsage,
},
2024-02-22 03:47:44 +00:00
&cli.StringSliceFlag{
Name: flagAppName,
Usage: flagAppUsage,
},
},
2021-05-05 09:42:59 +00:00
Action: a.runInstall,
},
{
2024-02-17 08:32:41 +00:00
Name: commandUpdateName,
Aliases: updateAliases,
2024-02-17 08:32:41 +00:00
Usage: commandUpdateUsage,
Flags: []cli.Flag{
&cli.BoolFlag{
2024-02-17 08:32:41 +00:00
Name: flagVerboseName,
Usage: flagVerboseUsage,
},
&cli.BoolFlag{
2024-02-17 08:32:41 +00:00
Name: flagDryRunName,
Usage: flagDryRunUsage,
},
2024-02-22 03:47:44 +00:00
&cli.StringSliceFlag{
Name: flagAppName,
Usage: flagAppUsage,
},
},
2021-05-05 09:42:59 +00:00
Action: a.runUpdate,
},
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{
Name: flagAppName,
Usage: flagAppUsage,
},
2022-10-24 14:57:44 +00:00
},
Action: a.runDownload,
},
{
2024-02-17 08:32:41 +00:00
Name: commandCleanName,
Aliases: cleanAliases,
2024-02-17 08:32:41 +00:00
Usage: commandCleanUsage,
Flags: []cli.Flag{
&cli.BoolFlag{
2024-02-17 08:32:41 +00:00
Name: flagVerboseName,
Usage: flagVerboseUsage,
},
&cli.BoolFlag{
2024-02-17 08:32:41 +00:00
Name: flagDryRunName,
Usage: flagDryRunUsage,
},
},
2021-05-05 09:42:59 +00:00
Action: a.runClean,
},
{
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,
Flags: []cli.Flag{
&cli.BoolFlag{
2024-02-17 08:32:41 +00:00
Name: flagVerboseName,
Usage: flagVerboseUsage,
},
&cli.StringSliceFlag{
Name: flagAppName,
Usage: flagAppUsage,
},
},
2022-05-31 09:27:06 +00:00
Action: a.runDiff,
},
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{
Name: flagAppName,
Usage: flagAppUsage,
},
2023-02-25 15:30:20 +00:00
},
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 {
2024-02-17 08:32:41 +00:00
color.PrintAppError(name, fmt.Sprintf("OS [%s] is not supported right now", runtime.GOOS))
return
}
if err := a.cliApp.Run(os.Args); err != nil {
2024-02-17 08:32:41 +00:00
color.PrintAppError(name, err.Error())
}
}