This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
/
main.go
71 lines (59 loc) · 2.01 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
package main
import (
"fmt"
"github.com/almighty/almighty-core/client"
"github.com/almighty/almighty-core/tool/cli"
goaclient "github.com/goadesign/goa/client"
"github.com/spf13/cobra"
"net/http"
"os"
"time"
)
func main() {
// Create command line parser
app := &cobra.Command{
Use: "alm-cli",
Short: `CLI client for the alm service`,
}
// Create client struct
httpClient := newHTTPClient()
c := client.New(httpClient)
// Register global flags
app.PersistentFlags().StringVarP(&c.Scheme, "scheme", "s", "", "Set the requests scheme")
app.PersistentFlags().StringVarP(&c.Host, "host", "H", "almighty.io", "API hostname")
app.PersistentFlags().DurationVarP(&httpClient.Timeout, "timeout", "t", time.Duration(20)*time.Second, "Set the request timeout")
app.PersistentFlags().BoolVar(&c.Dump, "dump", false, "Dump HTTP request and response.")
// Register signer flags
var key, format string
app.PersistentFlags().StringVar(&key, "key", "", "API key used for authentication")
app.PersistentFlags().StringVar(&format, "format", "Bearer %s", "Format used to create auth header or query from key")
// Parse flags and setup signers
app.ParseFlags(os.Args)
jwtSigner := newJWTSigner(key, format)
// Initialize API client
c.SetJWTSigner(jwtSigner)
c.UserAgent = "alm-cli/1.0"
// Register API commands
cli.RegisterCommands(app, c)
// Execute!
if err := app.Execute(); err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(-1)
}
}
// newHTTPClient returns the HTTP client used by the API client to make requests to the service.
func newHTTPClient() *http.Client {
// TBD: Change as needed (e.g. to use a different transport to control redirection policy or
// disable cert validation or...)
return http.DefaultClient
}
// newJWTSigner returns the request signer used for authenticating
// against the jwt security scheme.
func newJWTSigner(key, format string) goaclient.Signer {
return &goaclient.APIKeySigner{
SignQuery: false,
KeyName: "Authorization",
KeyValue: key,
Format: format,
}
}