-
Notifications
You must be signed in to change notification settings - Fork 2
/
upload.go
172 lines (154 loc) · 4.08 KB
/
upload.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
package files
import (
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
"github.com/AlecAivazis/survey/v2"
apipkg "github.com/appgate/sdpctl/pkg/api"
"github.com/appgate/sdpctl/pkg/configuration"
"github.com/appgate/sdpctl/pkg/docs"
"github.com/appgate/sdpctl/pkg/factory"
"github.com/appgate/sdpctl/pkg/files"
"github.com/appgate/sdpctl/pkg/filesystem"
"github.com/appgate/sdpctl/pkg/prompt"
"github.com/appgate/sdpctl/pkg/tui"
"github.com/appgate/sdpctl/pkg/util"
"github.com/hashicorp/go-multierror"
"github.com/spf13/cobra"
)
type uploadOptions struct {
config *configuration.Config
out io.Writer
ciMode bool
filePaths map[string]string
}
func NewFilesUploadCmd(f *factory.Factory) *cobra.Command {
opts := uploadOptions{
config: f.Config,
out: f.IOOutWriter,
filePaths: map[string]string{},
}
uploadCMD := &cobra.Command{
Use: "upload",
Aliases: []string{"up"},
Short: docs.FilesUploadDocs.Short,
Long: docs.FilesUploadDocs.Long,
Example: docs.FilesUploadDocs.ExampleString(),
Args: cobra.MinimumNArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
var errs *multierror.Error
var err error
if opts.ciMode, err = cmd.Flags().GetBool("ci-mode"); err != nil {
return err
}
for _, arg := range args {
pathSlice := strings.Split(arg, "=")
path := filesystem.AbsolutePath(pathSlice[0])
var rename string
if len(pathSlice) > 1 {
rename = filepath.Base(pathSlice[1])
}
ok, err := util.FileExists(path)
if err != nil {
errs = multierror.Append(errs, err)
continue
}
if !ok {
errs = multierror.Append(errs, fmt.Errorf("file does not exist: %s", path))
continue
}
opts.filePaths[path] = rename
}
return errs.ErrorOrNil()
},
RunE: func(cmd *cobra.Command, args []string) error {
var err error
api, err := f.Appliance(f.Config)
if err != nil {
return err
}
ctx := context.Background()
filesAPI := files.FileManager{API: api}
if !opts.ciMode {
filesAPI.Progress = tui.New(ctx, f.SpinnerOut)
}
tempPaths := map[string]string{}
for path, rename := range opts.filePaths {
name := filepath.Base(path)
if len(rename) > 0 {
name = rename
}
_, err := api.FileStatus(ctx, name)
if err != nil {
if errors.Is(err, apipkg.ErrFileNotFound) {
tempPaths[path] = rename
continue
}
return err
}
// no error means file already exists in the repository
if f.CanPrompt() {
p := &survey.Confirm{
Message: fmt.Sprintf("%s already exists. Overwrite?", name),
}
var overwrite bool
if err := prompt.SurveyAskOne(p, &overwrite); err != nil {
return err
}
if !overwrite {
continue
}
}
tempPaths[path] = rename
}
if len(tempPaths) <= 0 {
fmt.Fprintln(f.IOOutWriter, "No files to upload.")
return nil
}
// Delete all files that are being uploaded first
for path := range tempPaths {
name := filepath.Base(path)
api.DeleteFile(ctx, name)
}
errChan := make(chan error)
var wg sync.WaitGroup
var errs *multierror.Error
fmt.Fprintf(f.IOOutWriter, "Uploading %d file(s):\n\n", len(tempPaths))
for f, rename := range tempPaths {
file, err := os.Open(f)
if err != nil {
errs = multierror.Append(errs, err)
continue
}
wg.Add(1)
go func(ctx context.Context, wg *sync.WaitGroup, err chan<- error, f *os.File, filesAPI *files.FileManager, rename string) {
defer func() {
wg.Done()
f.Close()
}()
err <- filesAPI.Upload(ctx, files.QueueItem{File: f, RemoteName: rename})
}(ctx, &wg, errChan, file, &filesAPI, rename)
}
go func(wg *sync.WaitGroup, errChan chan<- error) {
wg.Wait()
close(errChan)
}(&wg, errChan)
for result := range errChan {
if result != nil {
errs = multierror.Append(errs, result)
}
}
if filesAPI.Progress != nil {
filesAPI.Progress.Wait()
}
fmt.Fprintln(f.IOOutWriter, "\nFile upload complete!")
return errs.ErrorOrNil()
},
}
return uploadCMD
}