From 7aa3945e9f827a8fc0af9eb34d74e3bdbd24aab3 Mon Sep 17 00:00:00 2001 From: srinandan Date: Thu, 2 Jan 2025 22:55:47 +0000 Subject: [PATCH] feat: adds command to generate cmd tree #337 --- internal/cmd/connectors/crtcustom.go | 6 ++- internal/cmd/integrations/integrations.go | 3 +- internal/cmd/root.go | 2 + internal/cmd/tree/tree.go | 55 +++++++++++++++++++++++ 4 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 internal/cmd/tree/tree.go diff --git a/internal/cmd/connectors/crtcustom.go b/internal/cmd/connectors/crtcustom.go index fbfaff78..daed9b66 100644 --- a/internal/cmd/connectors/crtcustom.go +++ b/internal/cmd/connectors/crtcustom.go @@ -53,8 +53,10 @@ var CrtCustomCmd = &cobra.Command{ Example: `Create a custom connector for OPEN_API type: ` + GetExample(3), } -var labels map[string]string -var connType ConnectorType +var ( + labels map[string]string + connType ConnectorType +) func init() { var name, description, displayName string diff --git a/internal/cmd/integrations/integrations.go b/internal/cmd/integrations/integrations.go index aa2c9396..ed9fff50 100644 --- a/internal/cmd/integrations/integrations.go +++ b/internal/cmd/integrations/integrations.go @@ -25,8 +25,7 @@ var Cmd = &cobra.Command{ Long: "Manage integrations in a GCP project", } -//var userLabel, snapshot string - +// var userLabel, snapshot string var examples = []string{ `integrationcli integrations create -n $name -f samples/sample.json -u $userLabel --default-token`, `integrationcli integrations create -n $name -f samples/sample.json -o samples/sample_overrides.json --default-token`, diff --git a/internal/cmd/root.go b/internal/cmd/root.go index de4f8ee1..b9050480 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -30,6 +30,7 @@ import ( "internal/cmd/sfdcchannels" "internal/cmd/sfdcinstances" "internal/cmd/token" + "internal/cmd/tree" "io" "net/http" "os" @@ -156,6 +157,7 @@ func init() { RootCmd.AddCommand(sfdcchannels.Cmd) RootCmd.AddCommand(endpoints.Cmd) RootCmd.AddCommand(provision.Cmd) + RootCmd.AddCommand(tree.Cmd) } func initConfig() { diff --git a/internal/cmd/tree/tree.go b/internal/cmd/tree/tree.go new file mode 100644 index 00000000..a1098c61 --- /dev/null +++ b/internal/cmd/tree/tree.go @@ -0,0 +1,55 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package tree + +import ( + "internal/clilog" + + "github.com/spf13/cobra" +) + +// Cmd to to print all the commands in integrationcli +var Cmd = &cobra.Command{ + Use: "tree", + Short: "Prints integrationcli command Tree", + Long: "Prints integrationcli command Tree", + RunE: func(cmd *cobra.Command, args []string) (err error) { + cmd.SilenceUsage = true + printCommand(cmd.Parent().Root(), 0, true) + return + }, +} + +func printCommand(cmd *cobra.Command, depth int, last bool) { + var prefix string + if depth > 0 { + if last { + prefix = "└── " + } else { + prefix = "├── " + } + + for i := 0; i < depth-1; i++ { + prefix = (string(rune(0x2502)) + " ") + prefix // Vertical bar + } + } + + clilog.Info.Printf("%s%s\n", prefix, cmd.Use+" - "+cmd.Short) + + for i, subCmd := range cmd.Commands() { + isLast := i == len(cmd.Commands())-1 + printCommand(subCmd, depth+1, isLast) + } +}