From 79a92c826fcd0d80b5a330a5925007c5e420afb2 Mon Sep 17 00:00:00 2001 From: Michael Lacore Date: Thu, 21 Nov 2024 13:48:24 -0800 Subject: [PATCH] Adding schematobicep command (#18) * Adding schematobicep command * errcheck lint fix * Fix for test ordering issue --- cmd/bicep.go | 39 +++++++++++++++++++++++++++++++++-- cmd/opentofu.go | 6 +++--- docs/helpdocs/bicep/output.md | 9 ++++++++ pkg/bicep/schematobicep.go | 11 +++++++++- 4 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 docs/helpdocs/bicep/output.md diff --git a/cmd/bicep.go b/cmd/bicep.go index 895679a..b57c0ed 100644 --- a/cmd/bicep.go +++ b/cmd/bicep.go @@ -3,6 +3,7 @@ package cmd import ( "encoding/json" "fmt" + "os" "github.com/massdriver-cloud/airlock/docs/helpdocs" "github.com/massdriver-cloud/airlock/pkg/bicep" @@ -16,16 +17,26 @@ func NewCmdBicep() *cobra.Command { Long: helpdocs.MustRender("bicep"), } - // Import + // Input bicepInputCmd := &cobra.Command{ Use: `input`, - Short: "Ingest a bicep template file and generate a JSON Schema", + Short: "Ingest a Bicep template file and generate a JSON Schema from the params", Args: cobra.ExactArgs(1), Long: helpdocs.MustRender("bicep/input"), RunE: runBicepInput, } + // Output + bicepOutputCmd := &cobra.Command{ + Use: "output", + Short: "Output a Bicep params specification from a JSON Schema document", + Args: cobra.ExactArgs(1), + Long: helpdocs.MustRender("bicep/output"), + RunE: runBicepOutput, + } + bicepCmd.AddCommand(bicepInputCmd) + bicepCmd.AddCommand(bicepOutputCmd) return bicepCmd } @@ -44,3 +55,27 @@ func runBicepInput(cmd *cobra.Command, args []string) error { fmt.Println(string(bytes)) return nil } + +func runBicepOutput(cmd *cobra.Command, args []string) error { + schemaPath := args[0] + + var err error + var in *os.File + if schemaPath == "-" { + in = os.Stdin + } else { + in, err = os.Open(schemaPath) + if err != nil { + return err + } + defer in.Close() + } + + bytes, err := bicep.SchemaToBicep(in) + if err != nil { + return err + } + + fmt.Printf("%s", bytes) + return nil +} diff --git a/cmd/opentofu.go b/cmd/opentofu.go index 5e6d9a2..9ddb373 100644 --- a/cmd/opentofu.go +++ b/cmd/opentofu.go @@ -21,16 +21,16 @@ func NewCmdOpenTofu() *cobra.Command { // Input opentofuInputCmd := &cobra.Command{ Use: `input`, - Short: "Ingest a OpenTofu module and generate a JSON Schema from the variables", + Short: "Ingest an OpenTofu module and generate a JSON Schema from the variables", Args: cobra.ExactArgs(1), Long: helpdocs.MustRender("opentofu/input"), RunE: runOpenTofuInput, } - // oputput + // Output opentofuOutputCmd := &cobra.Command{ Use: `output`, - Short: "Output a OpenTofu variables specification from a JSON schemea document", + Short: "Output an OpenTofu variables specification from a JSON Schema document", Args: cobra.ExactArgs(1), Long: helpdocs.MustRender("opentofu/output"), RunE: runOpenTofuOutput, diff --git a/docs/helpdocs/bicep/output.md b/docs/helpdocs/bicep/output.md new file mode 100644 index 0000000..e71713c --- /dev/null +++ b/docs/helpdocs/bicep/output.md @@ -0,0 +1,9 @@ +# Translate from a JSON Schema to Bicep Params + +This command will translate from a JSON Schema document into a set of formatted Bicep param declarations. + +## Examples + +```shell +airlock bicep output path/to/schema.json +``` diff --git a/pkg/bicep/schematobicep.go b/pkg/bicep/schematobicep.go index 35bcc49..05fb231 100644 --- a/pkg/bicep/schematobicep.go +++ b/pkg/bicep/schematobicep.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "reflect" + "sort" "github.com/massdriver-cloud/airlock/pkg/schema" ) @@ -199,7 +200,15 @@ func parseArray(arr []interface{}, prefix string) (string, error) { func parseObject(obj map[string]interface{}, prefix string) (string, error) { parsedObj := "{\n" - for k, v := range obj { + keys := make([]string, 0, len(obj)) + for k := range obj { + keys = append(keys, k) + } + + sort.Strings(keys) + + for _, k := range keys { + v := obj[k] renderedVal, err := renderBicep(v, prefix+indent) if err != nil { return "", err