From db0ac44c97c71bcd536c3a7df9f8791792ef22b1 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sun, 28 May 2023 13:19:24 +0200 Subject: [PATCH] credentials: Serve(): simplify error-handling logic Don't use an err if we can print the error immediately :) Signed-off-by: Sebastiaan van Stijn --- credentials/credentials.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/credentials/credentials.go b/credentials/credentials.go index 11da850..92a567b 100644 --- a/credentials/credentials.go +++ b/credentials/credentials.go @@ -49,21 +49,21 @@ func SetCredsLabel(label string) { // It uses os.Stdin as input and os.Stdout as output. // This function terminates the program with os.Exit(1) if there is an error. func Serve(helper Helper) { - var err error if len(os.Args) != 2 { - err = fmt.Errorf("Usage: %s ", Name) + _, _ = fmt.Fprintln(os.Stdout, usage()) + os.Exit(1) } - if err == nil { - err = HandleCommand(helper, os.Args[1], os.Stdin, os.Stdout) - } - - if err != nil { + if err := HandleCommand(helper, os.Args[1], os.Stdin, os.Stdout); err != nil { _, _ = fmt.Fprintln(os.Stdout, err) os.Exit(1) } } +func usage() string { + return fmt.Sprintf("Usage: %s ", Name) +} + // HandleCommand uses a helper and a key to run a credential action. func HandleCommand(helper Helper, key string, in io.Reader, out io.Writer) error { switch key {