-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
77 lines (64 loc) · 1.71 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"io"
"net/http"
"os"
"go.artefactual.dev/ssclient"
"go.artefactual.dev/ssclient/kiota/api"
)
const usage = "Usage: example -url=http://127.0.0.1:62081 -user=test -key=test"
func main() {
ctx := context.Background()
if err := run(ctx, os.Stdout, os.Args[1:]); err == flag.ErrHelp {
fmt.Fprintf(os.Stderr, "%s\n", usage)
os.Exit(2)
} else if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}
func run(ctx context.Context, stdout io.Writer, args []string) error {
if len(args) < 2 {
return flag.ErrHelp
}
var (
url string
user string
key string
)
flag.StringVar(&url, "url", "", "url, e.g. http://127.0.0.1:62081")
flag.StringVar(&user, "user", "", "user, e.g.: test")
flag.StringVar(&key, "key", "", "key, e.g.: test")
if err := flag.CommandLine.Parse(args); err != nil {
return err
}
client, err := ssclient.New(&http.Client{}, url, user, key)
if err != nil {
return fmt.Errorf("create ssclient: %v", err)
}
app := application{
client: client.Api().V2(),
stdout: stdout,
}
return app.locations(ctx)
}
type application struct {
client *api.V2RequestBuilder
stdout io.Writer
}
// locations prints a list of locations found in the storage server.
func (app application) locations(ctx context.Context) error {
reqConfig := &api.V2LocationRequestBuilderGetRequestConfiguration{}
listable, err := app.client.Location().Get(ctx, reqConfig)
if err != nil {
return err
}
fmt.Fprintf(app.stdout, "Found %d locations!\n", *listable.GetMeta().GetTotalCount())
for _, location := range listable.GetObjects() {
fmt.Fprintf(app.stdout, "» Location %s with purpose %s.\n", *location.GetUuid(), location.GetPurpose())
}
return nil
}