-
Notifications
You must be signed in to change notification settings - Fork 110
/
dataset.go
152 lines (138 loc) · 4.4 KB
/
dataset.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
package cli
import (
"context"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
datasetpb "go.viam.com/api/app/dataset/v1"
)
const (
datasetFlagName = "name"
datasetFlagDatasetID = "dataset-id"
datasetFlagDatasetIDs = "dataset-ids"
dataFlagLocationID = "location-id"
dataFlagFileIDs = "file-ids"
)
// DatasetCreateAction is the corresponding action for 'dataset create'.
func DatasetCreateAction(c *cli.Context) error {
client, err := newViamClient(c)
if err != nil {
return err
}
if err := client.createDataset(c.String(generalFlagOrgID), c.String(datasetFlagName)); err != nil {
return err
}
return nil
}
// createDataset creates a dataset with the a dataset ID.
func (c *viamClient) createDataset(orgID, datasetName string) error {
if err := c.ensureLoggedIn(); err != nil {
return err
}
resp, err := c.datasetClient.CreateDataset(context.Background(),
&datasetpb.CreateDatasetRequest{OrganizationId: orgID, Name: datasetName})
if err != nil {
return errors.Wrapf(err, "received error from server")
}
printf(c.c.App.Writer, "Created dataset %s with dataset ID: %s", datasetName, resp.GetId())
return nil
}
// DatasetRenameAction is the corresponding action for 'dataset rename'.
func DatasetRenameAction(c *cli.Context) error {
client, err := newViamClient(c)
if err != nil {
return err
}
if err := client.renameDataset(c.String(datasetFlagDatasetID), c.String(datasetFlagName)); err != nil {
return err
}
return nil
}
// renameDataset renames an existing datasetID with the newDatasetName.
func (c *viamClient) renameDataset(datasetID, newDatasetName string) error {
if err := c.ensureLoggedIn(); err != nil {
return err
}
_, err := c.datasetClient.RenameDataset(context.Background(),
&datasetpb.RenameDatasetRequest{Id: datasetID, Name: newDatasetName})
if err != nil {
return errors.Wrapf(err, "received error from server")
}
printf(c.c.App.Writer, "Dataset with ID %s renamed to %s", datasetID, newDatasetName)
return nil
}
// DatasetListAction is the corresponding action for 'dataset list'.
func DatasetListAction(c *cli.Context) error {
client, err := newViamClient(c)
if err != nil {
return err
}
datasetIDs := c.StringSlice(datasetFlagDatasetIDs)
orgID := c.String(generalFlagOrgID)
if orgID != "" && datasetIDs != nil {
return errors.New("must specify either dataset IDs or organization ID, got both")
}
if datasetIDs != nil {
if err := client.listDatasetByIDs(datasetIDs); err != nil {
return err
}
} else {
if err := client.listDatasetByOrg(orgID); err != nil {
return err
}
}
return nil
}
// listDatasetByIDs list all datasets by ID.
func (c *viamClient) listDatasetByIDs(datasetIDs []string) error {
if err := c.ensureLoggedIn(); err != nil {
return err
}
resp, err := c.datasetClient.ListDatasetsByIDs(context.Background(),
&datasetpb.ListDatasetsByIDsRequest{Ids: datasetIDs})
if err != nil {
return errors.Wrapf(err, "received error from server")
}
for _, dataset := range resp.GetDatasets() {
printf(c.c.App.Writer, "\t%s (ID: %s, Organization ID: %s)", dataset.Name, dataset.Id, dataset.OrganizationId)
}
return nil
}
// listDatasetByOrg list all datasets for the specified org ID.
func (c *viamClient) listDatasetByOrg(orgID string) error {
if err := c.ensureLoggedIn(); err != nil {
return err
}
resp, err := c.datasetClient.ListDatasetsByOrganizationID(context.Background(),
&datasetpb.ListDatasetsByOrganizationIDRequest{OrganizationId: orgID})
if err != nil {
return errors.Wrapf(err, "received error from server")
}
for _, dataset := range resp.GetDatasets() {
printf(c.c.App.Writer, "\t%s (ID: %s, Organization ID: %s)", dataset.Name, dataset.Id, dataset.OrganizationId)
}
return nil
}
// DatasetDeleteAction is the corresponding action for 'dataset rename'.
func DatasetDeleteAction(c *cli.Context) error {
client, err := newViamClient(c)
if err != nil {
return err
}
if err := client.deleteDataset(c.String(datasetFlagDatasetID)); err != nil {
return err
}
return nil
}
// deleteDataset deletes a dataset with the specified ID.
func (c *viamClient) deleteDataset(datasetID string) error {
if err := c.ensureLoggedIn(); err != nil {
return err
}
_, err := c.datasetClient.DeleteDataset(context.Background(),
&datasetpb.DeleteDatasetRequest{Id: datasetID})
if err != nil {
return errors.Wrapf(err, "received error from server")
}
printf(c.c.App.Writer, "Dataset with ID %s deleted", datasetID)
return nil
}