-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package tfexec | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
type formatConfig struct { | ||
recursive bool | ||
dir string | ||
} | ||
|
||
var defaultFormatConfig = formatConfig{ | ||
recursive: false, | ||
} | ||
|
||
type FormatOption interface { | ||
configureFormat(*formatConfig) | ||
} | ||
|
||
func (opt *RecursiveOption) configureFormat(conf *formatConfig) { | ||
conf.recursive = opt.recursive | ||
} | ||
|
||
func (opt *DirOption) configureFormat(conf *formatConfig) { | ||
conf.dir = opt.path | ||
} | ||
|
||
func FormatString(ctx context.Context, execPath string, content string) (string, error) { | ||
tf, err := NewTerraform(filepath.Dir(execPath), execPath) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return tf.FormatString(ctx, content) | ||
} | ||
|
||
func (tf *Terraform) FormatString(ctx context.Context, content string) (string, error) { | ||
cmd, err := tf.formatCmd(ctx, nil, Dir("-")) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
cmd.Stdin = strings.NewReader(content) | ||
|
||
var outBuf bytes.Buffer | ||
cmd.Stdout = mergeWriters(cmd.Stdout, &outBuf) | ||
|
||
err = tf.runTerraformCmd(cmd) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return outBuf.String(), nil | ||
} | ||
|
||
func (tf *Terraform) FormatWrite(ctx context.Context, opts ...FormatOption) error { | ||
for _, o := range opts { | ||
switch o := o.(type) { | ||
case *DirOption: | ||
if o.path == "-" { | ||
return fmt.Errorf("a path of \"-\" is not supported for this method, please use FormatString") | ||
} | ||
} | ||
} | ||
|
||
cmd, err := tf.formatCmd(ctx, []string{"-write=true", "-list=false", "-diff=false"}, opts...) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
panic("not implemented") | ||
panic(cmd) | ||
} | ||
|
||
func (tf *Terraform) FormatCheck(ctx context.Context, opts ...FormatOption) (bool, []string, error) { | ||
for _, o := range opts { | ||
switch o := o.(type) { | ||
case *DirOption: | ||
if o.path == "-" { | ||
return false, nil, fmt.Errorf("a path of \"-\" is not supported for this method, please use FormatString") | ||
} | ||
} | ||
} | ||
|
||
cmd, err := tf.formatCmd(ctx, []string{"-write=false", "-list=true", "-diff=false", "-check=true"}, opts...) | ||
if err != nil { | ||
return false, nil, err | ||
} | ||
|
||
var outBuf bytes.Buffer | ||
cmd.Stdout = mergeWriters(cmd.Stdout, &outBuf) | ||
|
||
err = tf.runTerraformCmd(cmd) | ||
if err != nil { | ||
return false, nil, err | ||
} | ||
|
||
panic("not implemented") | ||
} | ||
|
||
func (tf *Terraform) formatCmd(ctx context.Context, args []string, opts ...FormatOption) (*exec.Cmd, error) { | ||
c := defaultFormatConfig | ||
|
||
for _, o := range opts { | ||
switch o.(type) { | ||
case *RecursiveOption: | ||
err := tf.compatible(ctx, tf0_12_0, nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("-recursive was added to fmt in Terraform 0.12: %w", err) | ||
} | ||
} | ||
|
||
o.configureFormat(&c) | ||
} | ||
|
||
args = append([]string{"fmt", "-no-color"}, args...) | ||
|
||
if c.recursive { | ||
args = append(args, "-recursive") | ||
} | ||
|
||
if c.dir != "" { | ||
args = append(args, c.dir) | ||
} | ||
|
||
return tf.buildTerraformCmd(ctx, args...), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package e2etest | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hashicorp/go-version" | ||
|
||
"github.com/hashicorp/terraform-exec/tfexec" | ||
) | ||
|
||
func TestFormatString(t *testing.T) { | ||
runTest(t, "", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { | ||
unformatted := strings.TrimSpace(` | ||
resource "foo" "bar" { | ||
baz = 1 | ||
qux = 2 | ||
} | ||
`) | ||
|
||
expected := strings.TrimSpace(` | ||
resource "foo" "bar" { | ||
baz = 1 | ||
qux = 2 | ||
} | ||
`) | ||
|
||
actual, err := tf.FormatString(context.Background(), unformatted) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
actual = strings.TrimSpace(actual) | ||
|
||
if actual != expected { | ||
t.Fatalf("expected:\n%s\ngot:\n%s\n", expected, actual) | ||
} | ||
}) | ||
} | ||
|
||
func TestFormatCheck(t *testing.T) { | ||
runTest(t, "unformatted", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { | ||
formatted, files, err := tf.FormatCheck(context.Background()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if formatted { | ||
t.Fatal("expected unformatted") | ||
} | ||
|
||
if !reflect.DeepEqual(files, []string{"file1.tf", "file2.tf"}) { | ||
t.Fatalf("unexpected files list: %#v", files) | ||
} | ||
}) | ||
} | ||
|
||
func TestFormatWrite(t *testing.T) { | ||
t.Skip("not implemented") | ||
runTest(t, "unformatted", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { | ||
|
||
}) | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
resource "foo" "bar" { | ||
baz = 1 | ||
qux = 2 | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
resource "foo" "baz" { | ||
baz = 1 | ||
qux = 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters