feat: add flag app to specific which app to operate

main
sudo pacman -Syu 2024-02-17 21:57:24 +07:00
parent 7470ddde12
commit 9b906c456d
5 changed files with 132 additions and 34 deletions

View File

@ -11,8 +11,9 @@ import (
type action struct { type action struct {
flags struct { flags struct {
verbose bool appNames []string
dryRun bool verbose bool
dryRun bool
} }
} }
@ -27,7 +28,7 @@ func (a *action) runInstall(c *cli.Context) error {
return err 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) return fmt.Errorf("config: failed to install: %w", err)
} }
@ -40,7 +41,7 @@ func (a *action) runUpdate(c *cli.Context) error {
return err 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) return fmt.Errorf("config: failed to update: %w", err)
} }
@ -53,7 +54,7 @@ func (a *action) runDownload(c *cli.Context) error {
return err 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) return fmt.Errorf("config: failed to download: %w", err)
} }
@ -79,7 +80,7 @@ func (a *action) runDiff(c *cli.Context) error {
return err 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) return fmt.Errorf("config: failed to compare: %w", err)
} }
@ -92,7 +93,7 @@ func (a *action) runValidate(c *cli.Context) error {
return err 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) 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) { func (a *action) getFlags(c *cli.Context) {
a.flags.verbose = c.Bool(flagVerboseName) a.flags.verbose = c.Bool(flagVerboseName)
a.flags.dryRun = c.Bool(flagDryRunName) a.flags.dryRun = c.Bool(flagDryRunName)
a.flags.appNames = c.StringSlice(flagAppName)
} }
func (a *action) log(format string, v ...interface{}) { func (a *action) log(format string, v ...interface{}) {

View File

@ -38,6 +38,9 @@ const (
flagDryRunName = "dry-run" flagDryRunName = "dry-run"
flagDryRunUsage = "demo mode without actually changing anything" flagDryRunUsage = "demo mode without actually changing anything"
flagAppName = "app"
flagAppUsage = "specific app to operate"
currentDir = "." currentDir = "."
) )
@ -80,6 +83,10 @@ func NewApp() *App {
Name: flagDryRunName, Name: flagDryRunName,
Usage: flagDryRunUsage, Usage: flagDryRunUsage,
}, },
&cli.StringFlag{
Name: flagAppName,
Usage: flagAppUsage,
},
}, },
Action: a.runInstall, Action: a.runInstall,
}, },
@ -96,6 +103,10 @@ func NewApp() *App {
Name: flagDryRunName, Name: flagDryRunName,
Usage: flagDryRunUsage, Usage: flagDryRunUsage,
}, },
&cli.StringFlag{
Name: flagAppName,
Usage: flagAppUsage,
},
}, },
Action: a.runUpdate, Action: a.runUpdate,
}, },
@ -112,6 +123,10 @@ func NewApp() *App {
Name: flagDryRunName, Name: flagDryRunName,
Usage: flagDryRunUsage, Usage: flagDryRunUsage,
}, },
&cli.StringFlag{
Name: flagAppName,
Usage: flagAppUsage,
},
}, },
Action: a.runDownload, Action: a.runDownload,
}, },
@ -140,6 +155,10 @@ func NewApp() *App {
Name: flagVerboseName, Name: flagVerboseName,
Usage: flagVerboseUsage, Usage: flagVerboseUsage,
}, },
&cli.StringSliceFlag{
Name: flagAppName,
Usage: flagAppUsage,
},
}, },
Action: a.runDiff, Action: a.runDiff,
}, },
@ -152,6 +171,10 @@ func NewApp() *App {
Name: flagVerboseName, Name: flagVerboseName,
Usage: flagVerboseUsage, Usage: flagVerboseUsage,
}, },
&cli.StringFlag{
Name: flagAppName,
Usage: flagAppUsage,
},
}, },
Action: a.runValidate, Action: a.runValidate,
}, },

View File

@ -19,12 +19,12 @@ const (
var ErrConfigNotFound = errors.New("config not found") var ErrConfigNotFound = errors.New("config not found")
type Config interface { type Config interface {
Install() error Install(appNames ...string) error
Update() error Update(appNames ...string) error
Clean() error Clean() error
Diff() error Diff(appNames ...string) error
Download() error Download(appNames ...string) error
Validate() error Validate(appNames ...string) error
} }
type ConfigApps struct { type ConfigApps struct {
@ -75,3 +75,12 @@ func loadConfig(bytes []byte, unmarshalFn func(data []byte, v any) error) (*Conf
return &cfgReal, &cfgDemo, nil 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
}

View File

@ -8,8 +8,16 @@ type ConfigDemo struct {
var _ Config = (*ConfigDemo)(nil) var _ Config = (*ConfigDemo)(nil)
func (c *ConfigDemo) Install() error { func (c *ConfigDemo) Install(appNames ...string) error {
for _, app := range c.Apps { mAppNames := slice2map(appNames)
for appName, app := range c.Apps {
if len(appNames) > 0 {
if _, ok := mAppNames[appName]; !ok {
continue
}
}
for _, p := range app.Paths { for _, p := range app.Paths {
if p.External == "" { if p.External == "" {
continue continue
@ -22,8 +30,16 @@ func (c *ConfigDemo) Install() error {
return nil return nil
} }
func (c *ConfigDemo) Update() error { func (c *ConfigDemo) Update(appNames ...string) error {
for _, app := range c.Apps { mAppNames := slice2map(appNames)
for appName, app := range c.Apps {
if len(appNames) > 0 {
if _, ok := mAppNames[appName]; !ok {
continue
}
}
for _, p := range app.Paths { for _, p := range app.Paths {
if p.External == "" { if p.External == "" {
continue continue
@ -36,8 +52,16 @@ func (c *ConfigDemo) Update() error {
return nil return nil
} }
func (c *ConfigDemo) Download() error { func (c *ConfigDemo) Download(appNames ...string) error {
for _, app := range c.Apps { mAppNames := slice2map(appNames)
for appName, app := range c.Apps {
if len(appNames) > 0 {
if _, ok := mAppNames[appName]; !ok {
continue
}
}
for _, p := range app.Paths { for _, p := range app.Paths {
if p.URL == "" { if p.URL == "" {
continue continue
@ -63,10 +87,10 @@ func (c *ConfigDemo) Clean() error {
return nil return nil
} }
func (c *ConfigDemo) Diff() error { func (c *ConfigDemo) Diff(_ ...string) error {
return nil return nil
} }
func (c *ConfigDemo) Validate() error { func (c *ConfigDemo) Validate(_ ...string) error {
return nil return nil
} }

View File

@ -23,10 +23,18 @@ type ConfigReal struct {
var _ Config = (*ConfigReal)(nil) var _ Config = (*ConfigReal)(nil)
// Install internal -> external // Install internal -> external
func (c *ConfigReal) Install() error { func (c *ConfigReal) Install(appNames ...string) error {
eg := new(errgroup.Group) 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 { for _, p := range app.Paths {
if p.External == "" { if p.External == "" {
continue continue
@ -56,10 +64,18 @@ func (c *ConfigReal) Install() error {
} }
// Update external -> internal // Update external -> internal
func (c *ConfigReal) Update() error { func (c *ConfigReal) Update(appNames ...string) error {
eg := new(errgroup.Group) 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 { for _, p := range app.Paths {
if p.External == "" { if p.External == "" {
continue continue
@ -88,10 +104,18 @@ func (c *ConfigReal) Update() error {
return nil return nil
} }
func (c *ConfigReal) Download() error { func (c *ConfigReal) Download(appNames ...string) error {
eg := new(errgroup.Group) 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 { for _, p := range app.Paths {
if p.URL == "" { if p.URL == "" {
continue continue
@ -184,8 +208,16 @@ func getUnusedDirs(apps map[string]App) (map[string]struct{}, error) {
return unusedDirs, nil return unusedDirs, nil
} }
func (c *ConfigReal) Diff() error { func (c *ConfigReal) Diff(appNames ...string) error {
for _, app := range c.Apps { mAppNames := slice2map(appNames)
for appName, app := range c.Apps {
if len(appNames) > 0 {
if _, ok := mAppNames[appName]; !ok {
continue
}
}
for _, p := range app.Paths { for _, p := range app.Paths {
if p.External == "" { if p.External == "" {
continue continue
@ -200,10 +232,18 @@ func (c *ConfigReal) Diff() error {
return nil return nil
} }
func (c *ConfigReal) Validate() error { func (c *ConfigReal) Validate(appNames ...string) error {
eg := new(errgroup.Group) 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 { for _, p := range app.Paths {
app := app app := app
p := Path{ p := Path{