diff --git a/README.md b/README.md index fbfb75d..8235231 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ [![License](https://img.shields.io/github/license/massdriver-cloud/airlock)](https://github.com/massdriver-cloud/airlock/blob/master/LICENSE) ## Overview -Generate JSON Schema from various sources (terraform, helm) +Generate JSON Schema from various sources (OpenTofu, Helm) ## Getting Started @@ -17,4 +17,4 @@ Generate JSON Schema from various sources (terraform, helm) To install this package, simply run: ```bash -go get -u github.com/massdriver-cloud/airlock \ No newline at end of file +go get -u github.com/massdriver-cloud/airlock diff --git a/cmd/opentofu.go b/cmd/opentofu.go new file mode 100644 index 0000000..4b93f6a --- /dev/null +++ b/cmd/opentofu.go @@ -0,0 +1,83 @@ +package cmd + +import ( + "encoding/json" + "fmt" + "os" + + "github.com/massdriver-cloud/airlock/docs/helpdocs" + "github.com/massdriver-cloud/airlock/pkg/opentofu" + "github.com/spf13/cobra" +) + +func NewCmdOpenTofu() *cobra.Command { + opentofuCmd := &cobra.Command{ + Use: "opentofu", + Short: "OpenTofu (HCL) translations", + Aliases: []string{"tofu", "tf", "terraform"}, + Long: helpdocs.MustRender("opentofu"), + } + + // Input + opentofuInputCmd := &cobra.Command{ + Use: `input`, + Short: "Ingest a OpenTofu module and generate a JSON Schema from the variables", + Args: cobra.ExactArgs(1), + Long: helpdocs.MustRender("opentofu/input"), + RunE: runOpenTofuInput, + } + + // oputput + opentofuOutputCmd := &cobra.Command{ + Use: `output`, + Short: "Output a OpenTofu variables specification from a JSON schemea document", + Args: cobra.ExactArgs(1), + Long: helpdocs.MustRender("opentofu/output"), + RunE: runOpenTofuOutput, + } + + opentofuCmd.AddCommand(opentofuInputCmd) + opentofuCmd.AddCommand(opentofuOutputCmd) + + return opentofuCmd +} + +func runOpenTofuInput(cmd *cobra.Command, args []string) error { + schema, err := opentofu.TfToSchema(args[0]) + if err != nil { + return err + } + + bytes, err := json.MarshalIndent(schema, "", " ") + if err != nil { + return err + } + + fmt.Println(string(bytes)) + return nil +} + +func runOpenTofuOutput(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 := opentofu.SchemaToTf(in) + if err != nil { + return err + } + + fmt.Printf("%s", bytes) + + return nil +} diff --git a/cmd/root.go b/cmd/root.go index 41de927..5428dfc 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -33,7 +33,7 @@ var rootCmd = &cobra.Command{ // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() { rootCmd.AddCommand(NewCmdHelm()) - rootCmd.AddCommand(NewCmdTerraform()) + rootCmd.AddCommand(NewCmdOpenTofu()) rootCmd.AddCommand(NewCmdValidate()) if err := rootCmd.Execute(); err != nil { os.Exit(1) diff --git a/cmd/terraform.go b/cmd/terraform.go deleted file mode 100644 index 71c1a6c..0000000 --- a/cmd/terraform.go +++ /dev/null @@ -1,83 +0,0 @@ -package cmd - -import ( - "encoding/json" - "fmt" - "os" - - "github.com/massdriver-cloud/airlock/docs/helpdocs" - "github.com/massdriver-cloud/airlock/pkg/terraform" - "github.com/spf13/cobra" -) - -func NewCmdTerraform() *cobra.Command { - terraformCmd := &cobra.Command{ - Use: "terraform", - Short: "Terraform (HCL) translations", - Aliases: []string{"tf", "terraform", "opentofu"}, - Long: helpdocs.MustRender("terraform"), - } - - // Input - terraformInputCmd := &cobra.Command{ - Use: `input`, - Short: "Ingest a terraform module and generate a JSON Schema from the variables", - Args: cobra.ExactArgs(1), - Long: helpdocs.MustRender("terraform/input"), - RunE: runTerraformInput, - } - - // oputput - terraformOutputCmd := &cobra.Command{ - Use: `output`, - Short: "Output a terraform variables specification from a JSON schemea document", - Args: cobra.ExactArgs(1), - Long: helpdocs.MustRender("terraform/output"), - RunE: runTerraformOutput, - } - - terraformCmd.AddCommand(terraformInputCmd) - terraformCmd.AddCommand(terraformOutputCmd) - - return terraformCmd -} - -func runTerraformInput(cmd *cobra.Command, args []string) error { - schema, err := terraform.TfToSchema(args[0]) - if err != nil { - return err - } - - bytes, err := json.MarshalIndent(schema, "", " ") - if err != nil { - return err - } - - fmt.Println(string(bytes)) - return nil -} - -func runTerraformOutput(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 := terraform.SchemaToTf(in) - if err != nil { - return err - } - - fmt.Printf("%s", bytes) - - return nil -} diff --git a/docs/helpdocs/opentofu.md b/docs/helpdocs/opentofu.md new file mode 100644 index 0000000..497ffcb --- /dev/null +++ b/docs/helpdocs/opentofu.md @@ -0,0 +1 @@ +# Translate between OpenTofu variables and JSON Schemas diff --git a/docs/helpdocs/terraform/input.md b/docs/helpdocs/opentofu/input.md similarity index 63% rename from docs/helpdocs/terraform/input.md rename to docs/helpdocs/opentofu/input.md index 5720982..0463917 100644 --- a/docs/helpdocs/terraform/input.md +++ b/docs/helpdocs/opentofu/input.md @@ -1,9 +1,9 @@ -# Translate from a Terraform module to JSON Schema +# Translate from a OpenTofu module to JSON Schema This command will read all files in a directory with `*.tf` suffix, find all variable declaration blocks, and generate a corresponding JSON Schema. ## Examples ```shell -airlock terraform input path/to/terraform/module/ +airlock opentofu input path/to/opentofu/module/ ``` diff --git a/docs/helpdocs/opentofu/output.md b/docs/helpdocs/opentofu/output.md new file mode 100644 index 0000000..fba7653 --- /dev/null +++ b/docs/helpdocs/opentofu/output.md @@ -0,0 +1,9 @@ +# Translate from a JSON Schema to OpenTofu Variables + +This command will translate from a JSON Schema document into a set of HCL formatted OpenTofu variable declaration blocks. + +## Examples + +```shell +airlock opentofu output path/to/schema.json +``` diff --git a/docs/helpdocs/terraform.md b/docs/helpdocs/terraform.md deleted file mode 100644 index 23d6e03..0000000 --- a/docs/helpdocs/terraform.md +++ /dev/null @@ -1 +0,0 @@ -# Translate between Terraform variables and JSON Schemas diff --git a/docs/helpdocs/terraform/output.md b/docs/helpdocs/terraform/output.md deleted file mode 100644 index 2f1334d..0000000 --- a/docs/helpdocs/terraform/output.md +++ /dev/null @@ -1,9 +0,0 @@ -# Translate from a JSON Schema to Terraform Variables - -This command will translate from a JSON Schema document into a set of HCL formatted Terraform variable declaration blocks. - -## Examples - -```shell -airlock terraform output path/to/schema.json -``` diff --git a/pkg/terraform/schematotf.go b/pkg/opentofu/schematotf.go similarity index 97% rename from pkg/terraform/schematotf.go rename to pkg/opentofu/schematotf.go index 2d3d56d..372774a 100644 --- a/pkg/terraform/schematotf.go +++ b/pkg/opentofu/schematotf.go @@ -1,4 +1,4 @@ -package terraform +package opentofu import ( "encoding/json" @@ -87,7 +87,7 @@ func convertArray(node *schema.Schema) hclwrite.Tokens { } func convertMap(node *schema.Schema) hclwrite.Tokens { - // terraform maps must all have the same type for the map value. Therefore there are only limited + // opentofu maps must all have the same type for the map value. Therefore there are only limited // cases where we can interpret a map. Otherwise, we have to give up and just use type "any" // first check: if there are any existing properties, we can't guarantee everything is the same type, so bail diff --git a/pkg/terraform/schematotf_test.go b/pkg/opentofu/schematotf_test.go similarity index 87% rename from pkg/terraform/schematotf_test.go rename to pkg/opentofu/schematotf_test.go index 7ad57f0..c7ab0ea 100644 --- a/pkg/terraform/schematotf_test.go +++ b/pkg/opentofu/schematotf_test.go @@ -1,11 +1,11 @@ -package terraform_test +package opentofu_test import ( "os" "path/filepath" "testing" - "github.com/massdriver-cloud/airlock/pkg/terraform" + "github.com/massdriver-cloud/airlock/pkg/opentofu" ) func TestSchemaToTf(t *testing.T) { @@ -41,7 +41,7 @@ func TestSchemaToTf(t *testing.T) { t.Fatalf("%d, unexpected error", err) } - got, err := terraform.SchemaToTf(schemaFile) + got, err := opentofu.SchemaToTf(schemaFile) if err != nil { t.Fatalf("%d, unexpected error", err) } diff --git a/pkg/terraform/testdata/terraform/simple/schema.json b/pkg/opentofu/testdata/opentofu/simple/schema.json similarity index 100% rename from pkg/terraform/testdata/terraform/simple/schema.json rename to pkg/opentofu/testdata/opentofu/simple/schema.json diff --git a/pkg/terraform/testdata/terraform/simple/variables.tf b/pkg/opentofu/testdata/opentofu/simple/variables.tf similarity index 100% rename from pkg/terraform/testdata/terraform/simple/variables.tf rename to pkg/opentofu/testdata/opentofu/simple/variables.tf diff --git a/pkg/terraform/testdata/schemas/default.json b/pkg/opentofu/testdata/schemas/default.json similarity index 100% rename from pkg/terraform/testdata/schemas/default.json rename to pkg/opentofu/testdata/schemas/default.json diff --git a/pkg/terraform/testdata/schemas/default.tf b/pkg/opentofu/testdata/schemas/default.tf similarity index 100% rename from pkg/terraform/testdata/schemas/default.tf rename to pkg/opentofu/testdata/schemas/default.tf diff --git a/pkg/terraform/testdata/schemas/dependencies.json b/pkg/opentofu/testdata/schemas/dependencies.json similarity index 100% rename from pkg/terraform/testdata/schemas/dependencies.json rename to pkg/opentofu/testdata/schemas/dependencies.json diff --git a/pkg/terraform/testdata/schemas/dependencies.tf b/pkg/opentofu/testdata/schemas/dependencies.tf similarity index 100% rename from pkg/terraform/testdata/schemas/dependencies.tf rename to pkg/opentofu/testdata/schemas/dependencies.tf diff --git a/pkg/terraform/testdata/schemas/dynamics.json b/pkg/opentofu/testdata/schemas/dynamics.json similarity index 100% rename from pkg/terraform/testdata/schemas/dynamics.json rename to pkg/opentofu/testdata/schemas/dynamics.json diff --git a/pkg/terraform/testdata/schemas/dynamics.tf b/pkg/opentofu/testdata/schemas/dynamics.tf similarity index 100% rename from pkg/terraform/testdata/schemas/dynamics.tf rename to pkg/opentofu/testdata/schemas/dynamics.tf diff --git a/pkg/terraform/testdata/schemas/simple.json b/pkg/opentofu/testdata/schemas/simple.json similarity index 100% rename from pkg/terraform/testdata/schemas/simple.json rename to pkg/opentofu/testdata/schemas/simple.json diff --git a/pkg/terraform/testdata/schemas/simple.tf b/pkg/opentofu/testdata/schemas/simple.tf similarity index 100% rename from pkg/terraform/testdata/schemas/simple.tf rename to pkg/opentofu/testdata/schemas/simple.tf diff --git a/pkg/terraform/testdata/schemas/topleveldep.json b/pkg/opentofu/testdata/schemas/topleveldep.json similarity index 100% rename from pkg/terraform/testdata/schemas/topleveldep.json rename to pkg/opentofu/testdata/schemas/topleveldep.json diff --git a/pkg/terraform/testdata/schemas/topleveldep.tf b/pkg/opentofu/testdata/schemas/topleveldep.tf similarity index 100% rename from pkg/terraform/testdata/schemas/topleveldep.tf rename to pkg/opentofu/testdata/schemas/topleveldep.tf diff --git a/pkg/terraform/tftoschema.go b/pkg/opentofu/tftoschema.go similarity index 99% rename from pkg/terraform/tftoschema.go rename to pkg/opentofu/tftoschema.go index 933eea4..d36338f 100644 --- a/pkg/terraform/tftoschema.go +++ b/pkg/opentofu/tftoschema.go @@ -1,4 +1,4 @@ -package terraform +package opentofu import ( "errors" diff --git a/pkg/terraform/tftoschema_test.go b/pkg/opentofu/tftoschema_test.go similarity index 68% rename from pkg/terraform/tftoschema_test.go rename to pkg/opentofu/tftoschema_test.go index 68a417d..f51490b 100644 --- a/pkg/terraform/tftoschema_test.go +++ b/pkg/opentofu/tftoschema_test.go @@ -1,4 +1,4 @@ -package terraform_test +package opentofu_test import ( "encoding/json" @@ -6,7 +6,7 @@ import ( "path/filepath" "testing" - "github.com/massdriver-cloud/airlock/pkg/terraform" + "github.com/massdriver-cloud/airlock/pkg/opentofu" "github.com/stretchr/testify/require" ) @@ -22,14 +22,14 @@ func TestTfToSchema(t *testing.T) { } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - modulePath := filepath.Join("testdata/terraform", tc.name) + modulePath := filepath.Join("testdata/opentofu", tc.name) - want, err := os.ReadFile(filepath.Join("testdata/terraform", tc.name, "schema.json")) + want, err := os.ReadFile(filepath.Join("testdata/opentofu", tc.name, "schema.json")) if err != nil { t.Fatalf("%d, unexpected error", err) } - got, err := terraform.TfToSchema(modulePath) + got, err := opentofu.TfToSchema(modulePath) if err != nil { t.Fatalf("%d, unexpected error", err) }