diff --git a/cmd/vclusterctl/cmd/platform/get/get.go b/cmd/vclusterctl/cmd/platform/get/get.go index cbd21e8ad..430adb8ec 100644 --- a/cmd/vclusterctl/cmd/platform/get/get.go +++ b/cmd/vclusterctl/cmd/platform/get/get.go @@ -22,5 +22,6 @@ func NewGetCmd(globalFlags *flags.GlobalFlags, defaults *defaults.Defaults, cfg cmd.AddCommand(newClusterCmd(globalFlags, cfg)) cmd.AddCommand(newClusterAccessKeyCmd(globalFlags, cfg)) cmd.AddCommand(newSecretCmd(globalFlags, defaults, cfg)) + cmd.AddCommand(newUserCmd(globalFlags, cfg)) return cmd } diff --git a/cmd/vclusterctl/cmd/platform/get/user.go b/cmd/vclusterctl/cmd/platform/get/user.go new file mode 100644 index 000000000..48a1cf591 --- /dev/null +++ b/cmd/vclusterctl/cmd/platform/get/user.go @@ -0,0 +1,134 @@ +package get + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "os" + + "github.com/ghodss/yaml" + "github.com/loft-sh/api/v4/pkg/product" + "github.com/loft-sh/log" + "github.com/loft-sh/log/table" + "github.com/loft-sh/vcluster/pkg/cli/config" + "github.com/loft-sh/vcluster/pkg/cli/flags" + "github.com/loft-sh/vcluster/pkg/platform" + "github.com/loft-sh/vcluster/pkg/platform/helper" + "github.com/spf13/cobra" +) + +// UserCmd holds the lags +type UserCmd struct { + *flags.GlobalFlags + + log log.Logger + + output string +} + +const ( + OutputName = "name" +) + +// newUserCmd creates a new command +func newUserCmd(globalFlags *flags.GlobalFlags, cfg *config.CLI) *cobra.Command { + cmd := &UserCmd{ + GlobalFlags: globalFlags, + log: log.GetInstance(), + } + description := product.ReplaceWithHeader("platform get current-user", ` +Returns the currently logged in user + +Example: +vcluster platform get current-user +######################################################## + `) + c := &cobra.Command{ + Use: "current-user", + Short: "Retrieves the current logged in user", + Long: description, + Args: cobra.NoArgs, + RunE: func(cobraCmd *cobra.Command, _ []string) error { + return cmd.Run(cobraCmd.Context(), cfg) + }, + } + + c.Flags().StringVarP(&cmd.output, "output", "o", OutputValue, "Output format. One of: (json, yaml, value, name).") + + return c +} + +// RunUsers executes the functionality +func (cmd *UserCmd) Run(ctx context.Context, cfg *config.CLI) error { + baseClient, err := platform.NewClientFromConfig(ctx, cfg) + if err != nil { + return err + } + + client, err := baseClient.Management() + if err != nil { + return err + } + + userName, teamName, err := helper.GetCurrentUser(ctx, client) + if err != nil { + return err + } else if teamName != nil { + return errors.New("logged in with a team and not a user") + } + + switch cmd.output { + case OutputJSON, OutputYAML: + currentUser := struct { + Username string `json:"username"` + Name string `json:"name"` + DisplayName string `json:"displayName"` + Email string `json:"email"` + }{ + Username: userName.Username, + Name: userName.Name, + DisplayName: userName.DisplayName, + Email: userName.Email, + } + + encodedBytes, err := json.Marshal(currentUser) + if err != nil { + return fmt.Errorf("json marshal: %w", err) + } + + if cmd.output == OutputYAML { + encodedBytes, err = yaml.JSONToYAML(encodedBytes) + if err != nil { + return fmt.Errorf("yaml marshal: %w", err) + } + } + + if _, err := os.Stdout.Write(encodedBytes); err != nil { + return err + } + case OutputName: + if _, err := os.Stdout.WriteString(userName.Username); err != nil { + return err + } + case OutputValue, "": + header := []string{ + "Username", + "Kubernetes Name", + "Display Name", + "Email", + } + values := [][]string{ + { + userName.Username, + userName.Name, + userName.DisplayName, + userName.Email, + }, + } + + table.PrintTable(cmd.log, header, values) + } + + return nil +} diff --git a/go.mod b/go.mod index 51ac33f95..08fa33580 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( github.com/hashicorp/go-hclog v0.14.1 github.com/hashicorp/go-plugin v1.6.0 github.com/hashicorp/golang-lru/v2 v2.0.2 + github.com/hashicorp/yamux v0.1.1 github.com/invopop/jsonschema v0.12.0 github.com/kubernetes-csi/external-snapshotter/client/v4 v4.2.0 github.com/loft-sh/admin-apis v0.0.0-20240203010124-3600c1c582a8 @@ -25,8 +26,10 @@ require ( github.com/loft-sh/utils v0.0.29 github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d github.com/mitchellh/go-homedir v1.1.0 + github.com/mitchellh/go-testing-interface v1.0.0 github.com/moby/locker v1.0.1 github.com/moby/term v0.5.0 + github.com/oklog/run v1.0.0 github.com/olekukonko/tablewriter v0.0.5 github.com/onsi/ginkgo/v2 v2.15.0 github.com/onsi/gomega v1.31.1 @@ -89,13 +92,10 @@ require ( github.com/google/cel-go v0.17.7 // indirect github.com/google/gnostic-models v0.6.9-0.20230804172637-c7be7c783f49 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0 // indirect - github.com/hashicorp/yamux v0.1.1 // indirect github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213 // indirect github.com/loft-sh/apiserver v0.0.0-20240129130254-7b9a55ab1744 // indirect github.com/loft-sh/jspolicy v0.2.2 // indirect github.com/mattn/go-runewidth v0.0.15 // indirect - github.com/mitchellh/go-testing-interface v1.0.0 // indirect - github.com/oklog/run v1.0.0 // indirect github.com/oklog/ulid v1.3.1 // indirect github.com/otiai10/copy v1.11.0 // indirect github.com/rivo/uniseg v0.4.6 // indirect @@ -123,7 +123,7 @@ require ( github.com/coreos/go-semver v0.3.1 // indirect github.com/coreos/go-systemd/v22 v22.5.0 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect - github.com/evanphx/json-patch v5.8.1+incompatible // indirect + github.com/evanphx/json-patch v5.8.1+incompatible github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect github.com/felixge/httpsnoop v1.0.4 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect @@ -139,7 +139,7 @@ require ( github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect - github.com/golang/protobuf v1.5.3 // indirect + github.com/golang/protobuf v1.5.3 github.com/google/btree v1.1.2 // indirect github.com/google/go-cmp v0.6.0 // indirect github.com/google/go-github/v30 v30.1.0 // indirect