Skip to content

Commit

Permalink
Adding schematobicep command (#18)
Browse files Browse the repository at this point in the history
* Adding schematobicep command

* errcheck lint fix

* Fix for test ordering issue
  • Loading branch information
mclacore authored Nov 21, 2024
1 parent 82417a3 commit 79a92c8
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
39 changes: 37 additions & 2 deletions cmd/bicep.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand All @@ -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
}
6 changes: 3 additions & 3 deletions cmd/opentofu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions docs/helpdocs/bicep/output.md
Original file line number Diff line number Diff line change
@@ -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
```
11 changes: 10 additions & 1 deletion pkg/bicep/schematobicep.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"reflect"
"sort"

"github.com/massdriver-cloud/airlock/pkg/schema"
)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 79a92c8

Please sign in to comment.