Skip to content

Commit

Permalink
(#33) Further fixes to plugins support
Browse files Browse the repository at this point in the history
Signed-off-by: R.I.Pienaar <[email protected]>
  • Loading branch information
ripienaar committed May 9, 2023
1 parent 4229f3e commit b0b8389
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions app_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (a *Application) introspectModel() *ApplicationModel {
model := a.Model()
var nf []*FlagModel
for _, flag := range model.Flags {
if flag.Name == "help" || strings.HasPrefix(flag.Name, "help-") || strings.HasPrefix(flag.Name, "completion-") || strings.HasPrefix(flag.Name, "fisk-") {
if flag.Name == "help" || strings.HasPrefix(flag.Name, "help-") || strings.HasPrefix(flag.Name, "completion-") || strings.HasPrefix(flag.Name, "fisk-") || flag.Name == "version" {
continue
}

Expand Down Expand Up @@ -91,7 +91,10 @@ func (c *CmdClause) addFlagsFromModel(model *FlagGroupModel, appFlags *FlagGroup
}

for _, flag := range model.Flags {
if _, ok := c.pluginDelegator.globalFlags.long[flag.Name]; ok {
c.pluginDelegator.flagsIsSet[flag.Name] = func() *bool { v := false; return &v }()

if f, ok := c.pluginDelegator.globalFlags.long[flag.Name]; ok {
f.setByUser = c.pluginDelegator.flagsIsSet[flag.Name]
c.pluginDelegator.proxyGlobals = append(c.pluginDelegator.proxyGlobals, flag.Name)
continue
}
Expand All @@ -104,7 +107,6 @@ func (c *CmdClause) addFlagsFromModel(model *FlagGroupModel, appFlags *FlagGroup
f.required = flag.Required
f.hidden = flag.Hidden

c.pluginDelegator.flagsIsSet[flag.Name] = func() *bool { v := false; return &v }()
f.setByUser = c.pluginDelegator.flagsIsSet[flag.Name]

switch {
Expand All @@ -128,9 +130,9 @@ func (c *CmdClause) pluginAction(pd *pluginDelegator) Action {
parts := strings.Split(pc.SelectedCommand.FullCommand(), " ")
args := parts[1:]

for k, v := range pd.args {
for _, v := range pd.args {
if v != nil && *v != "" {
args = append(args, fmt.Sprintf("%s=%s", k, *v))
args = append(args, *v)
}
}

Expand Down Expand Up @@ -175,6 +177,10 @@ func (c *CmdClause) pluginAction(pd *pluginDelegator) Action {
}

for _, f := range pd.proxyGlobals {
if !*pd.flagsIsSet[f] {
continue
}

args = append(args, fmt.Sprintf("--%s=%s", f, pd.globalFlags.long[f].value.String()))
}

Expand Down Expand Up @@ -212,15 +218,16 @@ func (c *CmdClause) addCommandsFromModel(model *CmdGroupModel) {
pd := pluginDelegator{
parent: c.name,
name: cmd.Name,
command: c.pluginDelegator.command,
flags: map[string]*string{},
flagsIsSet: map[string]*bool{},
cumuFlags: map[string]*[]string{},
args: map[string]*string{},
cumuArgs: map[string]*[]string{},
boolFlags: map[string]*bool{},
unNegBoolFlags: map[string]*bool{},
globalFlags: c.pluginDelegator.globalFlags,
flagsIsSet: c.pluginDelegator.flagsIsSet, // shared with global so global flags isSet is also handled
command: c.pluginDelegator.command, // the command to run is always the same
globalFlags: c.pluginDelegator.globalFlags, // global flags are global
proxyGlobals: c.pluginDelegator.proxyGlobals, // global flags are global
}

cm := c.Command(cmd.Name, cmd.Help)
Expand Down

0 comments on commit b0b8389

Please sign in to comment.