/
buildings.go
235 lines (223 loc) · 9.02 KB
/
buildings.go
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
Copyright © 2020-2023 Hannes Hayashi
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cmd
import (
"log"
"github.com/hanneshayashi/gsm/gsmhelpers"
"github.com/spf13/cobra"
admin "google.golang.org/api/admin/directory/v1"
)
// buildingsCmd represents the buildings command
var buildingsCmd = &cobra.Command{
Use: "buildings",
Short: "Manage Buildings (Resources) (Part of Admin SDK)",
Long: "Implements the API documented at https://developers.google.com/admin-sdk/directory/reference/rest/v1/resources.buildings",
DisableAutoGenTag: true,
Run: func(cmd *cobra.Command, _ []string) {
err := cmd.Help()
if err != nil {
log.Fatalln(err)
}
},
}
var buildingFlags map[string]*gsmhelpers.Flag = map[string]*gsmhelpers.Flag{
"buildingId": {
AvailableFor: []string{"delete", "get", "insert", "patch"},
Type: "string",
Description: `The ID of the file.`,
Required: []string{"delete", "get", "insert", "patch"},
ExcludeFromAll: true,
},
"customer": {
AvailableFor: []string{"delete", "get", "insert", "list", "patch"},
Type: "string",
Description: `The unique ID for the customer's Workspace account. As an account administrator, you can also use the my_customer alias to represent your account's customer ID.`,
Defaults: map[string]any{"delete": "my_customer", "get": "my_customer", "insert": "my_customer", "list": "my_customer", "patch": "my_customer"},
},
"coordinatesSource": {
AvailableFor: []string{"insert", "patch"},
Type: "string",
Description: `Source from which Building.coordinates are derived.
Acceptable values are:
CLIENT_SPECIFIED - Building.coordinates are set to the coordinates included in the request.
RESOLVED_FROM_ADDRESS - Building.coordinates are automatically populated based on the postal address.
SOURCE_UNSPECIFIED - Defaults to RESOLVED_FROM_ADDRESS if postal address is provided. Otherwise, defaults to CLIENT_SPECIFIED if coordinates are provided. (default)`,
},
"addressLines": {
AvailableFor: []string{"insert", "patch"},
Type: "stringSlice",
Description: `Unstructured address lines describing the lower levels of an address.`,
},
"administrativeArea": {
AvailableFor: []string{"insert", "patch"},
Type: "string",
Description: `Optional. Highest administrative subdivision which is used for postal addresses of a country or region.`,
},
"languageCode": {
AvailableFor: []string{"insert", "patch"},
Type: "string",
Description: `Optional. BCP-47 language code of the contents of this address (if known).`,
},
"locality": {
AvailableFor: []string{"insert", "patch"},
Type: "string",
Description: `Optional. Generally refers to the city/town portion of the address.
Examples: US city, IT comune, UK post town.
In regions of the world where localities are not well defined or do not fit into this structure well, leave locality empty and use addressLines.`,
},
"postalCode": {
AvailableFor: []string{"insert", "patch"},
Type: "string",
Description: `Optional. Postal code of the address.`,
},
"regionCode": {
AvailableFor: []string{"insert", "patch"},
Type: "string",
Description: `Required. CLDR region code of the country/region of the address.`,
Required: []string{"insert"},
},
"sublocality": {
AvailableFor: []string{"insert", "patch"},
Type: "string",
Description: `Optional. Sublocality of the address.`,
},
"buildingName": {
AvailableFor: []string{"insert", "patch"},
Type: "string",
Description: `The building name as seen by users in Calendar.
Must be unique for the customer. For example, "NYC-CHEL".
The maximum length is 100 characters.`,
},
"latitude": {
AvailableFor: []string{"insert", "patch"},
Type: "float64",
Description: `Latitude in decimal degrees.`,
},
"longitude": {
AvailableFor: []string{"insert", "patch"},
Type: "float64",
Description: `Longitude in decimal degrees.`,
},
"description": {
AvailableFor: []string{"insert", "patch"},
Type: "string",
Description: `A brief description of the building. For example, "Chelsea Market".`,
},
"floorNames": {
AvailableFor: []string{"insert", "patch"},
Type: "stringSlice",
Description: `The display names for all floors in this building.
The floors are expected to be sorted in ascending order, from lowest floor to highest floor.
For example, ["B2", "B1", "L", "1", "2", "2M", "3", "PH"] Must contain at least one entry.`,
Required: []string{"insert"},
},
"fields": {
AvailableFor: []string{"get", "insert", "list", "patch"},
Type: "string",
Description: `Fields allows partial responses to be retrieved.
See https://developers.google.com/gdata/docs/2.0/basics#PartialResponse for more information.`,
},
}
var buildingFlagsALL = gsmhelpers.GetAllFlags(buildingFlags)
func init() {
rootCmd.AddCommand(buildingsCmd)
}
func mapToBuilding(flags map[string]*gsmhelpers.Value) (*admin.Building, error) {
building := &admin.Building{}
if flags["buildingId"].IsSet() {
building.BuildingId = flags["buildingId"].GetString()
if building.BuildingId == "" {
building.ForceSendFields = append(building.ForceSendFields, "BuildingId")
}
}
if flags["addressLines"].IsSet() || flags["administrativeArea"].IsSet() || flags["languageCode"].IsSet() || flags["locality"].IsSet() || flags["postalCode"].IsSet() || flags["regionCode"].IsSet() || flags["sublocality"].IsSet() {
building.Address = &admin.BuildingAddress{}
if flags["addressLines"].IsSet() {
building.Address.AddressLines = flags["addressLines"].GetStringSlice()
if len(building.Address.AddressLines) == 0 {
building.Address.ForceSendFields = append(building.Address.ForceSendFields, "AddressLines")
}
}
if flags["administrativeArea"].IsSet() {
building.Address.AdministrativeArea = flags["administrativeArea"].GetString()
if building.Address.AdministrativeArea == "" {
building.Address.ForceSendFields = append(building.Address.ForceSendFields, "AdministrativeArea")
}
}
if flags["languageCode"].IsSet() {
building.Address.LanguageCode = flags["languageCode"].GetString()
if building.Address.LanguageCode == "" {
building.Address.ForceSendFields = append(building.Address.ForceSendFields, "LanguageCode")
}
}
if flags["locality"].IsSet() {
building.Address.Locality = flags["locality"].GetString()
if building.Address.Locality == "" {
building.Address.ForceSendFields = append(building.Address.ForceSendFields, "Locality")
}
}
if flags["postalCode"].IsSet() {
building.Address.PostalCode = flags["postalCode"].GetString()
if building.Address.PostalCode == "" {
building.Address.ForceSendFields = append(building.Address.ForceSendFields, "PostalCode")
}
}
if flags["regionCode"].IsSet() {
building.Address.RegionCode = flags["regionCode"].GetString()
if building.Address.RegionCode == "" {
building.Address.ForceSendFields = append(building.Address.ForceSendFields, "RegionCode")
}
}
if flags["sublocality"].IsSet() {
building.Address.Sublocality = flags["sublocality"].GetString()
if building.Address.Sublocality == "" {
building.Address.ForceSendFields = append(building.Address.ForceSendFields, "Sublocality")
}
}
}
if flags["buildingName"].IsSet() {
building.BuildingName = flags["buildingName"].GetString()
if building.BuildingName == "" {
building.ForceSendFields = append(building.ForceSendFields, "BuildingName")
}
}
if flags["latitude"].IsSet() || flags["longitude"].IsSet() {
building.Coordinates = &admin.BuildingCoordinates{}
if flags["latitude"].IsSet() {
building.Coordinates.Latitude = flags["latitude"].GetFloat64()
if building.Coordinates.Latitude == 0.0 {
building.Coordinates.ForceSendFields = append(building.Coordinates.ForceSendFields, "Latitude")
}
}
if flags["longitude"].IsSet() {
building.Coordinates.Longitude = flags["longitude"].GetFloat64()
if building.Coordinates.Longitude == 0.0 {
building.Coordinates.ForceSendFields = append(building.Coordinates.ForceSendFields, "Longitude")
}
}
}
if flags["description"].IsSet() {
building.Description = flags["description"].GetString()
if building.Description == "" {
building.ForceSendFields = append(building.ForceSendFields, "Description")
}
}
if flags["floorNames"].IsSet() {
building.FloorNames = flags["floorNames"].GetStringSlice()
if len(building.FloorNames) == 0 {
building.ForceSendFields = append(building.ForceSendFields, "FloorNames")
}
}
return building, nil
}