Skip to content

Commit

Permalink
Merge pull request #338 from GoogleCloudPlatform/issue337
Browse files Browse the repository at this point in the history
feat: adds command to generate cmd tree #337
  • Loading branch information
srinandan authored Jan 3, 2025
2 parents 83075f5 + 7aa3945 commit c1819c2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"internal/cmd/sfdcchannels"
"internal/cmd/sfdcinstances"
"internal/cmd/token"
"internal/cmd/tree"
"io"
"net/http"
"os"
Expand Down Expand Up @@ -156,6 +157,7 @@ func init() {
RootCmd.AddCommand(sfdcchannels.Cmd)
RootCmd.AddCommand(endpoints.Cmd)
RootCmd.AddCommand(provision.Cmd)
RootCmd.AddCommand(tree.Cmd)
}

func initConfig() {
Expand Down
55 changes: 55 additions & 0 deletions internal/cmd/tree/tree.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit c1819c2

Please sign in to comment.