Skip to content

Commit

Permalink
Merge #137394
Browse files Browse the repository at this point in the history
137394: roachprod: move command init out of main r=golgeek a=nameisbhaskar

This PR refactors roachprod to move the commands out from the main.go to a separate package. Along with the commands, the flags are also moved. Separating the sub commands to a separate package will help us in better code maintenance and easier integration of these commands with other flows like drtprod. Also, initialisation outside of main can help us in making the commands unit testable.

Note that, with this PR, there is no change in functionality of roachprod.

The code will be further enhance for maintainability by separating the commands from commands.go into different categories (like cluster provisioning, datadog, prometheus) and make them separate go files.

Epic: none
Release note: None

Co-authored-by: Bhaskarjyoti Bora <[email protected]>
  • Loading branch information
craig[bot] and nameisbhaskar committed Dec 23, 2024
2 parents bea204f + 0aa7db1 commit ac3c5f2
Show file tree
Hide file tree
Showing 13 changed files with 2,860 additions and 2,441 deletions.
2 changes: 1 addition & 1 deletion pkg/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -1182,8 +1182,8 @@ GO_TARGETS = [
"//pkg/cmd/roachprod-microbench:roachprod-microbench_test",
"//pkg/cmd/roachprod-stress:roachprod-stress",
"//pkg/cmd/roachprod-stress:roachprod-stress_lib",
"//pkg/cmd/roachprod/cli:cli",
"//pkg/cmd/roachprod/grafana:grafana",
"//pkg/cmd/roachprod/update:update",
"//pkg/cmd/roachprod:roachprod",
"//pkg/cmd/roachprod:roachprod_lib",
"//pkg/cmd/roachtest/cluster:cluster",
Expand Down
31 changes: 2 additions & 29 deletions pkg/cmd/roachprod/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,13 @@ load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")

go_library(
name = "roachprod_lib",
srcs = [
"bash_completion.go",
"flags.go",
"main.go",
],
srcs = ["main.go"],
importpath = "github.com/cockroachdb/cockroach/pkg/cmd/roachprod",
visibility = ["//visibility:private"],
deps = [
"//pkg/build",
"//pkg/cmd/roachprod/grafana",
"//pkg/cmd/roachprod/update",
"//pkg/roachprod",
"//pkg/roachprod/cloud",
"//pkg/roachprod/config",
"//pkg/roachprod/errors",
"//pkg/roachprod/fluentbit",
"//pkg/roachprod/install",
"//pkg/roachprod/opentelemetry",
"//pkg/roachprod/roachprodutil",
"//pkg/roachprod/ssh",
"//pkg/roachprod/ui",
"//pkg/roachprod/vm",
"//pkg/roachprod/vm/gce",
"//pkg/util/envutil",
"//pkg/util/flagutil",
"//pkg/util/timeutil",
"@com_github_cockroachdb_errors//:errors",
"@com_github_fatih_color//:color",
"//pkg/cmd/roachprod/cli",
"@com_github_spf13_cobra//:cobra",
"@org_golang_x_crypto//ssh",
"@org_golang_x_exp//maps",
"@org_golang_x_term//:term",
"@org_golang_x_text//language",
"@org_golang_x_text//message",
],
)

Expand Down
45 changes: 45 additions & 0 deletions pkg/cmd/roachprod/cli/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "cli",
srcs = [
"bash_completion.go",
"commands.go",
"flags.go",
"handlers.go",
"resgistry.go",
"update.go",
"util.go",
],
importpath = "github.com/cockroachdb/cockroach/pkg/cmd/roachprod/cli",
visibility = ["//visibility:public"],
deps = [
"//pkg/cmd/roachprod/grafana",
"//pkg/roachprod",
"//pkg/roachprod/cloud",
"//pkg/roachprod/config",
"//pkg/roachprod/errors",
"//pkg/roachprod/fluentbit",
"//pkg/roachprod/install",
"//pkg/roachprod/opentelemetry",
"//pkg/roachprod/roachprodutil",
"//pkg/roachprod/ssh",
"//pkg/roachprod/ui",
"//pkg/roachprod/vm",
"//pkg/roachprod/vm/gce",
"//pkg/util/envutil",
"//pkg/util/flagutil",
"//pkg/util/timeutil",
"@com_github_cockroachdb_errors//:errors",
"@com_github_cockroachdb_errors//oserror",
"@com_github_fatih_color//:color",
"@com_github_spf13_cobra//:cobra",
"@com_google_cloud_go_storage//:storage",
"@org_golang_google_api//option",
"@org_golang_x_crypto//ssh",
"@org_golang_x_exp//maps",
"@org_golang_x_term//:term",
"@org_golang_x_text//language",
"@org_golang_x_text//message",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,24 @@
// Use of this software is governed by the CockroachDB Software License
// included in the /LICENSE file.

package main
package cli

import (
"fmt"
"strings"

"github.com/spf13/cobra"
)

// setBashCompletionFunction sets up a custom bash completion function to
// autocomplete cluster names in various commands.
func setBashCompletionFunction() {
func (cr *commandRegistry) setBashCompletionFunction() {
// Generate a list of commands that DON'T take a cluster argument.
var s []string
for _, cmd := range []*cobra.Command{createCmd, listCmd, syncCmd, gcCmd} {
s = append(s, fmt.Sprintf("%s_%s", rootCmd.Name(), cmd.Name()))
for _, cmd := range cr.excludeFromBashCompletion {
s = append(s, fmt.Sprintf("%s_%s", cr.rootCmd.Name(), cmd.Name()))
}
excluded := strings.Join(s, " | ")

rootCmd.BashCompletionFunction = fmt.Sprintf(
cr.rootCmd.BashCompletionFunction = fmt.Sprintf(
`__custom_func()
{
# only complete the 2nd arg, e.g. adminurl <foo>
Expand Down
Loading

0 comments on commit ac3c5f2

Please sign in to comment.