Skip to content

Commit

Permalink
Suggest typod command names by Levenshtein ditance.
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinChartier committed Jun 24, 2019
1 parent 521796c commit a2b41c7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
21 changes: 20 additions & 1 deletion commands/run.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package commands

import (
"fmt"
"github.com/agnivade/levenshtein"
"github.com/distributed-containers-inc/sanic/config"
"github.com/distributed-containers-inc/sanic/shell"
"github.com/urfave/cli"
"sort"
"strings"
)

func runCommandAction(c *cli.Context) error {
Expand All @@ -27,6 +31,7 @@ func runCommandAction(c *cli.Context) error {
}

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())
Expand All @@ -35,8 +40,22 @@ func runCommandAction(c *cli.Context) error {
}
return cli.NewExitError(err.Error(), code)
}
commandNames = append(commandNames, command.Name)
}
return cli.NewExitError("Command "+commandName+" was not found in environment "+s.GetSanicEnvironment(), 1)
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(
fmt.Sprintf("Command %s was not found in environment %s. Did you mean one of [%s]?",
commandName,
s.GetSanicEnvironment(),
strings.Join(commandNames, "|"),
), 1)

}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/NYTimes/gziphandler v1.1.1 // indirect
github.com/OneOfOne/xxhash v1.2.5 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/agnivade/levenshtein v1.0.2
github.com/apache/thrift v0.12.0 // indirect
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd // indirect
github.com/containerd/cgroups v0.0.0-20190603164311-51b62d303d38 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdko
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
github.com/Shopify/sarama v1.22.1/go.mod h1:FRzlvRpMFO/639zY1SDxUxkqH97Y0ndM5CbGj6oG3As=
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
github.com/agnivade/levenshtein v1.0.2 h1:xKF7WlEzoa+ZVkzBxy0ukdzI2etYiWGlTPMNTBGncKI=
github.com/agnivade/levenshtein v1.0.2/go.mod h1:JLvzGblJATanj48SD0YhHTEFGkWvw3ASLFWSiMIFXsE=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/apache/thrift v0.0.0-20161221203622-b2a4d4ae21c7/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
Expand Down

0 comments on commit a2b41c7

Please sign in to comment.