Skip to content

Commit 3dd5740

Browse files
committed
Allow changing of configuration file for docker #292, refactoring, comments
1 parent b6799af commit 3dd5740

File tree

5 files changed

+83
-40
lines changed

5 files changed

+83
-40
lines changed

cmd/cli-uploader/Main.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import (
66
"fmt"
77
"github.com/forceu/gokapi/cmd/cli-uploader/cliapi"
88
"github.com/forceu/gokapi/cmd/cli-uploader/cliconfig"
9+
"github.com/forceu/gokapi/cmd/cli-uploader/cliconstants"
910
"github.com/forceu/gokapi/cmd/cli-uploader/cliflags"
1011
"github.com/forceu/gokapi/internal/environment"
1112
"github.com/forceu/gokapi/internal/helper"
1213
"os"
1314
)
1415

1516
func main() {
16-
cliflags.Init(cliconfig.DockerFolderConfigFile, cliconfig.DockerFolderUpload)
1717
mode := cliflags.Parse()
1818
switch mode {
1919
case cliflags.ModeLogin:
@@ -72,11 +72,15 @@ func checkDockerFolders() {
7272
if !environment.IsDockerInstance() {
7373
return
7474
}
75-
if !helper.FolderExists(cliconfig.DockerFolderConfig) {
75+
_, isDefault := cliflags.GetConfigLocation()
76+
if !isDefault {
77+
return
78+
}
79+
if !helper.FolderExists(cliconstants.DockerFolderConfig) {
7680
fmt.Println("Warning: Docker folder does not exist, configuration will be lost when creating a new container")
77-
helper.CreateDir(cliconfig.DockerFolderConfig)
81+
helper.CreateDir(cliconstants.DockerFolderConfig)
7882
}
79-
if !helper.FolderExists(cliconfig.DockerFolderUpload) {
80-
helper.CreateDir(cliconfig.DockerFolderUpload)
83+
if !helper.FolderExists(cliconstants.DockerFolderUpload) {
84+
helper.CreateDir(cliconstants.DockerFolderUpload)
8185
}
8286
}

cmd/cli-uploader/cliapi/cliapi.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,32 @@ type header struct {
3333
Value string
3434
}
3535

36+
// EInvalidRequest is returned when the API returns a 400
37+
var EInvalidRequest = errors.New("400 Bad Request")
38+
39+
// EUnauthorised is returned when the API returns a 401
3640
var EUnauthorised = errors.New("unauthorised")
41+
42+
// ENotFound is returned when the API returns a 404
3743
var ENotFound = errors.New("404 Not Found")
38-
var EInvalidRequest = errors.New("400 Bad Request")
44+
45+
// EFileTooBig is returned when the file size exceeds the allowed limit
3946
var EFileTooBig = errors.New("file too big")
47+
48+
// EE2eKeyIncorrect is returned when the e2e key is incorrect
4049
var EE2eKeyIncorrect = errors.New("e2e key incorrect")
4150

51+
// Init initialises the API client with the given url and key.
52+
// The key is used for authentication.
53+
// The end2endKey is used for end-to-end encryption.
4254
func Init(url, key string, end2endKey []byte) {
4355
gokapiUrl = strings.TrimSuffix(url, "/") + "/api"
4456
apiKey = key
4557
e2eKey = end2endKey
4658

4759
}
4860

61+
// GetVersion returns the version of the Gokapi server
4962
func GetVersion() (string, int, error) {
5063
result, err := getUrl(gokapiUrl+"/info/version", []header{}, false)
5164
if err != nil {
@@ -66,6 +79,7 @@ func GetVersion() (string, int, error) {
6679
return parsedResult.Version, parsedResult.VersionInt, nil
6780
}
6881

82+
// GetConfig returns the upload configuration of the Gokapi server
6983
func GetConfig() (int, int, bool, error) {
7084
result, err := getUrl(gokapiUrl+"/info/config", []header{}, false)
7185
if err != nil {
@@ -123,6 +137,7 @@ func getUrl(url string, headers []header, longTimeout bool) (string, error) {
123137
return string(body), nil
124138
}
125139

140+
// UploadFile uploads a file to the Gokapi server
126141
func UploadFile(uploadParams cliflags.UploadConfig) (models.FileApiOutput, error) {
127142
var progressBar *progressbar.ProgressBar
128143
file, err := os.OpenFile(uploadParams.File, os.O_RDONLY, 0664)
@@ -313,6 +328,7 @@ func completeChunk(uid, filename string, filesize, realsize int64, useE2e bool,
313328
return parsedResult.FileInfo, nil
314329
}
315330

331+
// GetE2eInfo returns the end-to-end encryption information of the Gokapi server for this user
316332
func GetE2eInfo() (models.E2EInfoPlainText, error) {
317333
var result models.E2EInfoEncrypted
318334
var fileInfo models.E2EInfoPlainText

cmd/cli-uploader/cliconfig/cliconfig.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,23 @@ import (
66
"errors"
77
"fmt"
88
"github.com/forceu/gokapi/cmd/cli-uploader/cliapi"
9+
"github.com/forceu/gokapi/cmd/cli-uploader/cliconstants"
910
"github.com/forceu/gokapi/cmd/cli-uploader/cliflags"
1011
"github.com/forceu/gokapi/internal/helper"
1112
"os"
1213
"strings"
1314
)
1415

15-
const minGokapiVersionInt = 20100
16-
const minGokapiVersionStr = "2.1.0"
17-
18-
const DockerFolderConfig = "/app/config/"
19-
const DockerFolderConfigFile = DockerFolderConfig + "config.json"
20-
const DockerFolderUpload = "/upload/"
21-
2216
type configFile struct {
2317
Url string `json:"Url"`
2418
Apikey string `json:"Apikey"`
2519
E2ekey []byte `json:"E2Ekey"`
2620
}
2721

22+
// CreateLogin creates a login for the CLI.
23+
// It will ask the user for the URL and API key.
24+
// It will then test the connection and download the configuration.
25+
// If the configuration is valid, the login information will be saved.
2826
func CreateLogin() {
2927
fmt.Print("Gokapi URL: ")
3028
url := helper.ReadLine()
@@ -67,8 +65,8 @@ func CreateLogin() {
6765
os.Exit(1)
6866
}
6967

70-
if vint < minGokapiVersionInt {
71-
fmt.Println("\nERROR: Gokapi version must be at least " + minGokapiVersionStr)
68+
if vint < cliconstants.MinGokapiVersionInt {
69+
fmt.Println("\nERROR: Gokapi version must be at least " + cliconstants.MinGokapiVersionStr)
7270
fmt.Println("Your version is " + vstr)
7371
os.Exit(1)
7472
}
@@ -129,16 +127,20 @@ func save(url, apikey string, e2ekey []byte) error {
129127
return err
130128
}
131129

132-
return os.WriteFile(cliflags.GetConfigLocation(), jsonData, 0600)
130+
configPath, _ := cliflags.GetConfigLocation()
131+
return os.WriteFile(configPath, jsonData, 0600)
133132
}
134133

134+
// Load initialises the configuration by reading login information from a file and setting up CLI API parameters.
135+
// Verifies the existence of the configuration file and validates its integrity, terminating on errors.
135136
func Load() {
136-
if !helper.FileExists(cliflags.GetConfigLocation()) {
137+
configPath, _ := cliflags.GetConfigLocation()
138+
if !helper.FileExists(configPath) {
137139
fmt.Println("ERROR: No login information found")
138140
fmt.Println("Please run 'gokapi-cli login' to create a login")
139141
os.Exit(1)
140142
}
141-
data, err := os.ReadFile(cliflags.GetConfigLocation())
143+
data, err := os.ReadFile(configPath)
142144
if err != nil {
143145
fmt.Println("ERROR: Could not read login information")
144146
os.Exit(1)
@@ -154,9 +156,12 @@ func Load() {
154156
cliapi.Init(config.Url, config.Apikey, config.E2ekey)
155157
}
156158

159+
// Delete deletes the login information file.
160+
// It will return an error if the file exists but could not be deleted.
157161
func Delete() error {
158-
if !helper.FileExists(cliflags.GetConfigLocation()) {
162+
configPath, _ := cliflags.GetConfigLocation()
163+
if !helper.FileExists(configPath) {
159164
return nil
160165
}
161-
return os.Remove(cliflags.GetConfigLocation())
166+
return os.Remove(configPath)
162167
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cliconstants
2+
3+
// MinGokapiVersionInt is the minimum version of the gokapi server that is supported by the cli
4+
const MinGokapiVersionInt = 20100
5+
6+
// MinGokapiVersionStr is the minimum version of the gokapi server that is supported by the cli
7+
const MinGokapiVersionStr = "2.1.0"
8+
9+
// DefaultConfigFileName is the default config file name
10+
const DefaultConfigFileName = "gokapi-cli.json"
11+
12+
// DockerFolderConfig is the default config folder for an docker instance
13+
const DockerFolderConfig = "/app/config/"
14+
15+
// DockerFolderConfigFile is the default config path for a docker instance
16+
const DockerFolderConfigFile = DockerFolderConfig + "config.json"
17+
18+
// DockerFolderUpload is the default upload folder for a docker instance
19+
const DockerFolderUpload = "/upload/"

cmd/cli-uploader/cliflags/cliflags.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,25 @@ package cliflags
22

33
import (
44
"fmt"
5+
"github.com/forceu/gokapi/cmd/cli-uploader/cliconstants"
56
"github.com/forceu/gokapi/internal/environment"
67
"os"
78
"path/filepath"
89
"strconv"
910
)
1011

1112
const (
13+
// ModeLogin is the mode for the login command
1214
ModeLogin = iota
15+
// ModeLogout is the mode for the logout command
1316
ModeLogout
17+
// ModeUpload is the mode for the upload command
1418
ModeUpload
19+
// ModeInvalid is the mode for an invalid command
1520
ModeInvalid
1621
)
1722

18-
var dockerConfigFile string
19-
var dockerUploadFolder string
20-
23+
// UploadConfig contains the parameters for the upload command.
2124
type UploadConfig struct {
2225
File string
2326
JsonOutput bool
@@ -27,11 +30,7 @@ type UploadConfig struct {
2730
Password string
2831
}
2932

30-
func Init(dockerConfigPath, dockerUploadPath string) {
31-
dockerConfigFile = dockerConfigPath
32-
dockerUploadFolder = dockerUploadPath
33-
}
34-
33+
// Parse parses the command line arguments and returns the mode.
3534
func Parse() int {
3635
if len(os.Args) < 2 {
3736
printUsage(3)
@@ -52,6 +51,7 @@ func Parse() int {
5251
return ModeInvalid
5352
}
5453

54+
// GetUploadParameters parses the command line arguments and returns the parameters for the upload command.
5555
func GetUploadParameters() UploadConfig {
5656
result := UploadConfig{}
5757
for i := 2; i < len(os.Args); i++ {
@@ -90,7 +90,7 @@ func GetUploadParameters() UploadConfig {
9090
if environment.IsDockerInstance() {
9191
ok, dockerFile := getDockerUpload()
9292
if !ok {
93-
fmt.Println("ERROR: Missing parameter --file and no file or more than one file found in " + dockerUploadFolder)
93+
fmt.Println("ERROR: Missing parameter --file and no file or more than one file found in " + cliconstants.DockerFolderUpload)
9494
os.Exit(2)
9595
}
9696
result.File = dockerFile
@@ -112,7 +112,7 @@ func getDockerUpload() (bool, string) {
112112
if !environment.IsDockerInstance() {
113113
return false, ""
114114
}
115-
entries, err := os.ReadDir(dockerUploadFolder)
115+
entries, err := os.ReadDir(cliconstants.DockerFolderUpload)
116116
if err != nil {
117117
return false, ""
118118
}
@@ -132,22 +132,23 @@ func getDockerUpload() (bool, string) {
132132
if !fileFound {
133133
return false, ""
134134
}
135-
return true, filepath.Join(dockerUploadFolder, fileName)
135+
return true, filepath.Join(cliconstants.DockerFolderUpload, fileName)
136136
}
137137

138-
func GetConfigLocation() string {
139-
if environment.IsDockerInstance() {
140-
return dockerConfigFile
141-
}
138+
// GetConfigLocation returns the path to the configuration file. Returns true if the default file is used
139+
func GetConfigLocation() (string, bool) {
142140
for i := 2; i < len(os.Args); i++ {
143141
switch os.Args[i] {
144142
case "-c":
145143
fallthrough
146144
case "--configuration":
147-
return getParameter(&i)
145+
return getParameter(&i), false
148146
}
149147
}
150-
return "gokapi-cli.json"
148+
if environment.IsDockerInstance() {
149+
return cliconstants.DockerFolderConfigFile, true
150+
}
151+
return cliconstants.DefaultConfigFileName, true
151152
}
152153

153154
func getParameter(position *int) string {
@@ -183,9 +184,7 @@ func printUsage(exitCode int) {
183184

184185
fmt.Println("Options:")
185186
fmt.Println(" -f, --file <path> File to upload")
186-
if !environment.IsDockerInstance() {
187-
fmt.Println(" -c, --configuration <path> Path to configuration file (default: gokapi-cli.json)")
188-
}
187+
fmt.Println(" -c, --configuration <path> Path to configuration file (default: gokapi-cli.json)")
189188
fmt.Println(" -j, --json Output the result in JSON only")
190189
fmt.Println(" -n, --disable-e2e Disable end-to-end encryption")
191190
fmt.Println(" -e, --expiry-days <int> Set file expiry in days (default: unlimited)")

0 commit comments

Comments
 (0)