Add error handling to GetConfig() and main()

main
diff 2021-03-24 03:14:29 +00:00
parent b8998f6090
commit c79402bc54
1 changed files with 13 additions and 5 deletions

18
main.go
View File

@ -39,7 +39,7 @@ type Config struct {
uploading bool
}
func getConfig() Config {
func getConfig() (Config, error) {
conf := Config{
downloads: 1,
port: 8008,
@ -61,7 +61,11 @@ func getConfig() Config {
conf.filePath = flag.Arg(0)
conf.fileName = path.Base(conf.filePath)
return conf
if conf.filePath == "" && !conf.uploading {
return conf, errors.New("no file provided to download")
}
return conf, nil
}
func getIP() (string, error) {
@ -79,7 +83,11 @@ func getIP() (string, error) {
}
func main() {
conf := getConfig()
conf, err := getConfig()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
server := &http.Server{
Addr: fmt.Sprintf(":%v", conf.port),
@ -96,7 +104,7 @@ func main() {
ip, err := getIP()
if err != nil {
fmt.Println(err)
return
os.Exit(1)
}
url := fmt.Sprintf("http://%s:%v/%s", ip, conf.port, conf.fileName)
@ -108,7 +116,7 @@ func main() {
err = server.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
fmt.Println(err)
return
os.Exit(1)
}
}