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

Renaming pkg/cmd terraform -> opentofu #11

Merged
merged 6 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
go get -u github.com/massdriver-cloud/airlock
83 changes: 83 additions & 0 deletions cmd/opentofu.go
Original file line number Diff line number Diff line change
@@ -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{"tf", "terraform"},
chrisghill marked this conversation as resolved.
Show resolved Hide resolved
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
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
83 changes: 0 additions & 83 deletions cmd/terraform.go

This file was deleted.

1 change: 1 addition & 0 deletions docs/helpdocs/opentofu.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Translate between OpenTofu variables and JSON Schemas
Original file line number Diff line number Diff line change
@@ -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/
```
9 changes: 9 additions & 0 deletions docs/helpdocs/opentofu/output.md
Original file line number Diff line number Diff line change
@@ -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
```
1 change: 0 additions & 1 deletion docs/helpdocs/terraform.md

This file was deleted.

9 changes: 0 additions & 9 deletions docs/helpdocs/terraform/output.md

This file was deleted.

Binary file added main
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You checked in a binary file.

Binary file not shown.
4 changes: 2 additions & 2 deletions pkg/terraform/schematotf.go → pkg/opentofu/schematotf.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package terraform
package opentofu

import (
"encoding/json"
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package terraform
package opentofu

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package terraform_test
package opentofu_test

import (
"encoding/json"
"os"
"path/filepath"
"testing"

"github.com/massdriver-cloud/airlock/pkg/terraform"
"github.com/massdriver-cloud/airlock/pkg/opentofu"

"github.com/stretchr/testify/require"
)
Expand All @@ -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)
}
Expand Down