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 {
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{}) {

View File

@ -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,
},

View File

@ -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
}

View File

@ -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
}

View File

@ -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{