dotfiles/internal/cli/action.go

127 lines
2.5 KiB
Go
Raw Normal View History

package cli
import (
"fmt"
"log"
"github.com/urfave/cli/v2"
"github.com/haunt98/dotfiles/internal/config"
)
type action struct {
flags struct {
appNames []string
verbose bool
dryRun bool
}
}
// Show help by default
2021-05-05 09:42:59 +00:00
func (a *action) runHelp(c *cli.Context) error {
return cli.ShowAppHelp(c)
}
2021-05-05 09:42:59 +00:00
func (a *action) runInstall(c *cli.Context) error {
2024-02-17 08:32:41 +00:00
cfg, err := a.loadConfig(c, commandInstallName)
if err != nil {
return err
}
if err := cfg.Install(a.flags.appNames...); err != nil {
2022-10-24 14:57:44 +00:00
return fmt.Errorf("config: failed to install: %w", err)
}
return nil
}
2021-05-05 09:42:59 +00:00
func (a *action) runUpdate(c *cli.Context) error {
2024-02-17 08:32:41 +00:00
cfg, err := a.loadConfig(c, commandUpdateName)
if err != nil {
return err
}
if err := cfg.Update(a.flags.appNames...); err != nil {
2022-10-24 14:57:44 +00:00
return fmt.Errorf("config: failed to update: %w", err)
}
return nil
}
2022-10-24 14:57:44 +00:00
func (a *action) runDownload(c *cli.Context) error {
2024-02-17 08:32:41 +00:00
cfg, err := a.loadConfig(c, commandDownloadName)
2022-10-24 14:57:44 +00:00
if err != nil {
return err
}
if err := cfg.Download(a.flags.appNames...); err != nil {
2022-10-24 14:57:44 +00:00
return fmt.Errorf("config: failed to download: %w", err)
}
return nil
}
func (a *action) runClean(c *cli.Context) error {
2024-02-17 08:32:41 +00:00
cfg, err := a.loadConfig(c, commandCleanName)
if err != nil {
return err
}
if err := cfg.Clean(); err != nil {
2022-10-24 14:57:44 +00:00
return fmt.Errorf("config: failed to clean: %w", err)
}
return nil
}
2022-05-31 09:27:06 +00:00
func (a *action) runDiff(c *cli.Context) error {
2024-02-17 08:32:41 +00:00
cfg, err := a.loadConfig(c, commandDiffName)
if err != nil {
return err
}
if err := cfg.Diff(a.flags.appNames...); err != nil {
2022-10-24 14:57:44 +00:00
return fmt.Errorf("config: failed to compare: %w", err)
}
return nil
}
2023-02-25 15:30:20 +00:00
func (a *action) runValidate(c *cli.Context) error {
2024-02-17 08:32:41 +00:00
cfg, err := a.loadConfig(c, commandValidateName)
2023-02-25 15:30:20 +00:00
if err != nil {
return err
}
if err := cfg.Validate(a.flags.appNames...); err != nil {
2023-02-25 15:30:20 +00:00
return fmt.Errorf("config: failed to validate: %w", err)
}
return nil
}
2022-10-24 14:57:44 +00:00
func (a *action) loadConfig(c *cli.Context, command string) (config.Config, error) {
a.getFlags(c)
2023-02-25 15:44:26 +00:00
a.log("Start command [%s] with flags [%+v]\n", command, a.flags)
2022-10-24 14:57:44 +00:00
2024-02-17 15:57:59 +00:00
cfg, err := config.LoadConfig(currentDir, a.flags.dryRun)
if err != nil {
2022-10-24 14:57:44 +00:00
return nil, fmt.Errorf("config: failed to load: %w", err)
}
2024-02-17 15:57:59 +00:00
a.log("Config apps %+v\n", cfg.List())
2024-02-17 15:57:59 +00:00
return cfg, nil
}
func (a *action) getFlags(c *cli.Context) {
2024-02-17 08:32:41 +00:00
a.flags.verbose = c.Bool(flagVerboseName)
a.flags.dryRun = c.Bool(flagDryRunName)
a.flags.appNames = c.StringSlice(flagAppName)
}
func (a *action) log(format string, v ...interface{}) {
if a.flags.verbose {
log.Printf(format, v...)
}
}