Skip to content

Commit

Permalink
Add a global commands block.
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinChartier committed Jul 12, 2019
1 parent 23e5ea5 commit 9fde6d3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -99,6 +99,14 @@ environments:
ls -al
pwd
ps aux
# the global commands block defines commands for every environment.
# note 1: environments can define a command of the same name to override these
# note 2: you must be in an environment (sanic env) to use global commands
commands:
- name: do_stuff
command: ls -al
# the deploy block tells sanic how to deal with your kubernetes resources
deploy:
# for the "kustomize" template language, use "distributedcontainers/templater-kustomize"
Expand Down
62 changes: 40 additions & 22 deletions commands/run.go
Expand Up @@ -10,9 +10,36 @@ import (
"strings"
)

func runCommandAction(c *cli.Context) error {
if c.NArg() == 0 {
return newUsageError(c)
func commandsMap(cfg *config.SanicConfig, env *config.Environment) map[string]config.Command {
commands := make(map[string]config.Command)
for _, globalCommand := range cfg.Commands {
commands[globalCommand.Name] = globalCommand
}
for _, envCommand := range env.Commands {
commands[envCommand.Name] = envCommand
}
return commands
}

func mostSimilarCommands(commands map[string]config.Command, requestedCommand string, num int) []string {
var commandList []string
for _, cmd := range commands {
commandList = append(commandList, cmd.Name)
}
sort.Slice(commandList, func(i, j int) bool {
distI := levenshtein.ComputeDistance(commandList[i], requestedCommand)
distJ := levenshtein.ComputeDistance(commandList[j], requestedCommand)
return distI < distJ
})
if len(commandList) <= num {
return commandList
}
return commandList[:num]
}

func runCommandAction(cliCtx *cli.Context) error {
if cliCtx.NArg() == 0 {
return newUsageError(cliCtx)
}

s, err := shell.Current()
Expand All @@ -30,31 +57,22 @@ func runCommandAction(c *cli.Context) error {
return cli.NewExitError(err.Error(), 1)
}

commandName := c.Args().First()
var commandNames []string
for _, command := range env.Commands {
if command.Name == commandName {
code, err := s.ShellExec(command.Command, c.Args().Tail())
if err == nil {
return nil
}
return cli.NewExitError(err.Error(), code)
commands := commandsMap(&cfg, env)
commandName := cliCtx.Args().First()

if cmd, ok := commands[commandName]; ok {
code, err := s.ShellExec(cmd.Command, cliCtx.Args().Tail())
if err == nil {
return nil
}
commandNames = append(commandNames, command.Name)
}
sort.Slice(commandNames, func(i, j int) bool {
distI := levenshtein.ComputeDistance(commandNames[i], commandName)
distJ := levenshtein.ComputeDistance(commandNames[j], commandName)
return distI < distJ
})
if len(commandNames) > 6 {
commandNames = commandNames[:6]
return cli.NewExitError(err.Error, code)
}

return cli.NewExitError(
fmt.Sprintf("Command %s was not found in environment %s. Did you mean one of [%s]?",
commandName,
s.GetSanicEnvironment(),
strings.Join(commandNames, "|"),
strings.Join(mostSimilarCommands(commands, commandName, 6), "|"),
), 1)

}
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Expand Up @@ -41,6 +41,7 @@ type Build struct {

//SanicConfig is the global structure of entries in sanic.yaml
type SanicConfig struct {
Commands []Command
Environments map[string]Environment
Deploy Deploy
Build Build
Expand Down

0 comments on commit 9fde6d3

Please sign in to comment.