-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathcommand_tree_builder.go
More file actions
142 lines (121 loc) · 3.53 KB
/
command_tree_builder.go
File metadata and controls
142 lines (121 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2026 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
//
// https://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 gcloud
import (
"fmt"
"strings"
"github.com/googleapis/librarian/internal/sidekick/api"
"github.com/googleapis/librarian/internal/surfer/gcloud/provider"
)
type commandTreeBuilder struct {
model *api.API
config *provider.Config
}
func newCommandTreeBuilder(model *api.API, config *provider.Config) *commandTreeBuilder {
return &commandTreeBuilder{
model: model,
config: config,
}
}
func (b *commandTreeBuilder) build() (*CommandGroupsByTrack, error) {
tree := &CommandGroupsByTrack{}
for _, service := range b.model.Services {
groupBuilder, err := newCommandGroupBuilder(b.model, service, b.config)
if err != nil {
return nil, err
}
track := strings.ToUpper(provider.InferTrackFromPackage(service.Package))
root, err := b.root(tree, track, groupBuilder)
if err != nil {
return nil, err
}
for _, method := range service.Methods {
if err := b.insert(root, groupBuilder, method); err != nil {
return nil, err
}
}
}
return tree, nil
}
// insert traverses the tree and attaches a command leaf node. It resolves the
// literal path segments of the method and walks the tree, creating missing
// groups if they do not yet exist.
func (b *commandTreeBuilder) insert(root *CommandGroup, groupBuilder *commandGroupBuilder, method *api.Method) error {
if provider.IsSingletonResourceMethod(method, b.model) {
return nil
}
binding := provider.PrimaryBinding(method)
if binding == nil {
return nil
}
segments := provider.GetLiteralSegments(binding.PathTemplate.Segments)
if len(segments) == 0 {
return nil
}
curr := root
for i, seg := range segments {
if b.isTerminatedSegment(seg) {
return nil
}
isLeaf := i == len(segments)-1
if b.isFlattenedSegment(seg) && !isLeaf {
continue
}
if curr.Groups[seg] == nil {
curr.Groups[seg] = groupBuilder.build(segments, i)
}
curr = curr.Groups[seg]
}
cmd, err := newCommandBuilder(method, b.config, b.model, groupBuilder.service).build()
if err != nil {
return err
}
curr.Commands[cmd.Name] = cmd
return nil
}
func (b *commandTreeBuilder) root(tree *CommandGroupsByTrack, track string, groupBuilder *commandGroupBuilder) (*CommandGroup, error) {
switch track {
case "GA":
if tree.GA == nil {
tree.GA = groupBuilder.buildRoot()
}
return tree.GA, nil
case "BETA":
if tree.BETA == nil {
tree.BETA = groupBuilder.buildRoot()
}
return tree.BETA, nil
case "ALPHA":
if tree.ALPHA == nil {
tree.ALPHA = groupBuilder.buildRoot()
}
return tree.ALPHA, nil
default:
return nil, fmt.Errorf("unknown track %q", track)
}
}
var flattenedSegments = map[string]bool{
"projects": true,
"locations": true,
"zones": true,
"regions": true,
"folders": true,
"organizations": true,
}
func (b *commandTreeBuilder) isFlattenedSegment(lit string) bool {
return flattenedSegments[lit]
}
func (b *commandTreeBuilder) isTerminatedSegment(lit string) bool {
return lit == "operations" && !provider.ShouldGenerateOperations(b.config)
}