Skip to content

Commit d3ab583

Browse files
authored
Merge pull request #1795 from lizardruss/master
feat: add run commands to help usage
2 parents 348548e + 5c840ca commit d3ab583

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

cmd/root.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,47 @@ func BuildRoot(f factory.Factory, excludePlugins bool) *cobra.Command {
164164
persistentFlags := rootCmd.PersistentFlags()
165165
globalFlags = flags.SetGlobalFlags(persistentFlags)
166166

167+
rootCmd.SetUsageTemplate(`Usage:{{if .Runnable}}
168+
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
169+
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
170+
171+
Aliases:
172+
{{.NameAndAliases}}{{end}}{{if .HasExample}}
173+
174+
Examples:
175+
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
176+
177+
Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
178+
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
179+
180+
Flags:
181+
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
182+
183+
Global Flags:
184+
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
185+
186+
Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
187+
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
188+
189+
{{- if not (eq .Name "run")}}
190+
191+
Use "{{.CommandPath}} [command] --help" for more information about a command.
192+
{{- end -}}
193+
194+
{{- if (and .HasAvailableSubCommands) -}}
195+
{{- range .Commands -}}
196+
{{- if (and .HasSubCommands (eq .Name "run"))}}
197+
198+
Additional run commands:
199+
{{- range .Commands}}
200+
{{rpad (printf "'%s'" .CommandPath) .CommandPathPadding}} {{.Short}}
201+
{{- end -}}
202+
{{- end -}}
203+
{{- end -}}
204+
{{- end -}}
205+
{{end}}
206+
`)
207+
167208
// Add sub commands
168209
rootCmd.AddCommand(add.NewAddCmd(f, globalFlags, plugins))
169210
rootCmd.AddCommand(cleanup.NewCleanupCmd(f, globalFlags, plugins))

cmd/run.go

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@ package cmd
22

33
import (
44
"fmt"
5-
"github.com/loft-sh/devspace/pkg/devspace/hook"
65
"io"
76
"os"
87
"strings"
98

9+
"github.com/loft-sh/devspace/pkg/devspace/hook"
10+
1011
"github.com/loft-sh/devspace/pkg/devspace/config/loader"
12+
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
1113
"github.com/loft-sh/devspace/pkg/devspace/plugin"
1214

1315
"github.com/loft-sh/devspace/cmd/flags"
1416
"github.com/loft-sh/devspace/pkg/devspace/dependency"
1517
"github.com/loft-sh/devspace/pkg/util/factory"
1618
flagspkg "github.com/loft-sh/devspace/pkg/util/flags"
19+
"github.com/loft-sh/devspace/pkg/util/log"
1720
"github.com/loft-sh/devspace/pkg/util/message"
1821
"github.com/sirupsen/logrus"
1922

@@ -93,6 +96,30 @@ devspace --dependency my-dependency run any-command --any-command-flag
9396
},
9497
}
9598

99+
logger := f.GetLog()
100+
commands, _ := getCommands(f, logger)
101+
for _, command := range commands {
102+
description := command.Description
103+
if description == "" {
104+
description = "Runs command: " + command.Command
105+
}
106+
if len(description) > 64 {
107+
if len(description) > 64 {
108+
description = description[:61] + "..."
109+
}
110+
}
111+
runCmd.AddCommand(&cobra.Command{
112+
Use: command.Name,
113+
Short: description,
114+
Long: description,
115+
Args: cobra.ArbitraryArgs,
116+
DisableFlagParsing: true,
117+
RunE: func(cobraCmd *cobra.Command, args []string) error {
118+
return cobraCmd.Parent().RunE(cobraCmd, args)
119+
},
120+
})
121+
}
122+
96123
runCmd.Flags().StringVar(&cmd.Dependency, "dependency", "", "Run a command from a specific dependency")
97124
return runCmd
98125
}
@@ -164,3 +191,22 @@ func (cmd *RunCmd) RunRun(f factory.Factory, args []string) error {
164191
// Execute command
165192
return dependency.ExecuteCommand(commands, args[0], args[1:], cmd.Stdout, cmd.Stderr)
166193
}
194+
195+
func getCommands(f factory.Factory, logger log.Logger) ([]*latest.CommandConfig, error) {
196+
// Set config root
197+
configLoader := f.NewConfigLoader("")
198+
configExists, err := configLoader.SetDevSpaceRoot(logger)
199+
if err != nil {
200+
return nil, err
201+
}
202+
if !configExists {
203+
return nil, errors.New(message.ConfigNotFound)
204+
}
205+
206+
// Parse commands
207+
commandsInterface, err := configLoader.LoadWithParser(loader.NewCommandsParser(), nil, logger)
208+
if err != nil {
209+
return nil, err
210+
}
211+
return commandsInterface.Config().Commands, nil
212+
}

0 commit comments

Comments
 (0)