Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding schematobicep command #18

Merged
merged 4 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading