-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcli.go
More file actions
121 lines (108 loc) · 3.75 KB
/
Copy pathcli.go
File metadata and controls
121 lines (108 loc) · 3.75 KB
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
package goauth
import (
"context"
"io"
"net/http"
"time"
"github.com/grokify/mogo/errors/errorsutil"
"github.com/grokify/mogo/fmt/fmtutil"
"github.com/grokify/mogo/net/http/httpsimple"
"github.com/grokify/mogo/net/http/httputilmore"
"github.com/grokify/mogo/type/maputil"
"github.com/jessevdk/go-flags"
)
// Options is a struct to be used with `ParseOptions()` or `github.com/jessevdk/go-flags`.
// It can be embedded in another struct and used directly with `github.com/jessevdk/go-flags`.
type Options struct {
CredsPath string `long:"creds" description:"Environment File Path"`
Account string `long:"account" description:"Environment Variable Name"`
Token string `long:"token" description:"Token"`
CLI []bool `long:"cli" description:"CLI"`
}
func NewClientCmd(ctx context.Context, state string) (*http.Client, error) {
if opts, err := ParseOptions(); err != nil {
return nil, err
} else {
return opts.NewClient(ctx, state)
}
}
func ParseOptions() (*Options, error) {
opts := &Options{}
_, err := flags.Parse(opts)
return opts, err
}
func (opts *Options) Credentials() (Credentials, error) {
return NewCredentialsFromSetFile(opts.CredsPath, opts.Account, false)
}
func (opts *Options) CredentialsSet(inflateEndpoints bool) (*CredentialsSet, error) {
return ReadFileCredentialsSet(opts.CredsPath, inflateEndpoints)
}
func (opts *Options) NewClient(ctx context.Context, state string) (*http.Client, error) {
if opts.CredsPath == "" && opts.Account == "" {
return &http.Client{}, nil
} else if creds, err := opts.Credentials(); err != nil {
return nil, errorsutil.Wrap(err, "error in `goauth.Options.NewClient() call to self as `goauth.Options.Credentials()`")
} else if opts.UseCLI() {
if state == "" {
state = time.Now().Format(time.RFC3339)
}
return creds.NewClientCLI(ctx, state)
} else {
return creds.NewClient(ctx)
}
}
func (opts *Options) UseCLI() bool {
return len(opts.CLI) > 0
}
// CLIRequest will get a token using `goauth` and then execute the provided request
// parameters with the credential, e.g. OAuth 2.0 access token.
type CLIRequest struct {
Options
Request httpsimple.CLI
}
func (cli CLIRequest) Do(ctx context.Context, state string, w io.Writer) error {
if clt, err := cli.NewClient(ctx, state); err != nil {
return errorsutil.WrapWithLocation(err)
} else if sr, err := cli.Request.Request(); err != nil {
return errorsutil.WrapWithLocation(err)
} else {
sc := httpsimple.NewClient(clt, "")
resp, err := sc.Do(ctx, sr)
if err != nil {
return errorsutil.WrapWithLocation(err)
}
if _, err := fmtutil.FprintfIf(w, "Response Status Code: %d\n", resp.StatusCode); err != nil {
return errorsutil.WrapWithLocation(err)
}
if _, err := fmtutil.FprintfIf(w, "===== BEGIN RESPONSE META =====\nStatus Code: %d\n===== END RESPONSE META =====\n", resp.StatusCode); err != nil {
return errorsutil.WrapWithLocation(err)
}
if _, err := fmtutil.FprintIf(w, "===== BEGIN RESPONSE HEADERS =====\n"); err != nil {
return errorsutil.WrapWithLocation(err)
}
hkeys := maputil.Keys(resp.Header)
for _, header := range hkeys {
if v, ok := resp.Header[header]; ok {
for _, vi := range v {
if _, err := fmtutil.FprintfIf(w, "%s: %s\n", header, vi); err != nil {
return errorsutil.WrapWithLocation(err)
}
}
}
}
if _, err := fmtutil.FprintIf(w, "===== END RESPONSE HEADERS =====\n"); err != nil {
return errorsutil.WrapWithLocation(err)
}
b, err := httputilmore.ResponseBodyMore(resp, "", " ")
if err != nil {
return errorsutil.WrapWithLocation(err)
} else {
if w != nil {
if _, err := fmtutil.FprintfIf(w, "===== BEGIN RESPONSE BODY =====\n%s\n===== END RESPONSE BODY =====", string(b)); err != nil {
return errorsutil.WrapWithLocation(err)
}
}
return nil
}
}
}