Skip to content
This repository was archived by the owner on Feb 5, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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 CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Due to their public nature, GitHub and mailing lists are not appropriate places
- Go get a couple of projects nesessary for updating docs and examples:
```shell
$ go get github.com/segmentio/terraform-docs
$ go get github.com/s-urbaniak/terraform-examples
$ go get github.com/coreos/tectonic-installer/contrib/terraform-examples
```

### Contribution Flow
Expand Down Expand Up @@ -104,4 +104,4 @@ git tools.
[modify-installer]: Documentation/contrib/modify-installer.md
[tf-doc]: https://www.terraform.io/docs/index.html
[coreos-golang]: https://github.com/coreos/docs/tree/master/golang
[disclosure]: https://coreos.com/security/disclosure/
[disclosure]: https://coreos.com/security/disclosure/
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ destroy: terraform-init
cd $(BUILD_DIR) && $(TF_CMD) destroy $(TF_DESTROY_OPTIONS) -force $(TOP_DIR)/platforms/$(PLATFORM)

define terraform-docs
$(if $(TF_DOCS),,$(error "terraform-docs revision >= a8b59f8 is required (https://github.com/segmentio/terraform-docs)"))
$(if $(TF_DOCS),,$(error terraform-docs revision >= a8b59f8 is required (https://github.com/segmentio/terraform-docs)))

@echo '<!-- DO NOT EDIT. THIS FILE IS GENERATED BY THE MAKEFILE. -->' > $1
@echo '# Terraform variables' >> $1
Expand All @@ -62,7 +62,7 @@ define terraform-docs
endef

define terraform-examples
$(if $(TF_EXAMPLES),,$(error "terraform-examples revision >= 83d7ad6 is required (https://github.com/s-urbaniak/terraform-examples)"))
$(if $(TF_EXAMPLES),,$(error terraform-examples is required. Execute "go get github.com/coreos/tectonic-installer/contrib/terraform-examples" to install it.))
terraform-examples $2 $3 $4 $5 > $1
endef

Expand Down
3 changes: 3 additions & 0 deletions contrib/terraform-examples/Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[constraint]]
branch = "master"
name = "github.com/hashicorp/hcl"
8 changes: 8 additions & 0 deletions contrib/terraform-examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## terraform-examples

This is a very simple (and most probably incomplete) tool to generate example terraform.tfvars from files or stdin with variable declarations.

Usage:
```
$ usage: terraform-example [-|<FILE>...]
```
213 changes: 213 additions & 0 deletions contrib/terraform-examples/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
package main

import (
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"sort"
"strings"

"github.com/hashicorp/hcl"
"github.com/hashicorp/hcl/hcl/ast"
"github.com/hashicorp/hcl/hcl/token"
)

type Variable struct {
Name string
Description string
Default string
}

type Index struct {
Variables []Variable
}

type ByName []Variable

func (s ByName) Len() int { return len(s) }
func (s ByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s ByName) Less(i, j int) bool { return s[i].Name < s[j].Name }

type stateFunc func(n ast.Node, v *Variable) stateFunc

func newIndex(c []byte) (*Index, error) {
file, err := hcl.ParseBytes(c)
if err != nil {
return nil, err
}

v := &Variable{}
idx := &Index{}
state := idx.content

ast.Walk(file.Node, func(n ast.Node) (ast.Node, bool) {
state = state(n, v)
return n, true
})

idx.Variables = append(idx.Variables, *v)
idx.Variables = idx.Variables[1:]

return idx, nil
}

func (i *Index) content(n ast.Node, v *Variable) stateFunc {
key, ok := n.(*ast.ObjectKey)
if !ok {
return i.content
}

if key.Token.Type != token.IDENT {
return i.content
}

switch key.Token.Text {
case "default":
return i.defaultValue
case "description":
return i.description
case "variable":
i.Variables = append(i.Variables, *v)
*v = Variable{}
return i.variableName
}

return i.content
}

func (i *Index) variableName(n ast.Node, v *Variable) stateFunc {
key, ok := n.(*ast.ObjectKey)
if !ok {
return i.variableName
}

if key.Token.Type != token.STRING {
return i.variableName
}

v.Name = key.Token.Text

return i.content
}

func (i *Index) defaultValue(n ast.Node, v *Variable) stateFunc {
if _, ok := n.(*ast.ListType); ok {
return i.content
}

if _, ok := n.(*ast.ObjectType); ok {
return i.content
}

key, ok := n.(*ast.LiteralType)
if !ok {
return i.defaultValue
}

switch key.Token.Type {
case token.BOOL:
v.Default = key.Token.Text
return i.content
case token.STRING:
v.Default = key.Token.Text
return i.content
}

return i.defaultValue
}

func (i *Index) description(n ast.Node, v *Variable) stateFunc {
key, ok := n.(*ast.LiteralType)
if !ok {
return i.description
}

switch key.Token.Type {
case token.HEREDOC:
v.Description = key.Token.Value().(string)
return i.content
case token.STRING:
v.Description = key.Token.Value().(string)
return i.content
}

return i.description
}

func contents(args []string) ([]byte, error) {
if args[0] == "-" {
if len(args) > 1 {
return nil, errors.New("invalid stdin qualifier: multiple files provided")
}

return ioutil.ReadAll(os.Stdin)
}

var contents []byte

for _, path := range flag.Args() {
c, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}

contents = append(contents, c...)
contents = append(contents, []byte("\n")...)
}

return contents, nil
}

func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: terraform-example [-|<FILE>...]\n")
flag.PrintDefaults()
}
flag.Parse()

if len(flag.Args()) == 0 {
flag.Usage()
os.Exit(1)
}

var c []byte
c, err := contents(flag.Args())
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

idx, err := newIndex(c)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

sort.Sort(ByName(idx.Variables))

for _, v := range idx.Variables {
if strings.HasPrefix(v.Description, "(internal)") {
continue
}

fmt.Printf("\n")

v.Name = strings.Trim(v.Name, `"`)

if strings.HasPrefix(v.Description, "(optional)") {
v.Name = `// ` + v.Name
}

for _, l := range strings.Split(strings.TrimSpace(v.Description), "\n") {
fmt.Printf("// %s\n", l)
}

if v.Default == "" {
fmt.Printf("%s = \"\"\n", v.Name)
} else {
fmt.Printf("%s = %s\n", v.Name, v.Default)
}
}
}
2 changes: 1 addition & 1 deletion images/tectonic-builder/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ RUN go get github.com/jstemmer/go-junit-report

### Tools used by 'make structure-check'
RUN go get github.com/segmentio/terraform-docs
RUN go get github.com/s-urbaniak/terraform-examples
RUN go get github.com/coreos/tectonic-installer/contrib/terraform-examples
RUN go get github.com/bronze1man/yaml2json

### License parser
Expand Down