refactor: unify error handling even more
This commit is contained in:
@@ -19,7 +19,10 @@ func Init() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
parseFlags()
|
err = parseFlags()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if GetReadonlyMode() && GetSinkholeMode() {
|
if GetReadonlyMode() && GetSinkholeMode() {
|
||||||
return fmt.Errorf("Cannot enable both readonly and sinkhole modes at the same time.")
|
return fmt.Errorf("Cannot enable both readonly and sinkhole modes at the same time.")
|
||||||
|
@@ -73,7 +73,7 @@ func generateRandomPassword() string {
|
|||||||
return base64.RawURLEncoding.EncodeToString(b)[:LengthOfRandomBasicAuthPassword]
|
return base64.RawURLEncoding.EncodeToString(b)[:LengthOfRandomBasicAuthPassword]
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseFlags() {
|
func parseFlags() error {
|
||||||
flag.BoolVar(&basicAuthMode, "auth", false, "Enable basic authentication.")
|
flag.BoolVar(&basicAuthMode, "auth", false, "Enable basic authentication.")
|
||||||
flag.BoolVar(&httpMode, "http", false, "Enable http mode. Nothing will be encrypted.")
|
flag.BoolVar(&httpMode, "http", false, "Enable http mode. Nothing will be encrypted.")
|
||||||
flag.BoolVar(&readonlyMode, "readonly", false, "Enable readonly mode. No files can be uploaded or deleted.")
|
flag.BoolVar(&readonlyMode, "readonly", false, "Enable readonly mode. No files can be uploaded or deleted.")
|
||||||
@@ -87,10 +87,25 @@ func parseFlags() {
|
|||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
parseFlagValueBasicAuthPassword()
|
parseFlagValueBasicAuthPassword()
|
||||||
parseFlagValuePortToListenOn()
|
|
||||||
|
err := parseFlagValuePortToListenOn()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
parseFlagValuePathDataFolder()
|
parseFlagValuePathDataFolder()
|
||||||
parseFlagValuePathTLSCertFile()
|
|
||||||
parseFlagValuePathTLSKeyFile()
|
err = parseFlagValuePathTLSCertFile()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = parseFlagValuePathTLSKeyFile()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseFlagValueBasicAuthPassword() {
|
func parseFlagValueBasicAuthPassword() {
|
||||||
@@ -122,44 +137,53 @@ func parseFlagValuePathDataFolder() {
|
|||||||
pathUploadFolder = filepath.Join(pathDataFolder, DefaultNameUploadFolder)
|
pathUploadFolder = filepath.Join(pathDataFolder, DefaultNameUploadFolder)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseFlagValuePortToListenOn() {
|
func parseFlagValuePortToListenOn() error {
|
||||||
if portToListenOn < 1 || portToListenOn > 65535 {
|
if portToListenOn < 1 || portToListenOn > 65535 {
|
||||||
portToListenOn = DefaultPortToListenOn
|
return fmt.Errorf("The port must be between 1 and 65535 (both ports included).")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseFlagValuePathTLSCertFile() {
|
func parseFlagValuePathTLSCertFile() error {
|
||||||
if pathTLSCertFile == "" {
|
if pathTLSCertFile == "" {
|
||||||
pathTLSKeyFile = ""
|
if pathTLSKeyFile != "" {
|
||||||
return
|
return fmt.Errorf("Both a certificate and the corresponding key must be provided.")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
info, err := os.Stat(pathTLSCertFile)
|
info, err := os.Stat(pathTLSCertFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Error: Failed to read cert: %v\n", err)
|
return fmt.Errorf("Failed to read cert: %v", err)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if info.IsDir() {
|
if info.IsDir() {
|
||||||
fmt.Fprintf(os.Stderr, "Error: Cert must be a file\n")
|
fmt.Fprintf(os.Stderr, "Error: Cert must be a file\n")
|
||||||
os.Exit(1)
|
return fmt.Errorf("Cert must be a valid file.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseFlagValuePathTLSKeyFile() {
|
func parseFlagValuePathTLSKeyFile() error {
|
||||||
if pathTLSKeyFile == "" {
|
if pathTLSKeyFile == "" {
|
||||||
pathTLSCertFile = ""
|
if pathTLSCertFile != "" {
|
||||||
return
|
return fmt.Errorf("Both a certificate and the corresponding key must be provided.")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
info, err := os.Stat(pathTLSKeyFile)
|
info, err := os.Stat(pathTLSKeyFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Error: Failed to read key: %v\n", err)
|
return fmt.Errorf("Failed to read key: %v", err)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if info.IsDir() {
|
if info.IsDir() {
|
||||||
fmt.Fprintf(os.Stderr, "Error: Key must be a file\n")
|
return fmt.Errorf("Key must be a valid file.")
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user