Skip to content

Commit

Permalink
Allow to read a plan file from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
minamijoyo committed Apr 18, 2022
1 parent 2f117ed commit f8dbf09
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cmd/filter.go
Expand Up @@ -11,7 +11,7 @@ import (
func init() {
filterCmd := newFilterCmd()
flags := filterCmd.PersistentFlags()
flags.StringP("file", "f", "-", "A path of input file")
flags.StringP("file", "f", "-", "A path to input Terraform configuration file")
flags.BoolP("update", "u", false, "Update files in-place")
_ = viper.BindPFlag("file", flags.Lookup("file"))
_ = viper.BindPFlag("update", flags.Lookup("update"))
Expand Down
37 changes: 25 additions & 12 deletions cmd/migration.go
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"io"
"os"

"github.com/minamijoyo/tfedit/migration"
Expand All @@ -12,7 +13,9 @@ import (
func init() {
migrationCmd := newMigrationCmd()
flags := migrationCmd.PersistentFlags()
flags.StringP("file", "f", "-", "A path to input Terraform plan file in JSON format")
flags.StringP("out", "o", "-", "Write a migration file to a given path")
_ = viper.BindPFlag("file", flags.Lookup("file"))
_ = viper.BindPFlag("out", flags.Lookup("out"))

RootCmd.AddCommand(migrationCmd)
Expand All @@ -36,17 +39,13 @@ func newMigrationCmd() *cobra.Command {

func newMigrationAwsv4upgradeCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "awsv4upgrade <PATH>",
Use: "awsv4upgrade",
Short: "Generate a migration file for awsv4upgrade",
Long: `Generate a migration file for awsv4upgrade
Generate a migration file as a tfmigrate format.
Read a Terraform plan file in JSON format and
generate a migration file in tfmigrate HCL format.
Only aws_s3_bucket refactor is supported.
Arguments:
PATH A path of Terraform plan file.
Only JSON format is supported.
`,
RunE: runMigrationAwsv4upgradeCmd,
}
Expand All @@ -55,19 +54,33 @@ Arguments:
}

func runMigrationAwsv4upgradeCmd(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("expected 1 argument, but got %d arguments", len(args))
if len(args) != 0 {
return fmt.Errorf("expected 0 argument, but got %d arguments", len(args))
}

planFile := args[0]
planFile := viper.GetString("file")
migrationFile := viper.GetString("out")
output, err := migration.Generate(planFile)

var planJSON []byte
var err error
if planFile == "-" {
planJSON, err = io.ReadAll(cmd.InOrStdin())
if err != nil {
return fmt.Errorf("failed to read input from stdin: %s", err)
}
} else {
planJSON, err = os.ReadFile(planFile)
if err != nil {
return fmt.Errorf("failed to read file: %s", err)
}
}

output, err := migration.Generate(planJSON)
if err != nil {
return err
}

if migrationFile == "-" {
// Write to stdout
fmt.Fprintf(cmd.OutOrStdout(), string(output))
} else {
if err := os.WriteFile(migrationFile, output, os.ModePerm); err != nil {
Expand Down
10 changes: 2 additions & 8 deletions migration/migration.go
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"os"
"text/template"

tfjson "github.com/hashicorp/terraform-json"
Expand All @@ -29,14 +28,9 @@ func (r *AWSS3BucketACLFilterResource) ID() string {
return r.Bucket + "," + r.ACL
}

func Generate(planFile string) ([]byte, error) {
b, err := os.ReadFile(planFile)
if err != nil {
return nil, fmt.Errorf("failed to read file: %s", err)
}

func Generate(planJSON []byte) ([]byte, error) {
var plan tfjson.Plan
if err := json.Unmarshal(b, &plan); err != nil {
if err := json.Unmarshal(planJSON, &plan); err != nil {
return nil, fmt.Errorf("failed to parse plan file: %s", err)
}

Expand Down

0 comments on commit f8dbf09

Please sign in to comment.