/
logs.go
173 lines (160 loc) · 4.21 KB
/
logs.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package appliance
import (
"context"
"errors"
"fmt"
"io"
"mime"
"net/http"
"os"
"path/filepath"
"github.com/appgate/sdpctl/pkg/api"
"github.com/appgate/sdpctl/pkg/cmdappliance"
"github.com/appgate/sdpctl/pkg/docs"
"github.com/appgate/sdpctl/pkg/factory"
"github.com/appgate/sdpctl/pkg/terminal"
"github.com/appgate/sdpctl/pkg/tui"
"github.com/appgate/sdpctl/pkg/util"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/vbauerster/mpb/v8"
"github.com/vbauerster/mpb/v8/decor"
)
type logOpts struct {
cmdappliance.AppliancCmdOpts
Out io.Writer
BaseURL string
HTTPClient func() (*http.Client, error)
SpinnerOut func() io.Writer
Version int
Path string
json bool
}
func NewLogsCmd(f *factory.Factory) *cobra.Command {
aopts := cmdappliance.AppliancCmdOpts{
Appliance: f.Appliance,
Config: f.Config,
CanPrompt: f.CanPrompt(),
}
opts := logOpts{
aopts,
f.IOOutWriter,
f.BaseURL(),
f.CustomHTTPClient,
f.GetSpinnerOutput(),
f.Config.Version,
"",
false,
}
cmd := &cobra.Command{
Use: "logs",
Short: docs.ApplianceLogsDoc.Short,
Long: docs.ApplianceLogsDoc.Short,
Example: docs.ApplianceLogsDoc.ExampleString(),
PreRunE: func(cmd *cobra.Command, args []string) error {
return cmdappliance.ArgsSelectAppliance(cmd, args, &opts.AppliancCmdOpts)
},
RunE: func(c *cobra.Command, args []string) error {
return logsRun(c, args, &opts)
},
}
cmd.Flags().StringVarP(&opts.Path, "path", "", "", "Optional path to write to")
cmd.Flags().BoolVar(&opts.json, "json", false, "Display in JSON format")
return cmd
}
func logsRun(cmd *cobra.Command, args []string, opts *logOpts) error {
terminal.Lock()
defer terminal.Unlock()
client, err := opts.HTTPClient()
if err != nil {
return fmt.Errorf("Could not resolve a HTTP client based on your current config %s", err)
}
path, err := os.Getwd()
if err != nil {
return err
}
if len(opts.Path) > 0 {
path = opts.Path
}
requestURL := fmt.Sprintf("%s/appliances/%s/logs", opts.BaseURL, opts.ApplianceID)
request, err := http.NewRequest(http.MethodGet, requestURL, nil)
if err != nil {
return err
}
request = request.WithContext(context.WithValue(context.Background(), api.ContextAcceptValue, fmt.Sprintf("application/vnd.appgate.peer-v%d+zip", opts.Version)))
request.Close = true
log.Infof("Starting downloading log zip bundle for %s", opts.ApplianceID)
response, err := client.Do(request)
if response == nil || err != nil {
return api.HTTPErrorResponse(response, err)
}
if response.StatusCode != http.StatusOK {
return api.HTTPErrorResponse(response, err)
}
defer response.Body.Close()
name := fmt.Sprintf("%s_logs.zip", opts.ApplianceID)
_, params, err := mime.ParseMediaType(response.Header.Get("Content-Disposition"))
if err == nil {
if v, ok := params["filename"]; ok {
name = v
}
}
file, err := os.Create(filepath.Join(path, name))
if err != nil {
return err
}
p := mpb.New(mpb.WithWidth(64), mpb.WithOutput(opts.SpinnerOut()))
bar := p.New(0,
mpb.SpinnerStyle(tui.SpinnerStyle...),
mpb.BarFillerOnComplete(tui.Check),
mpb.BarWidth(1),
mpb.AppendDecorators(decor.Name(name+" "), decor.CurrentKiloByte("% .1f")),
)
size, err := copy(file, response.Body, bar)
if err != nil {
return err
}
p.Wait()
log.Infof("Downloaded %d bytes zip bundle for %s", size, opts.ApplianceID)
if opts.json {
return util.PrintJSON(opts.Out, map[string]string{"path": file.Name()})
}
fmt.Fprintf(opts.Out, "saved to %s\n", file.Name())
return nil
}
func copy(dst io.Writer, src io.Reader, bar *mpb.Bar) (written int64, err error) {
buf := make([]byte, 32*1024)
for {
nr, er := src.Read(buf)
if nr > 0 {
// increment methods won't trigger complete event because bar was constructed with total = 0
bar.IncrBy(nr)
nw, ew := dst.Write(buf[0:nr])
if nw < 0 || nr < nw {
nw = 0
if ew == nil {
ew = errors.New("Invalid write result")
}
}
written += int64(nw)
if ew != nil {
err = ew
break
}
if nr != nw {
err = io.ErrShortWrite
break
}
}
if er != nil {
if er == io.EOF {
// triggering complete event now
bar.SetTotal(-1, true)
} else if er != io.EOF {
err = er
}
break
}
}
return written, err
}