Skip to content

Commit

Permalink
fix(cli): Silent failure when storetypes cannot find valid source file.
Browse files Browse the repository at this point in the history
  • Loading branch information
spbsoluble committed Nov 7, 2022
1 parent 3f90908 commit 82bcc0d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 23 deletions.
2 changes: 1 addition & 1 deletion cmd/rot.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func readCertsFile(certsFilePath string, kfclient *api.Client) (map[string]strin
return certs, nil
}

func isRootStore(st *api.GetStoreByIDResp, invs *[]api.CertStoreInventory, minCerts int, maxKeys int, maxLeaf int) bool {
func isRootStore(st *api.GetCertificateStoreResponse, invs *[]api.CertStoreInventory, minCerts int, maxKeys int, maxLeaf int) bool {
leafCount := 0
keyCount := 0
certCount := 0
Expand Down
32 changes: 22 additions & 10 deletions cmd/storeTypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ var storesTypeCreateCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("create called")
//Check if store type is valid
validStoreTypes := getValidStoreTypes()
validStoreTypes := getValidStoreTypes("")
storeType, _ := cmd.Flags().GetString("name")
storeTypeIsValid := false
for _, v := range validStoreTypes {
Expand All @@ -139,7 +139,11 @@ var storesTypeCreateCmd = &cobra.Command{
log.Fatalf("Error: Invalid store type: %s", storeType)
} else {
kfClient, _ := initClient()
storeTypeConfig, _ := readStoreTypesConfig()
storeTypeConfig, stErr := readStoreTypesConfig("")
if stErr != nil {
fmt.Printf("Error: %s", stErr)
log.Fatalf("Error: %s", stErr)
}
log.Printf("[DEBUG] Store type config: %v", storeTypeConfig[storeType])
sConfig := storeTypeConfig[storeType].(map[string]interface{})
props, pErr := buildStoreTypePropertiesInterface(sConfig["Properties"].([]interface{}))
Expand Down Expand Up @@ -235,7 +239,7 @@ var fetchStoreTypes = &cobra.Command{
Short: "Fetches store type templates from Keyfactor's Github.",
Long: `Fetches store type templates from Keyfactor's Github.`,
Run: func(cmd *cobra.Command, args []string) {
templates, err := readStoreTypesConfig()
templates, err := readStoreTypesConfig("")
if err != nil {
log.Printf("Error: %s", err)
fmt.Printf("Error: %s\n", err)
Expand All @@ -254,7 +258,7 @@ var generateStoreTypeTemplate = &cobra.Command{
Short: "Generates either a JSON or CSV template file for certificate store type bulk operations.",
Long: `Generates either a JSON or CSV template file for certificate store type bulk operations.`,
Run: func(cmd *cobra.Command, args []string) {
templates, err := readStoreTypesConfig()
templates, err := readStoreTypesConfig("")
if err != nil {
log.Printf("Error: %s", err)
fmt.Printf("Error: %s\n", err)
Expand All @@ -268,10 +272,13 @@ var generateStoreTypeTemplate = &cobra.Command{
},
}

func readStoreTypesConfig() (map[string]interface{}, error) {
content, err := os.ReadFile("./store_types.json")
func readStoreTypesConfig(fp string) (map[string]interface{}, error) {
if fp == "" {
fp = "./store-types.json"
}
content, err := os.ReadFile(fp)
if err != nil {
log.Fatal("Error when opening file: ", err)
return nil, err
}

// Now let's unmarshall the data into `payload`
Expand All @@ -285,8 +292,13 @@ func readStoreTypesConfig() (map[string]interface{}, error) {
return datas, nil
}

func getValidStoreTypes() []string {
validStoreTypes, _ := readStoreTypesConfig()
func getValidStoreTypes(fp string) []string {
validStoreTypes, rErr := readStoreTypesConfig(fp)
if rErr != nil {
log.Printf("Error: %s", rErr)
fmt.Printf("Error: %s\n", rErr)
return nil
}
validStoreTypesList := make([]string, 0, len(validStoreTypes))
for k := range validStoreTypes {
validStoreTypesList = append(validStoreTypesList, k)
Expand All @@ -298,7 +310,7 @@ func getValidStoreTypes() []string {

func init() {

validTypesString := strings.Join(getValidStoreTypes(), ", ")
validTypesString := strings.Join(getValidStoreTypes(""), ", ")
RootCmd.AddCommand(storeTypesCmd)

// GET store type templates
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module kfutil
go 1.19

require (
github.com/Keyfactor/keyfactor-go-client v1.2.0-rc1
github.com/Keyfactor/keyfactor-go-client v1.2.0-rc2
github.com/spf13/cobra v1.6.1
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/Keyfactor/keyfactor-go-client v1.2.0-rc1 h1:cvh9cl4RDW1JML9GPmGxLIOYZ4v+XBVFKJ6ch0nPzZw=
github.com/Keyfactor/keyfactor-go-client v1.2.0-rc1/go.mod h1:u1M1AjcwiO/Tbvc7EsNl9YTy757hO5wmey1/W/7Qkbs=
github.com/Keyfactor/keyfactor-go-client v1.2.0-rc2 h1:wRvG/6dN+lu17k6FjFtjl8BPGWo848OS/tX8uyCuOsI=
github.com/Keyfactor/keyfactor-go-client v1.2.0-rc2/go.mod h1:u1M1AjcwiO/Tbvc7EsNl9YTy757hO5wmey1/W/7Qkbs=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
Expand Down
18 changes: 9 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ Copyright © 2022 NAME HERE <EMAIL ADDRESS>
package main

import (
"flag"
"fmt"
"github.com/spf13/cobra/doc"
"kfutil/cmd"
"os"
)

func main() {
var docsFlag bool
flag.BoolVar(&docsFlag, "makedocs", false, "Create markdown docs.")
flag.Parse()
if docsFlag {
docs()
os.Exit(0)
}
//var docsFlag bool
//flag.BoolVar(&docsFlag, "makedocs", false, "Create markdown docs.")
//flag.Parse()
//if docsFlag {
// docs()
// os.Exit(0)
//}
fmt.Println("Hello, playground")
cmd.Execute()
}

Expand Down

0 comments on commit 82bcc0d

Please sign in to comment.