From af53a31d2c1b1084b5f2d1519ea3ca0ed8063aff Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Mon, 27 Jun 2022 19:50:43 +0200 Subject: [PATCH] Lint: Fix linter warnings / errors Signed-off-by: Knut Ahlers --- cmd/password/cmdGet.go | 11 ++++++++--- cmd/password/cmdServe.go | 10 +++++++++- cmd/password/main.go | 4 +--- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/cmd/password/cmdGet.go b/cmd/password/cmdGet.go index 5cdefda..634ae7f 100644 --- a/cmd/password/cmdGet.go +++ b/cmd/password/cmdGet.go @@ -11,6 +11,11 @@ import ( pwd "github.com/Luzifer/password/v2/lib" ) +const ( + defaultPasswordCount = 1 + defaultPasswordLength = 20 +) + func getCmdGet() *cobra.Command { cmd := cobra.Command{ Use: "get", @@ -19,8 +24,8 @@ func getCmdGet() *cobra.Command { } cmd.Flags().BoolVarP(&flags.CLI.JSON, "json", "j", false, "return output in JSON format") - cmd.Flags().IntVarP(&flags.CLI.Length, "length", "l", 20, "length of the generated password") - cmd.Flags().IntVarP(&flags.CLI.Num, "number", "n", 1, "number of passwords to generate") + cmd.Flags().IntVarP(&flags.CLI.Length, "length", "l", defaultPasswordLength, "length of the generated password") + cmd.Flags().IntVarP(&flags.CLI.Num, "number", "n", defaultPasswordCount, "number of passwords to generate") cmd.Flags().BoolVarP(&flags.CLI.SpecialCharacters, "special", "s", false, "use special characters in your password") cmd.Flags().BoolVarP(&flags.CLI.XKCD, "xkcd", "x", false, "use XKCD style password") @@ -66,7 +71,7 @@ func actionCmdGet(cmd *cobra.Command, args []string) { case err == pwd.ErrLengthTooLow: fmt.Println("The password has to be more than 4 characters long to meet the security considerations") default: - fmt.Println("An unknown error occured") + fmt.Println("An unknown error occurred") } os.Exit(1) } diff --git a/cmd/password/cmdServe.go b/cmd/password/cmdServe.go index 6ac7ddc..81753f1 100644 --- a/cmd/password/cmdServe.go +++ b/cmd/password/cmdServe.go @@ -9,12 +9,15 @@ import ( "strconv" "github.com/gorilla/mux" + "github.com/pkg/errors" "github.com/spf13/cobra" http_helper "github.com/Luzifer/go_helpers/v2/http" pwd "github.com/Luzifer/password/v2/lib" ) +const defaultHTTPListenPort = 3000 + func getCmdServe() *cobra.Command { cmd := cobra.Command{ Use: "serve", @@ -22,7 +25,7 @@ func getCmdServe() *cobra.Command { Run: actionCmdServe, } - cmd.Flags().IntVar(&flags.Server.Port, "port", 3000, "port to listen on") + cmd.Flags().IntVar(&flags.Server.Port, "port", defaultHTTPListenPort, "port to listen on") return &cmd } @@ -62,6 +65,11 @@ func handleAPIGetPasswordv1(res http.ResponseWriter, r *http.Request) { password, err = pwd.NewSecurePassword().GeneratePassword(length, special) } + if err != nil { + http.Error(res, errors.Wrap(err, "generating password").Error(), http.StatusInternalServerError) + return + } + res.Header().Add("Content-Type", "text/plain") res.Header().Add("Cache-Control", "no-cache") res.Write([]byte(password)) diff --git a/cmd/password/main.go b/cmd/password/main.go index 79a2f18..5b3ab8c 100644 --- a/cmd/password/main.go +++ b/cmd/password/main.go @@ -2,9 +2,7 @@ package main import "github.com/spf13/cobra" -var ( - version = "dev" -) +var version = "dev" func main() { rootCmd := cobra.Command{