Skip to content

Commit

Permalink
feat: accept json as template args (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
lostbean authored Sep 5, 2024
1 parent b0eb232 commit 3890abf
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions kardinal-cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cmd

import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -477,14 +479,15 @@ func init() {

createCmd.Flags().StringSliceVarP(&serviceImagePairs, "service-image", "s", []string{}, "Extra service and respective image to include in the same flow (can be used multiple times)")
createCmd.Flags().StringVarP(&templateName, "template", "t", "", "Template name to use for the flow creation")
createCmd.Flags().StringVarP(&templateArgsFile, "template-args", "a", "", "Path to YAML file containing template arguments")
createCmd.Flags().StringVarP(&templateArgsFile, "template-args", "a", "", "JSON with the template arguments or path to YAML file containing template arguments")

deployCmd.PersistentFlags().StringVarP(&kubernetesManifestFile, "k8s-manifest", "k", "", "Path to the K8S manifest file")
deployCmd.MarkPersistentFlagRequired("k8s-manifest")

templateCreateCmd.Flags().StringVarP(&templateYamlFile, "template-yaml", "t", "", "Path to the YAML file containing the template")
templateCreateCmd.Flags().StringVarP(&templateDescription, "description", "d", "", "Description of the template")
templateCreateCmd.MarkFlagRequired("template-yaml")

deleteCmd.Flags().BoolP(deleteAllDevFlowsFlagName, "", false, "Delete all the current dev flows")
}

Expand Down Expand Up @@ -581,23 +584,34 @@ func parseKubernetesManifestFile(kubernetesManifestFile string) ([]api_types.Ser
return finalServiceConfigs, finalIngressConfigs, namespace, nil
}

func parseTemplateArgs(filename string) (map[string]interface{}, error) {
if filename == "" {
func parseTemplateArgs(filepathOrJson string) (map[string]interface{}, error) {
if filepathOrJson == "" {
return nil, nil
}

data, err := os.ReadFile(filename)
if err != nil {
return nil, err
}

var args map[string]interface{}
err = yaml.Unmarshal(data, &args)
if err != nil {
return nil, err
}

return args, nil
if _, err := os.Stat(filepathOrJson); errors.Is(err, os.ErrNotExist) {
err = json.Unmarshal([]byte(filepathOrJson), &args)
if err != nil {
return nil, stacktrace.Propagate(err, "Template arguments must be a valid JSON object or a path to a JSON file")
}

return args, nil
} else {
data, err := os.ReadFile(filepathOrJson)
if err != nil {
return nil, err
}

err = yaml.Unmarshal(data, &args)
if err != nil {
return nil, err
}

return args, nil

}
}

// Use in priority the label app value
Expand Down

0 comments on commit 3890abf

Please sign in to comment.