Skip to content

Commit 9c0292e

Browse files
authored
feat: migrate generate commad from protoconf (#23)
1 parent beff00c commit 9c0292e

File tree

21 files changed

+984
-40
lines changed

21 files changed

+984
-40
lines changed

cmd/generate/command.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package generate
2+
3+
import (
4+
"bytes"
5+
"flag"
6+
"fmt"
7+
"os"
8+
9+
"runtime"
10+
11+
"github.com/mitchellh/cli"
12+
"github.com/protoconf/protoconf-terraform/pkg/importing"
13+
)
14+
15+
type cliCommand struct {
16+
ui cli.Ui
17+
}
18+
19+
type cliConfig struct {
20+
importPath string
21+
outputPath string
22+
}
23+
24+
func newFlagSet() (*flag.FlagSet, *cliConfig) {
25+
flags := flag.NewFlagSet("", flag.ExitOnError)
26+
flags.Usage = func() {
27+
fmt.Fprintln(flags.Output(), "Usage: [OPTION]...")
28+
flags.PrintDefaults()
29+
}
30+
31+
config := &cliConfig{}
32+
flags.StringVar(&config.importPath, "import_path", fmt.Sprintf(".terraform/providers/*/*/*/*/%s_%s", runtime.GOOS, runtime.GOARCH), "Path of terraform plugins")
33+
flags.StringVar(&config.outputPath, "output", "src", "Path to write proto files to.")
34+
35+
return flags, config
36+
}
37+
38+
func (c *cliCommand) Run(args []string) int {
39+
flags, config := newFlagSet()
40+
flags.Parse(args)
41+
42+
g := importing.NewGenerator(config.importPath, config.outputPath, c.ui)
43+
err := g.PopulateProviders()
44+
if err != nil {
45+
c.ui.Error(fmt.Sprintf("Failed to generate providers: %v", err))
46+
return 1
47+
}
48+
err = g.Save()
49+
if err != nil {
50+
c.ui.Error(fmt.Sprintf("failed to write proto files: %v", err))
51+
return 1
52+
}
53+
return 0
54+
}
55+
56+
func (c *cliCommand) Help() string {
57+
var b bytes.Buffer
58+
b.WriteString(c.Synopsis())
59+
b.WriteString("\n")
60+
flags, _ := newFlagSet()
61+
flags.SetOutput(&b)
62+
flags.Usage()
63+
return b.String()
64+
}
65+
66+
func (c *cliCommand) Synopsis() string {
67+
return "Creates proto files from terraform providers schema"
68+
}
69+
70+
// NewCommand is a cli.CommandFactory
71+
func NewCommand() (cli.Command, error) {
72+
ui := &cli.BasicUi{
73+
Reader: os.Stdin,
74+
Writer: os.Stdout,
75+
ErrorWriter: os.Stderr,
76+
}
77+
return &cliCommand{ui: ui}, nil
78+
}

cmd/protoconf-terraform/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66

77
"github.com/mitchellh/cli"
8+
"github.com/protoconf/protoconf-terraform/cmd/generate"
89
initcmd "github.com/protoconf/protoconf-terraform/cmd/init"
910
"github.com/protoconf/protoconf-terraform/cmd/run"
1011
)
@@ -18,8 +19,9 @@ func main() {
1819
cmd := cli.NewCLI("protoconf-terraform", "v0.1.3")
1920
cmd.Args = os.Args[1:]
2021
cmd.Commands = map[string]cli.CommandFactory{
21-
"init": initcmd.NewCommand,
22-
"run": run.NewCommand,
22+
"init": initcmd.NewCommand,
23+
"run": run.NewCommand,
24+
"generate": generate.NewCommand,
2325
}
2426

2527
code, err := cmd.Run()

example/src/terraform/random/provider/v3/random.proto

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
syntax = "proto3";
22

3-
//Provider: random 3.3.2
3+
//Provider: random 3.5.1
44
package terraform.random.provider.v3;
55

66
import "terraform/v1/meta.proto";
77

88
//Random version is 0
99
message Random {
10-
repeated string depends_on = 1 [json_name = "depends_on"];
10+
map<string, string> for_each = 1 [json_name = "for_each"];
1111

12-
int32 count = 2;
12+
repeated string depends_on = 2 [json_name = "depends_on"];
1313

14-
map<string, string> for_each = 3 [json_name = "for_each"];
14+
int32 count = 3;
1515

1616
string provider = 4;
1717

example/src/terraform/random/resources/v3/id.proto

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
syntax = "proto3";
22

3-
//Provider: random 3.3.2
3+
//Provider: random 3.5.1
44
package terraform.random.resources.v3;
55

66
import "terraform/v1/meta.proto";
@@ -31,11 +31,11 @@ message RandomId {
3131
//Arbitrary string to prefix the output value with. This string is supplied as-is, meaning it is not guaranteed to be URL-safe or base64 encoded.
3232
string prefix = 8;
3333

34-
repeated string depends_on = 9 [json_name = "depends_on"];
34+
map<string, string> for_each = 9 [json_name = "for_each"];
3535

36-
int32 count = 10;
36+
repeated string depends_on = 10 [json_name = "depends_on"];
3737

38-
map<string, string> for_each = 11 [json_name = "for_each"];
38+
int32 count = 11;
3939

4040
string provider = 12;
4141

example/src/terraform/random/resources/v3/integer.proto

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
syntax = "proto3";
22

3-
//Provider: random 3.3.2
3+
//Provider: random 3.5.1
44
package terraform.random.resources.v3;
55

66
import "terraform/v1/meta.proto";
@@ -25,11 +25,11 @@ message RandomInteger {
2525
//A custom seed to always produce the same value.
2626
string seed = 6;
2727

28-
repeated string depends_on = 7 [json_name = "depends_on"];
28+
map<string, string> for_each = 7 [json_name = "for_each"];
2929

30-
int32 count = 8;
30+
repeated string depends_on = 8 [json_name = "depends_on"];
3131

32-
map<string, string> for_each = 9 [json_name = "for_each"];
32+
int32 count = 9;
3333

3434
string provider = 10;
3535

example/src/terraform/random/resources/v3/password.proto

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
syntax = "proto3";
22

3-
//Provider: random 3.3.2
3+
//Provider: random 3.5.1
44
package terraform.random.resources.v3;
55

66
import "terraform/v1/meta.proto";
77

8-
//RandomPassword version is 2
8+
//RandomPassword version is 3
99
message RandomPassword {
10-
//A bcrypt hash of the generated random string.
10+
//A bcrypt hash of the generated random string. **NOTE**: If the generated random string is greater than 72 bytes in length, `bcrypt_hash` will contain a hash of the first 72 bytes.
1111
string bcrypt_hash = 1 [json_name = "bcrypt_hash"];
1212

1313
//A static value used internally by Terraform, this should not be referenced in configurations.
@@ -52,11 +52,11 @@ message RandomPassword {
5252
//Include uppercase alphabet characters in the result. Default value is `true`.
5353
bool upper = 15;
5454

55-
repeated string depends_on = 16 [json_name = "depends_on"];
55+
map<string, string> for_each = 16 [json_name = "for_each"];
5656

57-
int32 count = 17;
57+
repeated string depends_on = 17 [json_name = "depends_on"];
5858

59-
map<string, string> for_each = 18 [json_name = "for_each"];
59+
int32 count = 18;
6060

6161
string provider = 19;
6262

example/src/terraform/random/resources/v3/pet.proto

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
syntax = "proto3";
22

3-
//Provider: random 3.3.2
3+
//Provider: random 3.5.1
44
package terraform.random.resources.v3;
55

66
import "terraform/v1/meta.proto";
77

88
//RandomPet version is 0
99
message RandomPet {
10-
//The random pet name
10+
//The random pet name.
1111
string id = 1;
1212

1313
//Arbitrary map of values that, when changed, will trigger recreation of resource. See [the main provider documentation](../index.html) for more information.
@@ -22,11 +22,11 @@ message RandomPet {
2222
//The character to separate words in the pet name. Defaults to "-"
2323
string separator = 5;
2424

25-
repeated string depends_on = 6 [json_name = "depends_on"];
25+
map<string, string> for_each = 6 [json_name = "for_each"];
2626

27-
int32 count = 7;
27+
repeated string depends_on = 7 [json_name = "depends_on"];
2828

29-
map<string, string> for_each = 8 [json_name = "for_each"];
29+
int32 count = 8;
3030

3131
string provider = 9;
3232

example/src/terraform/random/resources/v3/shuffle.proto

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
syntax = "proto3";
22

3-
//Provider: random 3.3.2
3+
//Provider: random 3.5.1
44
package terraform.random.resources.v3;
55

66
import "terraform/v1/meta.proto";
@@ -27,11 +27,11 @@ message RandomShuffle {
2727
//**Important:** Even with an identical seed, it is not guaranteed that the same permutation will be produced across different versions of Terraform. This argument causes the result to be *less volatile*, but not fixed for all time.
2828
string seed = 6;
2929

30-
repeated string depends_on = 7 [json_name = "depends_on"];
30+
map<string, string> for_each = 7 [json_name = "for_each"];
3131

32-
int32 count = 8;
32+
repeated string depends_on = 8 [json_name = "depends_on"];
3333

34-
map<string, string> for_each = 9 [json_name = "for_each"];
34+
int32 count = 9;
3535

3636
string provider = 10;
3737

example/src/terraform/random/resources/v3/string.proto

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
syntax = "proto3";
22

3-
//Provider: random 3.3.2
3+
//Provider: random 3.5.1
44
package terraform.random.resources.v3;
55

66
import "terraform/v1/meta.proto";
@@ -49,11 +49,11 @@ message RandomString {
4949
//Include uppercase alphabet characters in the result. Default value is `true`.
5050
bool upper = 14;
5151

52-
repeated string depends_on = 15 [json_name = "depends_on"];
52+
map<string, string> for_each = 15 [json_name = "for_each"];
5353

54-
int32 count = 16;
54+
repeated string depends_on = 16 [json_name = "depends_on"];
5555

56-
map<string, string> for_each = 17 [json_name = "for_each"];
56+
int32 count = 17;
5757

5858
string provider = 18;
5959

example/src/terraform/random/resources/v3/uuid.proto

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
syntax = "proto3";
22

3-
//Provider: random 3.3.2
3+
//Provider: random 3.5.1
44
package terraform.random.resources.v3;
55

66
import "terraform/v1/meta.proto";
@@ -16,11 +16,11 @@ message RandomUuid {
1616
//The generated uuid presented in string format.
1717
string result = 3;
1818

19-
repeated string depends_on = 4 [json_name = "depends_on"];
19+
map<string, string> for_each = 4 [json_name = "for_each"];
2020

21-
int32 count = 5;
21+
repeated string depends_on = 5 [json_name = "depends_on"];
2222

23-
map<string, string> for_each = 6 [json_name = "for_each"];
23+
int32 count = 6;
2424

2525
string provider = 7;
2626

0 commit comments

Comments
 (0)