-
Notifications
You must be signed in to change notification settings - Fork 19
/
x.go
61 lines (49 loc) · 1.59 KB
/
x.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
package x
import (
"os"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/exoscale/openapi-cli-generator/cli"
logger "github.com/izumin5210/gentleman-logger"
"github.com/spf13/cobra"
"gopkg.in/h2non/gentleman.v2/context"
"gopkg.in/h2non/gentleman.v2/plugin"
)
// InitCommand initializes the code-generated CLI instance and returns the resulting Cobra command
// to the caller so it can be added as subcommand to a higher level CLI.
func InitCommand() *cobra.Command {
cli.Init(&cli.Config{
AppName: "x",
Caching: false,
})
cli.AddGlobalFlag("zone", "z", "Exoscale zone", "")
cli.AddGlobalFlag("environment", "e", "Exoscale API environment", "")
if _, ok := os.LookupEnv("EXOSCALE_TRACE"); ok {
cli.Client.Use(logger.New(os.Stderr))
}
xRegister(false)
return cli.Root
}
// SetClientCredentials adds a pre-request hook to sign outgoing requests using specified API credentials.
func SetClientCredentials(apiKey, apiSecret string) error {
security, err := exoapi.NewSecurityProvider(apiKey, apiSecret)
if err != nil {
return err
}
// Intercept the outgoing API request and sign it
cli.Client.Use(plugin.NewPhasePlugin("before dial",
func(ctx *context.Context, h context.Handler) {
if err := security.Intercept(ctx, ctx.Request); err != nil {
panic(err)
}
h.Next(ctx)
}),
)
return nil
}
// SetClientUserAgent adds a pre-request hook to set the User-Agent header value on outgoing HTTP requests.
func SetClientUserAgent(ua string) {
cli.Client.UseRequest(func(ctx *context.Context, h context.Handler) {
ctx.Request.Header.Set("User-Agent", ua)
h.Next(ctx)
})
}