From b08396c8f32067e469324086e6c162238358489e Mon Sep 17 00:00:00 2001 From: benfdking Date: Sun, 7 Jun 2020 22:03:07 +0100 Subject: [PATCH] Add better documenation and clear up --- cmd/defaults.go | 7 +++++-- cmd/jwt.go | 6 +++--- cmd/list.go | 27 +++++++-------------------- cmd/root.go | 5 +++++ pkg/defaultjwt/defaultClaims.go | 4 +--- 5 files changed, 21 insertions(+), 28 deletions(-) diff --git a/cmd/defaults.go b/cmd/defaults.go index 03c048b..5d803c9 100644 --- a/cmd/defaults.go +++ b/cmd/defaults.go @@ -18,9 +18,12 @@ var defaultsCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { cs := defaultjwt.GetDefaultClaims() if explain { + w := newTabWriter() + fmt.Fprintf(w, "%s\t%s\t%s\n", "Key", "Value", "Description") for _, c := range cs { - fmt.Printf("key: %s\tvalue: %s\tdescription: %s\n", c.Key, c.Value, c.Description) + fmt.Fprintf(w, "%s\t%s\t%s\n", c.Key, c.Value, c.Description) } + w.Flush() } else { m := make(map[string]string, len(cs)) for _, c := range cs { @@ -38,5 +41,5 @@ var defaultsCmd = &cobra.Command{ func init() { rootCmd.AddCommand(defaultsCmd) - defaultsCmd.Flags().BoolVarP(&explain, "explain", "e", false, "outputs a table explaining the default values") + defaultsCmd.Flags().BoolVarP(&explain, "explain", "e", false, "Explain default values") } diff --git a/cmd/jwt.go b/cmd/jwt.go index 5e905fe..f1a9054 100644 --- a/cmd/jwt.go +++ b/cmd/jwt.go @@ -62,8 +62,8 @@ var jwtCmd = &cobra.Command{ func init() { rootCmd.AddCommand(jwtCmd) - jwtCmd.Flags().StringToStringVarP(&claims, "claims", "c", map[string]string{}, "claims to be added to the jwt") + jwtCmd.Flags().StringToStringVarP(&claims, "claims", "c", map[string]string{}, "Claims for JWT") jwtCmd.Flags().StringVarP(&key, "key", "k", "a", "[abc] jwt key to use") - jwtCmd.Flags().BoolVarP(&addDefaults, "defaults", "d", true, "adds default oidc parameters, true by default") - jwtCmd.Flags().StringVarP(&filePath, "file", "f", "", "json file to read claims from") + jwtCmd.Flags().BoolVarP(&addDefaults, "defaults", "d", true, "Add default claims") + jwtCmd.Flags().StringVarP(&filePath, "file", "f", "", "Add claims from JSON file") } diff --git a/cmd/list.go b/cmd/list.go index 3ea0a77..f9aecb6 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -11,20 +11,8 @@ import ( // listCmd represents the list command var listCmd = &cobra.Command{ Use: "list [abc]", - Short: "List the default keys, optionally specifying one only returns one", - Args: func(cmd *cobra.Command, args []string) error { - switch { - case len(args) > 1: - return fmt.Errorf("can only provide no or 1 argument, not %d arguments", len(args)) - case len(args) == 1: - if !url.IsVersion(args[0]) { - return fmt.Errorf("can only be one of the aviable keys: %s", url.ReturnVersions()) - } - return nil - default: - return nil - } - }, + Short: "List the default keys, optionally specifying one only returns one jwks url", + Args: cobra.RangeArgs(0, 1), Run: func(cmd *cobra.Command, args []string) { if len(args) == 1 { jwksURL, _, err := url.ReturnJWKSAndPrivatePEMURL(args[0]) @@ -33,22 +21,21 @@ var listCmd = &cobra.Command{ } fmt.Println(jwksURL) } else { + w := newTabWriter() + fmt.Fprintf(w, "%s\t%s\t%s\n", "Key", "JWKS URL", "Private PEM URL") const alphabet = "abc" for _, c := range alphabet { - jwksURL, _, err := url.ReturnJWKSAndPrivatePEMURL(string(c)) + jwks, pem, err := url.ReturnJWKSAndPrivatePEMURL(string(c)) if err != nil { log.Fatal(err) } - fmt.Printf("key %c, jwks url: "+jwksURL+"\n", c) + fmt.Fprintf(w, "%s\t%s\t%s\n", string(c), jwks, pem) } + w.Flush() } }, } func init() { rootCmd.AddCommand(listCmd) - - // TODO ADD Showing pem url - //var showPemURL bool - //listCmd.Flags().BoolVarP(&showPemURL, "pem", "p", false, "Show URL For Private key") } diff --git a/cmd/root.go b/cmd/root.go index 1f84332..fcc86d2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,6 +3,7 @@ package cmd import ( "fmt" "os" + "text/tabwriter" "github.com/spf13/cobra" ) @@ -31,6 +32,10 @@ func init() { rootCmd.AddCommand(versionCmd) } +func newTabWriter() *tabwriter.Writer { + return tabwriter.NewWriter(os.Stdout, 2, 0, 3, ' ', tabwriter.TabIndent) +} + // Execute adds all child commands to the root command and sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute(version, commit, date string) { diff --git a/pkg/defaultjwt/defaultClaims.go b/pkg/defaultjwt/defaultClaims.go index d84dafa..aece416 100644 --- a/pkg/defaultjwt/defaultClaims.go +++ b/pkg/defaultjwt/defaultClaims.go @@ -13,9 +13,7 @@ type DefaultClaims struct { Description string } -// GetDefaultClaims returns a list of default claims -// -// default claims have been taken from rfc 7519 +// GetDefaultClaims returns a list of default claims. Default claims have been taken from rfc 7519 func GetDefaultClaims() []DefaultClaims { return []DefaultClaims{ {