-
Notifications
You must be signed in to change notification settings - Fork 31
/
upload.go
120 lines (102 loc) · 2.95 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
package deploy
import (
"os"
"strings"
msg "github.com/aziontech/azion-cli/messages/deploy"
"github.com/aziontech/azion-cli/pkg/api/storage"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"github.com/aziontech/azion-cli/pkg/contracts"
"github.com/aziontech/azion-cli/pkg/logger"
"github.com/schollz/progressbar/v3"
"github.com/zRedShift/mimemagic"
"go.uber.org/zap"
)
var (
PathStatic = ".edge/storage"
Jobs chan contracts.FileOps
Retries int64
)
func (cmd *DeployCmd) uploadFiles(f *cmdutil.Factory, conf *contracts.AzionApplicationOptions) error {
// Get total amount of files to display progress
totalFiles := 0
if err := cmd.FilepathWalk(PathStatic, func(path string, info os.FileInfo, err error) error {
if err != nil {
logger.Debug("Error while reading files to be uploaded", zap.Error(err))
logger.Debug("File that caused the error: " + PathStatic)
return err
}
if !info.IsDir() {
totalFiles++
}
return nil
}); err != nil {
logger.Debug("Error while reading files to be uploaded", zap.Error(err))
return err
}
clientUpload := storage.NewClient(cmd.F.HttpClient, cmd.F.Config.GetString("storage_url"), cmd.F.Config.GetString("token"))
logger.FInfo(cmd.F.IOStreams.Out, msg.UploadStart)
noOfWorkers := 5
var currentFile int64
Jobs := make(chan contracts.FileOps, totalFiles)
results := make(chan error, noOfWorkers)
// Create worker goroutines
for i := 1; i <= noOfWorkers; i++ {
go worker(Jobs, results, ¤tFile, clientUpload, conf)
}
bar := progressbar.NewOptions(
totalFiles,
progressbar.OptionSetDescription("Uploading files"),
progressbar.OptionShowCount(),
progressbar.OptionSetWriter(cmd.F.IOStreams.Out),
progressbar.OptionClearOnFinish(),
)
if f.Silent {
bar = nil
}
if err := cmd.FilepathWalk(PathStatic, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
fileContent, err := cmd.Open(path)
if err != nil {
logger.Debug("Error while trying to read file <"+path+"> about to be uploaded", zap.Error(err))
return err
}
fileString := strings.TrimPrefix(path, PathStatic)
mimeType, err := mimemagic.MatchFilePath(path, -1)
if err != nil {
logger.Debug("Error while matching file path", zap.Error(err))
return err
}
fileOptions := contracts.FileOps{
Path: fileString,
MimeType: mimeType.MediaType(),
FileContent: fileContent,
}
Jobs <- fileOptions
}
return nil
}); err != nil {
logger.Debug("Error while reading files to be uploaded", zap.Error(err))
return err
}
close(Jobs)
// Check for errors from workers
for a := 1; a <= totalFiles; a++ {
result := <-results
if result != nil {
return result
}
if bar != nil {
err := bar.Set(int(currentFile))
if err != nil {
return err
}
}
}
// All jobs are processed, no more values will be sent on results:
close(results)
logger.FInfo(cmd.F.IOStreams.Out, msg.UploadSuccessful)
return nil
}