Skip to content

Commit

Permalink
Merge pull request #37 from GGP1/validate_flags
Browse files Browse the repository at this point in the history
Validate flags before login
  • Loading branch information
GGP1 authored Jul 28, 2024
2 parents 76c5411 + 924207d commit a10f08a
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

Check warning on line 1 in main.go

View workflow job for this annotation

GitHub Actions / lint

should have a package comment

Check warning on line 1 in main.go

View workflow job for this annotation

GitHub Actions / lint

should have a package comment

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand All @@ -10,12 +11,21 @@ import (
"github.com/GGP1/kure/commands/root"
"github.com/GGP1/kure/config"
"github.com/GGP1/kure/sig"
"github.com/spf13/pflag"

"github.com/awnumar/memguard"
bolt "go.etcd.io/bbolt"
)

func main() {
if err := validateFlags(); err != nil {
if errors.Is(err, pflag.ErrHelp) {
os.Exit(0)
}
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}

if err := config.Init(); err != nil {
fmt.Fprintln(os.Stderr, "couldn't initialize the configuration:", err)
os.Exit(1)
Expand Down Expand Up @@ -55,3 +65,24 @@ func main() {
db.Close()
memguard.SafeExit(0)
}

// validateFlags looks for the command called and parses its flags. If the flag is `--help`,
// it will print the command's help message and return the error pflag.ErrHelp.
func validateFlags() error {
cmd, args, err := root.NewCmd(nil).Find(os.Args[1:])
if err != nil {
return err
}

if err := cmd.ParseFlags(args); err != nil {
if errors.Is(err, pflag.ErrHelp) {
if err := cmd.Help(); err != nil {
return err
}
return pflag.ErrHelp
}
return err
}

return nil
}

0 comments on commit a10f08a

Please sign in to comment.