-
Notifications
You must be signed in to change notification settings - Fork 2
/
get.go
98 lines (77 loc) · 1.96 KB
/
get.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
package client
import (
"io"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/cheggaaa/pb"
"github.com/pkg/errors"
)
type GetOptions struct {
SkipAuthRetry bool
Quiet bool
RemoteFile string
LocalFile string
}
func (s Service) Get(opts GetOptions) error {
client, err := s.GetClient(opts.SkipAuthRetry)
if err != nil {
return errors.Wrap(err, "getting client")
}
if strings.Contains(opts.RemoteFile, "*") || strings.Contains(opts.RemoteFile, "?") {
listed, err := client.GetList()
if err != nil {
return errors.Wrap(err, "listing files")
}
var matched bool
for _, listedMatch := range listed.Files {
matches, err := filepath.Match(opts.RemoteFile, listedMatch.Name)
if matches && err == nil {
if matched {
return errors.Wrapf(err, "multiple matches found for: %s", opts.RemoteFile)
}
matched = true
opts.RemoteFile = listedMatch.Name
}
}
}
localFilePath := opts.LocalFile
if localFilePath == "" {
localFilePath = path.Base(opts.RemoteFile)
}
var file io.ReadWriteSeeker
if localFilePath == "-" {
fileTemp, err := s.fs.TempFile("ssoca-download-stdout")
if err != nil {
return errors.Wrap(err, "creating temp file for stdout")
}
defer os.RemoveAll(fileTemp.Name())
file = fileTemp
} else {
file, err = s.fs.OpenFile(localFilePath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0600)
if err != nil {
return errors.Wrap(err, "opening file")
}
}
downloadStatus := pb.New(0).SetRefreshRate(250 * time.Millisecond).SetWidth(80)
downloadStatus.Output = s.runtime.GetStderr()
downloadStatus.ShowPercent = false
err = client.Download(opts.RemoteFile, file, downloadStatus)
if err != nil {
return err
}
if localFilePath != "-" {
return nil
}
_, err = file.Seek(0, io.SeekStart)
if err != nil {
return errors.Wrap(err, "seeking downloaded temp file")
}
_, err = io.Copy(s.runtime.GetStdout(), file)
if err != nil {
return errors.Wrap(err, "writing download to STDOUT")
}
return nil
}