Skip to content

Commit

Permalink
cmdr: add LoadedSources
Browse files Browse the repository at this point in the history
  • Loading branch information
hedzr committed Oct 30, 2024
1 parent d5d4d82 commit eefbac3
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 9 deletions.
7 changes: 4 additions & 3 deletions cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ type Runner interface {
Root() *RootCommand // root command
Args() []string // command-line

SuggestRetCode() int // os process return code
SetSuggestRetCode(ret int) // update ret code (0-255) from onAction, onTask, ...
ParsedState() ParsedState // the parsed states
SuggestRetCode() int // os process return code
SetSuggestRetCode(ret int) // update ret code (0-255) from onAction, onTask, ...
ParsedState() ParsedState // the parsed states
LoadedSources() (results []LoadedSources) // the loaded sources

// Actions return a state map.
// The states can be:
Expand Down
1 change: 1 addition & 0 deletions cli/for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,7 @@ func (*workerS) Root() *RootCommand { return nil
func (*workerS) Args() []string { return nil } //
func (w *workerS) SuggestRetCode() int { return w.retCode } //
func (w *workerS) ParsedState() ParsedState { return nil }
func (w *workerS) LoadedSources() (results []LoadedSources) { return }

//

Expand Down
26 changes: 26 additions & 0 deletions cli/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,32 @@ type Loader interface {
Load(app App) (err error)
}

// SingleFileLoadable finds out a loader if it supports loading single file
type SingleFileLoadable interface {
LoadFile(ctx context.Context, filename string, app App) (err error)
}

// WriteBackHandler is same with [store.Writable].
type WriteBackHandler interface {
Save(ctx context.Context) error
}

// LoadedSource is a package which contains all loaded
// files/sources.
type LoadedSource struct {
Main []string // such as main config file(s)
Children []string // the config files in conf.d/ subdirectory
}

// LoadedSources collect the assorted sources.
type LoadedSources map[string]*LoadedSource

// QueryLoadedSources will be available if a loader allows
// which loaded sources are queried.
type QueryLoadedSources interface {
LoadedSources() LoadedSources
}

type RootCommand struct {
AppName string
Version string
Expand Down
27 changes: 21 additions & 6 deletions cli/worker/pre.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,30 +210,45 @@ func (w *workerS) commandsToStoreR(ctx context.Context, root *cli.RootCommand, c
func (w *workerS) loadLoaders(ctx context.Context) (err error) {
w.precheckLoaders(ctx)

// By default, we try loading `$(pwd)/.appName.json'.
// The main reason is the feature doesn't take new dependence to another 3rd-party lib.
// For cmdr/v2, we restrict to go builtins, google, and ours libraries.
// And, ours libraries will not import any others except go builtins and google's.
// By default, we try loading `$(pwd)/.appName.json' if there
// is no any loaders specified.
//
// The main reason is the feature doesn't take new dependence
// to another 3rd-party lib.
//
// For cmdr/v2, we restrict to go builtins, google, and ours
// libraries. And, ours libraries will not import any others
// except go builtins and google's.
if len(w.Config.Loaders) == 0 {
appDir := dir.GetCurrentDir()
appName := w.Name()
jsonLoader := &jsonLoaderS{}
jsonLoader.filename = path.Join(appDir, "."+appName+".json")
logz.DebugContext(ctx, "use internal tiny json loader", "filename", jsonLoader.filename)
w.Config.Loaders = append(w.Config.Loaders, jsonLoader)
return
}

for _, loader := range w.Config.Loaders {
if loader != nil {
if err = loader.Load(w.root.App()); err != nil {
if err = loader.Load(ctx, w.root.App()); err != nil {

Check failure on line 233 in cli/worker/pre.go

View workflow job for this annotation

GitHub Actions / coverage

too many arguments in call to loader.Load

Check failure on line 233 in cli/worker/pre.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

too many arguments in call to loader.Load
break
}
}
}
return
}

func (w *workerS) LoadedSources() (results []cli.LoadedSources) {
for _, loader := range w.Config.Loaders {
if loader != nil {
if q, ok := loader.(cli.QueryLoadedSources); ok {
results = append(results, q.LoadedSources())
}
}
}
return
}

func (w *workerS) precheckLoaders(ctx context.Context) {
if w.configFile != "" {
found := false
Expand Down
2 changes: 2 additions & 0 deletions cmdr.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ func ParsedLastCmd() cli.Cmd { return App().ParsedState().LastCmd() }
func ParsedCommands() []cli.Cmd { return App().ParsedState().MatchedCommands() } // the parsed commands
func ParsedPositionalArgs() []string { return App().ParsedState().PositionalArgs() } // the rest positional args

func LoadedSources() []cli.LoadedSources { return App().LoadedSources() } // the loaded config files or other sources

// Store returns the KVStore associated with current App().
func Store() store.Store { return App().Store() }

Expand Down

0 comments on commit eefbac3

Please sign in to comment.