From 9b906c456dcc31f101da4782b366c03c9761a6c9 Mon Sep 17 00:00:00 2001 From: Hau Nguyen Date: Sat, 17 Feb 2024 21:57:24 +0700 Subject: [PATCH] feat: add flag app to specific which app to operate --- internal/cli/action.go | 16 ++++---- internal/cli/app.go | 23 ++++++++++++ internal/config/config.go | 19 +++++++--- internal/config/config_demo.go | 40 ++++++++++++++++---- internal/config/config_real.go | 68 +++++++++++++++++++++++++++------- 5 files changed, 132 insertions(+), 34 deletions(-) diff --git a/internal/cli/action.go b/internal/cli/action.go index 7a2fd26..4d8462b 100644 --- a/internal/cli/action.go +++ b/internal/cli/action.go @@ -11,8 +11,9 @@ import ( type action struct { flags struct { - verbose bool - dryRun bool + appNames []string + verbose bool + dryRun bool } } @@ -27,7 +28,7 @@ func (a *action) runInstall(c *cli.Context) error { return err } - if err := cfg.Install(); err != nil { + if err := cfg.Install(a.flags.appNames...); err != nil { return fmt.Errorf("config: failed to install: %w", err) } @@ -40,7 +41,7 @@ func (a *action) runUpdate(c *cli.Context) error { return err } - if err := cfg.Update(); err != nil { + if err := cfg.Update(a.flags.appNames...); err != nil { return fmt.Errorf("config: failed to update: %w", err) } @@ -53,7 +54,7 @@ func (a *action) runDownload(c *cli.Context) error { return err } - if err := cfg.Download(); err != nil { + if err := cfg.Download(a.flags.appNames...); err != nil { return fmt.Errorf("config: failed to download: %w", err) } @@ -79,7 +80,7 @@ func (a *action) runDiff(c *cli.Context) error { return err } - if err := cfg.Diff(); err != nil { + if err := cfg.Diff(a.flags.appNames...); err != nil { return fmt.Errorf("config: failed to compare: %w", err) } @@ -92,7 +93,7 @@ func (a *action) runValidate(c *cli.Context) error { return err } - if err := cfg.Validate(); err != nil { + if err := cfg.Validate(a.flags.appNames...); err != nil { return fmt.Errorf("config: failed to validate: %w", err) } @@ -119,6 +120,7 @@ func (a *action) loadConfig(c *cli.Context, command string) (config.Config, erro func (a *action) getFlags(c *cli.Context) { 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{}) { diff --git a/internal/cli/app.go b/internal/cli/app.go index 0871e78..845d875 100644 --- a/internal/cli/app.go +++ b/internal/cli/app.go @@ -38,6 +38,9 @@ const ( flagDryRunName = "dry-run" flagDryRunUsage = "demo mode without actually changing anything" + flagAppName = "app" + flagAppUsage = "specific app to operate" + currentDir = "." ) @@ -80,6 +83,10 @@ func NewApp() *App { Name: flagDryRunName, Usage: flagDryRunUsage, }, + &cli.StringFlag{ + Name: flagAppName, + Usage: flagAppUsage, + }, }, Action: a.runInstall, }, @@ -96,6 +103,10 @@ func NewApp() *App { Name: flagDryRunName, Usage: flagDryRunUsage, }, + &cli.StringFlag{ + Name: flagAppName, + Usage: flagAppUsage, + }, }, Action: a.runUpdate, }, @@ -112,6 +123,10 @@ func NewApp() *App { Name: flagDryRunName, Usage: flagDryRunUsage, }, + &cli.StringFlag{ + Name: flagAppName, + Usage: flagAppUsage, + }, }, Action: a.runDownload, }, @@ -140,6 +155,10 @@ func NewApp() *App { Name: flagVerboseName, Usage: flagVerboseUsage, }, + &cli.StringSliceFlag{ + Name: flagAppName, + Usage: flagAppUsage, + }, }, Action: a.runDiff, }, @@ -152,6 +171,10 @@ func NewApp() *App { Name: flagVerboseName, Usage: flagVerboseUsage, }, + &cli.StringFlag{ + Name: flagAppName, + Usage: flagAppUsage, + }, }, Action: a.runValidate, }, diff --git a/internal/config/config.go b/internal/config/config.go index af2ea3c..1e899a1 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -19,12 +19,12 @@ const ( var ErrConfigNotFound = errors.New("config not found") type Config interface { - Install() error - Update() error + Install(appNames ...string) error + Update(appNames ...string) error Clean() error - Diff() error - Download() error - Validate() error + Diff(appNames ...string) error + Download(appNames ...string) error + Validate(appNames ...string) error } type ConfigApps struct { @@ -75,3 +75,12 @@ func loadConfig(bytes []byte, unmarshalFn func(data []byte, v any) error) (*Conf return &cfgReal, &cfgDemo, nil } + +// Helper +func slice2map(vs []string) map[string]struct{} { + m := make(map[string]struct{}, len(vs)) + for _, v := range vs { + m[v] = struct{}{} + } + return m +} diff --git a/internal/config/config_demo.go b/internal/config/config_demo.go index b444b15..93af390 100644 --- a/internal/config/config_demo.go +++ b/internal/config/config_demo.go @@ -8,8 +8,16 @@ type ConfigDemo struct { var _ Config = (*ConfigDemo)(nil) -func (c *ConfigDemo) Install() error { - for _, app := range c.Apps { +func (c *ConfigDemo) Install(appNames ...string) error { + mAppNames := slice2map(appNames) + + for appName, app := range c.Apps { + if len(appNames) > 0 { + if _, ok := mAppNames[appName]; !ok { + continue + } + } + for _, p := range app.Paths { if p.External == "" { continue @@ -22,8 +30,16 @@ func (c *ConfigDemo) Install() error { return nil } -func (c *ConfigDemo) Update() error { - for _, app := range c.Apps { +func (c *ConfigDemo) Update(appNames ...string) error { + mAppNames := slice2map(appNames) + + for appName, app := range c.Apps { + if len(appNames) > 0 { + if _, ok := mAppNames[appName]; !ok { + continue + } + } + for _, p := range app.Paths { if p.External == "" { continue @@ -36,8 +52,16 @@ func (c *ConfigDemo) Update() error { return nil } -func (c *ConfigDemo) Download() error { - for _, app := range c.Apps { +func (c *ConfigDemo) Download(appNames ...string) error { + mAppNames := slice2map(appNames) + + for appName, app := range c.Apps { + if len(appNames) > 0 { + if _, ok := mAppNames[appName]; !ok { + continue + } + } + for _, p := range app.Paths { if p.URL == "" { continue @@ -63,10 +87,10 @@ func (c *ConfigDemo) Clean() error { return nil } -func (c *ConfigDemo) Diff() error { +func (c *ConfigDemo) Diff(_ ...string) error { return nil } -func (c *ConfigDemo) Validate() error { +func (c *ConfigDemo) Validate(_ ...string) error { return nil } diff --git a/internal/config/config_real.go b/internal/config/config_real.go index bf60c73..865b51b 100644 --- a/internal/config/config_real.go +++ b/internal/config/config_real.go @@ -23,10 +23,18 @@ type ConfigReal struct { var _ Config = (*ConfigReal)(nil) // Install internal -> external -func (c *ConfigReal) Install() error { - eg := new(errgroup.Group) +func (c *ConfigReal) Install(appNames ...string) error { + var eg errgroup.Group + + mAppNames := slice2map(appNames) + + for appName, app := range c.Apps { + if len(appNames) > 0 { + if _, ok := mAppNames[appName]; !ok { + continue + } + } - for _, app := range c.Apps { for _, p := range app.Paths { if p.External == "" { continue @@ -56,10 +64,18 @@ func (c *ConfigReal) Install() error { } // Update external -> internal -func (c *ConfigReal) Update() error { - eg := new(errgroup.Group) +func (c *ConfigReal) Update(appNames ...string) error { + var eg errgroup.Group + + mAppNames := slice2map(appNames) + + for appName, app := range c.Apps { + if len(appNames) > 0 { + if _, ok := mAppNames[appName]; !ok { + continue + } + } - for _, app := range c.Apps { for _, p := range app.Paths { if p.External == "" { continue @@ -88,10 +104,18 @@ func (c *ConfigReal) Update() error { return nil } -func (c *ConfigReal) Download() error { - eg := new(errgroup.Group) +func (c *ConfigReal) Download(appNames ...string) error { + var eg errgroup.Group + + mAppNames := slice2map(appNames) + + for appName, app := range c.Apps { + if len(appNames) > 0 { + if _, ok := mAppNames[appName]; !ok { + continue + } + } - for _, app := range c.Apps { for _, p := range app.Paths { if p.URL == "" { continue @@ -184,8 +208,16 @@ func getUnusedDirs(apps map[string]App) (map[string]struct{}, error) { return unusedDirs, nil } -func (c *ConfigReal) Diff() error { - for _, app := range c.Apps { +func (c *ConfigReal) Diff(appNames ...string) error { + mAppNames := slice2map(appNames) + + for appName, app := range c.Apps { + if len(appNames) > 0 { + if _, ok := mAppNames[appName]; !ok { + continue + } + } + for _, p := range app.Paths { if p.External == "" { continue @@ -200,10 +232,18 @@ func (c *ConfigReal) Diff() error { return nil } -func (c *ConfigReal) Validate() error { - eg := new(errgroup.Group) +func (c *ConfigReal) Validate(appNames ...string) error { + var eg errgroup.Group + + mAppNames := slice2map(appNames) + + for appName, app := range c.Apps { + if len(appNames) > 0 { + if _, ok := mAppNames[appName]; !ok { + continue + } + } - for _, app := range c.Apps { for _, p := range app.Paths { app := app p := Path{