-
-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathMain.go
More file actions
202 lines (179 loc) · 4.84 KB
/
Main.go
File metadata and controls
202 lines (179 loc) · 4.84 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"archive/zip"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"github.com/forceu/gokapi/cmd/cli-uploader/cliapi"
"github.com/forceu/gokapi/cmd/cli-uploader/cliconfig"
"github.com/forceu/gokapi/cmd/cli-uploader/cliconstants"
"github.com/forceu/gokapi/cmd/cli-uploader/cliflags"
"github.com/forceu/gokapi/internal/environment"
"github.com/forceu/gokapi/internal/helper"
"github.com/schollz/progressbar/v3"
)
func main() {
mode := cliflags.Parse()
switch mode {
case cliflags.ModeLogin:
doLogin()
case cliflags.ModeLogout:
doLogout()
case cliflags.ModeUpload:
processUpload(cliflags.ModeUpload)
case cliflags.ModeArchive:
processUpload(cliflags.ModeArchive)
case cliflags.ModeDownload:
processDownload()
case cliflags.ModeInvalid:
os.Exit(3)
}
}
func doLogin() {
checkDockerFolders()
cliconfig.CreateLogin()
}
func processUpload(mode int) {
cliconfig.Load()
uploadParam := cliflags.GetUploadParameters(mode)
if mode == cliflags.ModeArchive {
zipPath, err := zipFolder(uploadParam.Directory, uploadParam.TmpFolder, !uploadParam.JsonOutput)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
uploadParam.File = zipPath
defer deleteTempFolder(zipPath) // ensure cleanup
}
// Perform the upload
result, err := cliapi.UploadFile(uploadParam)
if err != nil {
fmt.Println()
if errors.Is(cliapi.ErrUnauthorised, err) {
fmt.Println("ERROR: Unauthorised API key. Please re-run login or make sure that the API key has the permission to upload files.")
} else {
fmt.Println("ERROR: Could not upload file")
fmt.Println(err)
}
os.Exit(1)
}
// Output result
if uploadParam.JsonOutput {
jsonStr, _ := json.Marshal(result)
fmt.Println(string(jsonStr))
return
}
fmt.Println("Upload successful")
fmt.Println("File Name: " + result.Name)
fmt.Println("File ID: " + result.Id)
fmt.Println("File Download URL: " + result.UrlDownload)
}
func processDownload() {
cliconfig.Load()
uploadParam := cliflags.GetUploadParameters(cliflags.ModeDownload)
// Perform the download
err := cliapi.DownloadFile(uploadParam)
if err != nil {
fmt.Println()
if errors.Is(cliapi.ErrUnauthorised, err) {
fmt.Println("ERROR: Unauthorised API key. Please re-run login or make sure that the API key has the permission to download files.")
} else {
fmt.Println("ERROR: Could not download file")
fmt.Println(err)
}
os.Exit(1)
}
}
func doLogout() {
err := cliconfig.Delete()
if err != nil {
fmt.Println("ERROR: Could not delete configuration file")
fmt.Println(err)
os.Exit(2)
}
fmt.Println("Logged out. To login again, run: gokapi-cli login")
}
func checkDockerFolders() {
if !environment.IsDockerInstance() {
return
}
_, isDefault := cliflags.GetConfigLocation()
if !isDefault {
return
}
if !helper.FolderExists(cliconstants.DockerFolderConfig) {
fmt.Println("Warning: Docker folder does not exist, configuration will be lost when creating a new container")
helper.CreateDir(cliconstants.DockerFolderConfig)
}
if !helper.FolderExists(cliconstants.DockerFolderUpload) {
helper.CreateDir(cliconstants.DockerFolderUpload)
}
}
func deleteTempFolder(path string) {
folder := filepath.Dir(path)
_ = os.RemoveAll(folder)
}
// zipFolder compresses the contents of srcDir into a zip file at destZip
func zipFolder(srcDir, tmpFolder string, showOutput bool) (string, error) {
var progressBar *progressbar.ProgressBar
if showOutput {
progressBar = progressbar.NewOptions(-1,
progressbar.OptionSetDescription("Compressing files..."),
progressbar.OptionClearOnFinish(),
)
defer progressBar.Finish()
}
folder, err := os.MkdirTemp(tmpFolder, "gokapi-cli-")
if err != nil {
return "", err
}
srcDir, err = filepath.Abs(srcDir)
if err != nil {
return "", err
}
srcDir = filepath.Clean(srcDir)
zipPath := filepath.Join(folder, filepath.Base(srcDir)+".zip")
zipFile, err := os.Create(zipPath)
if err != nil {
return "", err
}
defer zipFile.Close()
zipWriter := zip.NewWriter(zipFile)
defer zipWriter.Close()
// Walk through every file and folder in the source directory
return zipPath, filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Compute the relative path (so zip doesn't store absolute paths)
relPath, err := filepath.Rel(srcDir, path)
if err != nil {
return err
}
// Skip the root directory itself
if relPath == "." {
return nil
}
// For folders, just add an entry with a trailing slash
if info.IsDir() {
_, err := zipWriter.Create(relPath + "/")
return err
}
// For files, create a file entry
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
zipEntry, err := zipWriter.Create(relPath)
if err != nil {
return err
}
progressBar.Describe("Compressing: " + filepath.Base(relPath))
_, err = io.Copy(zipEntry, file)
return err
})
}