Skip to content

Commit

Permalink
Merge pull request #1452 from rsteube/cargo-groups
Browse files Browse the repository at this point in the history
cargo: added command groups
  • Loading branch information
rsteube authored Dec 15, 2022
2 parents d6069b6 + 6560898 commit e748b90
Show file tree
Hide file tree
Showing 18 changed files with 123 additions and 41 deletions.
7 changes: 4 additions & 3 deletions completers/cargo_completer/cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
)

var addCmd = &cobra.Command{
Use: "add",
Short: "Add dependency to a Cargo.toml manifest file",
Run: func(cmd *cobra.Command, args []string) {},
Use: "add",
Short: "Add dependency to a Cargo.toml manifest file",
Run: func(cmd *cobra.Command, args []string) {},
GroupID: groupFor("add"),
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/cargo_completer/cmd/bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
)

var benchCmd = &cobra.Command{
Use: "bench",
Short: "Execute all benchmarks of a local package",
Run: func(cmd *cobra.Command, args []string) {},
Use: "bench",
Short: "Execute all benchmarks of a local package",
Run: func(cmd *cobra.Command, args []string) {},
GroupID: groupFor("bench"),
}

func init() {
Expand Down
1 change: 1 addition & 0 deletions completers/cargo_completer/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var buildCmd = &cobra.Command{
Aliases: []string{"b"},
Short: "Compile a local package and all of its dependencies",
Run: func(cmd *cobra.Command, args []string) {},
GroupID: groupFor("build"),
}

func init() {
Expand Down
1 change: 1 addition & 0 deletions completers/cargo_completer/cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var checkCmd = &cobra.Command{
Aliases: []string{"c"},
Short: "Check a local package and all of its dependencies for errors",
Run: func(cmd *cobra.Command, args []string) {},
GroupID: groupFor("check"),
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/cargo_completer/cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
)

var cleanCmd = &cobra.Command{
Use: "clean",
Short: "Remove artifacts that cargo has generated in the past",
Run: func(cmd *cobra.Command, args []string) {},
Use: "clean",
Short: "Remove artifacts that cargo has generated in the past",
Run: func(cmd *cobra.Command, args []string) {},
GroupID: groupFor("clean"),
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/cargo_completer/cmd/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
)

var docCmd = &cobra.Command{
Use: "doc",
Short: "Build a package's documentation",
Run: func(cmd *cobra.Command, args []string) {},
Use: "doc",
Short: "Build a package's documentation",
Run: func(cmd *cobra.Command, args []string) {},
GroupID: groupFor("doc"),
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/cargo_completer/cmd/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
)

var fetchCmd = &cobra.Command{
Use: "fetch",
Short: "",
Run: func(cmd *cobra.Command, args []string) {},
Use: "fetch",
Short: "",
Run: func(cmd *cobra.Command, args []string) {},
GroupID: groupFor("fetch"),
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/cargo_completer/cmd/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
)

var fixCmd = &cobra.Command{
Use: "fix",
Short: "Automatically fix lint warnings reported by rustc",
Run: func(cmd *cobra.Command, args []string) {},
Use: "fix",
Short: "Automatically fix lint warnings reported by rustc",
Run: func(cmd *cobra.Command, args []string) {},
GroupID: groupFor("fix"),
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/cargo_completer/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
)

var initCmd = &cobra.Command{
Use: "init",
Short: "Create a new cargo package in an existing directory",
Run: func(cmd *cobra.Command, args []string) {},
Use: "init",
Short: "Create a new cargo package in an existing directory",
Run: func(cmd *cobra.Command, args []string) {},
GroupID: groupFor("init"),
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/cargo_completer/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
)

var installCmd = &cobra.Command{
Use: "install",
Short: "Install a Rust binary",
Run: func(cmd *cobra.Command, args []string) {},
Use: "install",
Short: "Install a Rust binary",
Run: func(cmd *cobra.Command, args []string) {},
GroupID: groupFor("install"),
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/cargo_completer/cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
)

var newCmd = &cobra.Command{
Use: "new",
Short: "Create a new cargo package at <path>",
Run: func(cmd *cobra.Command, args []string) {},
Use: "new",
Short: "Create a new cargo package at <path>",
Run: func(cmd *cobra.Command, args []string) {},
GroupID: groupFor("new"),
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/cargo_completer/cmd/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
)

var publishCmd = &cobra.Command{
Use: "publish",
Short: "Upload a package to the registry",
Run: func(cmd *cobra.Command, args []string) {},
Use: "publish",
Short: "Upload a package to the registry",
Run: func(cmd *cobra.Command, args []string) {},
GroupID: groupFor("publish"),
}

func init() {
Expand Down
69 changes: 67 additions & 2 deletions completers/cargo_completer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,29 @@ var rootCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {},
}

const (
group_build = iota
group_manifest
group_package
group_publishing
group_general
)

var groups = []*cobra.Group{
{ID: "build", Title: "Build Commands"},
{ID: "manifest", Title: "Manifest Commands"},
{ID: "package", Title: "Package Commands"},
{ID: "publishing", Title: "Publishing Commands"},
{ID: "general", Title: "General Commands"},
}

func Execute() error {
return rootCmd.Execute()
}

func init() {
rootCmd.AddGroup(groups...)

carapace.Gen(rootCmd).Standalone()

rootCmd.Flags().StringS("Z", "Z", "", "Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details")
Expand Down Expand Up @@ -51,8 +70,9 @@ func init() {
if matches := re.FindStringSubmatch(line); matches != nil {
pluginCmd := &cobra.Command{
Use: matches[1],
Short: matches[2],
Short: matches[3],
Run: func(cmd *cobra.Command, args []string) {},
GroupID: groupFor(matches[1]),
DisableFlagParsing: true,
}

Expand All @@ -63,7 +83,52 @@ func init() {
rootCmd.AddCommand(pluginCmd)
}
}

}
})
}

func groupFor(name string) string {
// https: //doc.rust-lang.org/cargo/commands/index.html
switch name {
case "help",
"version":
return groups[group_general].ID
case "bench",
"build",
"check",
"clean",
"doc",
"fetch",
"fix",
"run",
"rustc",
"rustdoc",
"test",
"report":
return groups[group_build].ID
case "add",
"generate-lockfile",
"locate-project",
"metadata",
"pkgid",
"remove",
"tree",
"update",
"vendor",
"verify-project":
return groups[group_manifest].ID
case "init",
"new",
"search",
"uninstall":
return groups[group_package].ID
case "login",
"owner",
"package",
"publish",
"yank":
return groups[group_publishing].ID
default:
return ""
}
}
1 change: 1 addition & 0 deletions completers/cargo_completer/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var runCmd = &cobra.Command{
Aliases: []string{"r"},
Short: "Run a binary or example of the local package",
Run: func(cmd *cobra.Command, args []string) {},
GroupID: groupFor("run"),
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/cargo_completer/cmd/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
)

var searchCmd = &cobra.Command{
Use: "search",
Short: "Search packages in crates.io",
Run: func(cmd *cobra.Command, args []string) {},
Use: "search",
Short: "Search packages in crates.io",
Run: func(cmd *cobra.Command, args []string) {},
GroupID: groupFor("search"),
}

func init() {
Expand Down
1 change: 1 addition & 0 deletions completers/cargo_completer/cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var testCmd = &cobra.Command{
Aliases: []string{"t"},
Short: "Execute all unit and integration tests and build examples of a local package",
Run: func(cmd *cobra.Command, args []string) {},
GroupID: groupFor("test"),
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/cargo_completer/cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
)

var uninstallCmd = &cobra.Command{
Use: "uninstall",
Short: "Remove a Rust binary",
Run: func(cmd *cobra.Command, args []string) {},
Use: "uninstall",
Short: "Remove a Rust binary",
Run: func(cmd *cobra.Command, args []string) {},
GroupID: groupFor("uninstall"),
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/cargo_completer/cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
)

var updateCmd = &cobra.Command{
Use: "update",
Short: "Update dependencies as recorded in the local lock file",
Run: func(cmd *cobra.Command, args []string) {},
Use: "update",
Short: "Update dependencies as recorded in the local lock file",
Run: func(cmd *cobra.Command, args []string) {},
GroupID: groupFor("update"),
}

func init() {
Expand Down

0 comments on commit e748b90

Please sign in to comment.