Skip to content

Commit f46bbdb

Browse files
ddelnanocosmic-copybara
authored andcommitted
Update px cli to make --cloud_addr a required argument (pixie-io#1960)
GitOrigin-RevId: bbb90a3
1 parent b79b6cd commit f46bbdb

7 files changed

Lines changed: 73 additions & 8 deletions

File tree

go.mod

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ require (
4141
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0
4242
github.com/lestrrat-go/jwx v1.2.26
4343
github.com/lib/pq v1.10.4
44+
github.com/manifoldco/promptui v0.9.0
45+
github.com/mattn/go-isatty v0.0.17
4446
github.com/mattn/go-runewidth v0.0.9
4547
github.com/mikefarah/yq/v4 v4.30.8
4648
github.com/nats-io/nats-server/v2 v2.10.4
@@ -75,6 +77,7 @@ require (
7577
go.etcd.io/etcd/client/v3 v3.5.8
7678
go.etcd.io/etcd/server/v3 v3.5.8
7779
go.uber.org/zap v1.24.0
80+
golang.org/x/exp v0.0.0-20230307190834-24139beb5833
7881
golang.org/x/mod v0.9.0
7982
golang.org/x/net v0.17.0
8083
golang.org/x/oauth2 v0.6.0
@@ -118,6 +121,7 @@ require (
118121
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
119122
github.com/cespare/xxhash/v2 v2.2.0 // indirect
120123
github.com/chai2010/gettext-go v1.0.2 // indirect
124+
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
121125
github.com/cockroachdb/apd v1.1.0 // indirect
122126
github.com/cockroachdb/errors v1.8.1 // indirect
123127
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f // indirect
@@ -204,7 +208,6 @@ require (
204208
github.com/magiconair/properties v1.8.7 // indirect
205209
github.com/mailru/easyjson v0.7.7 // indirect
206210
github.com/mattn/go-colorable v0.1.13 // indirect
207-
github.com/mattn/go-isatty v0.0.17 // indirect
208211
github.com/mattn/go-sqlite3 v1.14.5 // indirect
209212
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
210213
github.com/minio/highwayhash v1.0.2 // indirect
@@ -267,7 +270,6 @@ require (
267270
go.uber.org/atomic v1.10.0 // indirect
268271
go.uber.org/multierr v1.6.0 // indirect
269272
golang.org/x/crypto v0.15.0 // indirect
270-
golang.org/x/exp v0.0.0-20230307190834-24139beb5833 // indirect
271273
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
272274
golang.org/x/text v0.14.0 // indirect
273275
golang.org/x/tools v0.7.0 // indirect

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,11 @@ github.com/chai2010/gettext-go v1.0.2 h1:1Lwwip6Q2QGsAdl/ZKPCwTe9fe0CjlUbqj5bFNS
136136
github.com/chai2010/gettext-go v1.0.2/go.mod h1:y+wnP2cHYaVj19NZhYKAwEMH2CI1gNHeQQ+5AjwawxA=
137137
github.com/checkpoint-restore/go-criu/v5 v5.0.0/go.mod h1:cfwC0EG7HMUenopBsUf9d89JlCLQIfgVcNsNN0t6T2M=
138138
github.com/checkpoint-restore/go-criu/v5 v5.3.0/go.mod h1:E/eQpaFtUKGOOSEBZgmKAcn+zUUwWxqcaKZlF54wK8E=
139+
github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE=
139140
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
141+
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
140142
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
143+
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8=
141144
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
142145
github.com/cilium/ebpf v0.6.2/go.mod h1:4tRaxcgiL706VnOzHOdBlY8IEAIdxINsQBcU4xJJXRs=
143146
github.com/cilium/ebpf v0.7.0/go.mod h1:/oI2+1shJiTGAMgl6/RgJr36Eo1jzrRcAWbcXO2usCA=
@@ -642,6 +645,8 @@ github.com/mailru/easyjson v0.7.1/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7
642645
github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
643646
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
644647
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
648+
github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA=
649+
github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg=
645650
github.com/markbates/oncer v0.0.0-20181203154359-bf2de49a0be2/go.mod h1:Ld9puTsIW75CHf65OeIOkyKbteujpZVXDpWK6YGZbxE=
646651
github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kNSCBdG0=
647652
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=

go_deps.bzl

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/pixie_cli/pkg/cmd/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ go_library(
7171
"@com_github_fatih_color//:color",
7272
"@com_github_gofrs_uuid//:uuid",
7373
"@com_github_lestrrat_go_jwx//jwt",
74+
"@com_github_manifoldco_promptui//:promptui",
75+
"@com_github_mattn_go_isatty//:go-isatty",
7476
"@com_github_segmentio_analytics_go_v3//:analytics-go",
7577
"@com_github_sirupsen_logrus//:logrus",
7678
"@com_github_spf13_cobra//:cobra",
@@ -82,6 +84,7 @@ go_library(
8284
"@io_k8s_client_go//kubernetes",
8385
"@io_k8s_client_go//rest",
8486
"@org_golang_google_grpc//:grpc",
87+
"@org_golang_x_exp//slices",
8588
"@org_golang_x_term//:term",
8689
],
8790
)

src/pixie_cli/pkg/cmd/deploy.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
log "github.com/sirupsen/logrus"
3434
"github.com/spf13/cobra"
3535
"github.com/spf13/viper"
36+
"golang.org/x/exp/slices"
3637
"google.golang.org/grpc"
3738
v1 "k8s.io/api/core/v1"
3839
k8serrors "k8s.io/apimachinery/pkg/api/errors"
@@ -57,8 +58,6 @@ import (
5758
)
5859

5960
const (
60-
// DefaultCloudAddr is the Community Cloud address.
61-
DefaultCloudAddr = "withpixie.ai:443"
6261
// DeploySuccess is the successful deploy const.
6362
DeploySuccess = "successfulDeploy"
6463
)
@@ -144,7 +143,7 @@ var DeployCmd = &cobra.Command{
144143

145144
cloudAddr := viper.GetString("cloud_addr")
146145
docsAddr := cloudAddr
147-
if cloudAddr != DefaultCloudAddr {
146+
if !slices.Contains(AvailableCloudAddrs, cloudAddr) {
148147
docsAddr = "px.dev"
149148
}
150149

src/pixie_cli/pkg/cmd/root.go

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ import (
2424
"strings"
2525

2626
"github.com/fatih/color"
27+
"github.com/manifoldco/promptui"
28+
"github.com/mattn/go-isatty"
2729
"github.com/segmentio/analytics-go/v3"
2830
"github.com/spf13/cobra"
2931
"github.com/spf13/pflag"
3032
"github.com/spf13/viper"
33+
"golang.org/x/exp/slices"
3134

3235
"px.dev/pixie/src/pixie_cli/pkg/auth"
3336
"px.dev/pixie/src/pixie_cli/pkg/pxanalytics"
@@ -36,9 +39,18 @@ import (
3639
"px.dev/pixie/src/pixie_cli/pkg/utils"
3740
)
3841

42+
var (
43+
AvailableCloudAddrs = []string{
44+
"withpixie.ai:443",
45+
}
46+
// cloud addr is a required argument. Use empty string since Viper requires a default value.
47+
defaultCloudAddr = ""
48+
)
49+
3950
func init() {
4051
// Flags that are relevant to all sub-commands.
41-
RootCmd.PersistentFlags().StringP("cloud_addr", "a", "withpixie.ai:443", "The address of Pixie Cloud")
52+
53+
RootCmd.PersistentFlags().StringP("cloud_addr", "a", defaultCloudAddr, "The address of Pixie Cloud")
4254
viper.BindPFlag("cloud_addr", RootCmd.PersistentFlags().Lookup("cloud_addr"))
4355

4456
RootCmd.PersistentFlags().StringP("dev_cloud_namespace", "m", "", "The namespace of Pixie Cloud, if using a cluster local cloud.")
@@ -129,7 +141,8 @@ var RootCmd = &cobra.Command{
129141
PersistentPreRun: func(cmd *cobra.Command, args []string) {
130142
printEnvVars()
131143

132-
cloudAddr := viper.GetString("cloud_addr")
144+
cloudAddr := getCloudAddrIfRequired(cmd)
145+
133146
if matched, err := regexp.MatchString(".+:[0-9]+$", cloudAddr); !matched && err == nil {
134147
viper.Set("cloud_addr", cloudAddr+":443")
135148
}
@@ -185,6 +198,42 @@ var RootCmd = &cobra.Command{
185198
},
186199
}
187200

201+
// Name a variable to store a slice of commands that don't require cloudAddr
202+
var cmdsCloudAddrNotReqd = []*cobra.Command{
203+
CollectLogsCmd,
204+
VersionCmd,
205+
}
206+
207+
func getCloudAddrIfRequired(cmd *cobra.Command) string {
208+
// Commands within allow list should be opted out in addition to Cobra's
209+
// default help command
210+
if slices.Contains(cmdsCloudAddrNotReqd, cmd) || cmd.Short == "Help about any command" {
211+
return defaultCloudAddr
212+
}
213+
214+
cloudAddr := viper.GetString("cloud_addr")
215+
if cloudAddr == "" {
216+
if !isatty.IsTerminal(os.Stdin.Fd()) {
217+
utils.Errorf("No cloud address provided during run within non-interactive shell. Please set the cloud address using the `--cloud_addr` flag or `PX_CLOUD_ADDR` environment variable.")
218+
os.Exit(1)
219+
} else {
220+
prompt := promptui.Select{
221+
Label: "Select Pixie cloud",
222+
Items: AvailableCloudAddrs,
223+
}
224+
_, selectedCloud, err := prompt.Run()
225+
if err != nil {
226+
utils.WithError(err).Fatal("Failed to select cloud address")
227+
os.Exit(1)
228+
}
229+
230+
cloudAddr = selectedCloud
231+
viper.Set("cloud_addr", cloudAddr)
232+
}
233+
}
234+
return cloudAddr
235+
}
236+
188237
func checkAuthForCmd(c *cobra.Command) {
189238
if viper.GetString("direct_vizier_addr") != "" {
190239
if viper.GetString("direct_vizier_key") == "" {

src/pixie_cli/pkg/cmd/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func init() {
4545
RunCmd.Flags().BoolP("e2e_encryption", "e", true, "Enable E2E encryption")
4646
RunCmd.Flags().BoolP("all-clusters", "d", false, "Run script across all clusters")
4747
RunCmd.Flags().StringP("cluster", "c", "", "ID of the cluster to run on. "+
48-
"Use 'px get viziers', or visit Admin console: work.withpixie.ai/admin, to find the ID")
48+
"Use 'px get viziers' to find the ID")
4949
RunCmd.Flags().MarkHidden("all-clusters")
5050

5151
RunCmd.Flags().StringP("bundle", "b", "", "Path/URL to bundle file")

0 commit comments

Comments
 (0)