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

Add go modules and refactor packages #8

Merged
merged 2 commits into from
Apr 3, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
terraform-variables-generator
10 changes: 7 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
language: go
go:
- 1.9
- 1.12
env:
- GO111MODULE=on
# Don't email me the results of the test runs.
notifications:
email: false
before_script:
- go get github.com/golang/lint/golint # Linter
- go get -u golang.org/x/lint/golint # Linter
- go get -u github.com/golangci/golangci-lint/cmd/golangci-lint

script:
- go test -v -race ./... # Run all the tests with the race detector enabled
- go vet ./... # go vet is the official Go static analyzer
- golint -set_exit_status $(go list ./...) # one last linter
- golint -set_exit_status $(go list ./...) # one last linter
- golangci-lint run # some more linters
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## (Unreleased)

ENHANCEMENTS:
* Optimize project structure, split to separate packages and add cobra for cli
63 changes: 63 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"

Choose a reason for hiding this comment

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

what are your thoughts about cobra vs kingpin?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://github.com/alecthomas/kingpin last commit 9 month ago, either it's perfect or dead. I'll check if we can use it.


log "github.com/sirupsen/logrus"

"github.com/alexandrst88/terraform-variables-generator/pkg/generator"
"github.com/alexandrst88/terraform-variables-generator/pkg/utils"
)

const (
tfFileExt = "*.tf"
)

var (
generatorVersion string

vars bool
varsFile string
)

// Execute will run main logic
func Execute(version string) {
generatorVersion = version

cmd := &cobra.Command{
Use: "generator",
Short: "CLI for generating terraform variables",
Example: " terraform-variable-generator",
Version: generatorVersion,
Run: runGenerator,
}

cmd.PersistentFlags().BoolVar(&vars, "vars", true, "generate variables")
cmd.PersistentFlags().StringVar(&varsFile, "vars-file", "./variables.tf", "path to generated variables file")

if err := cmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
os.Exit(0)
}

func runGenerator(cmd *cobra.Command, args []string) {
if vars {
if utils.FileExists(varsFile) {
utils.UserPromt(varsFile)
}

tfFiles, err := utils.GetAllFiles(tfFileExt)
utils.CheckError(err)
if len(tfFiles) == 0 {
log.Warn("No terraform files to proceed, exiting")
return
}

generator.GenerateVars(tfFiles, varsFile)
}
}
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/alexandrst88/terraform-variables-generator

go 1.12

require (
github.com/sirupsen/logrus v1.4.1
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3 // indirect
)
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.4.1 h1:GL2rEmy6nsikmW0r8opw9JIRScdMF5hA8cOYLH7In1k=
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8=
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 h1:I6FyU15t786LL7oL/hn43zqTuEGr4PN7F4XJ1p4E3Y8=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
37 changes: 0 additions & 37 deletions helpers.go

This file was deleted.

10 changes: 10 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import "github.com/alexandrst88/terraform-variables-generator/cmd"

// Version is updated by linker flags during build time
var Version = "dev"

func main() {
cmd.Execute(Version)
}
File renamed without changes.
8 changes: 5 additions & 3 deletions terraform.go → pkg/generator/terraform.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main
package generator

import (
"regexp"
"sort"
"strings"

"github.com/alexandrst88/terraform-variables-generator/pkg/utils"
)

type terraformVars struct {
Expand All @@ -16,13 +18,13 @@ func (t *terraformVars) matchVarPref(row, varPrefix string) {
match := pattern.FindAllStringSubmatch(row, -1)
for _, m := range match {
res := replacer.Replace(m[0])
if !containsElement(t.Variables, res) {
if !utils.ContainsElement(t.Variables, res) {
t.Variables = append(t.Variables, res)
}
}
}
}

func (t *terraformVars) sortVars() {
sort.Sort(sort.StringSlice(t.Variables))
sort.Strings(t.Variables)
}
23 changes: 7 additions & 16 deletions generator.go → pkg/generator/vars.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package generator

import (
"bufio"
Expand All @@ -8,13 +8,13 @@ import (
"sync"

log "github.com/sirupsen/logrus"

"github.com/alexandrst88/terraform-variables-generator/pkg/utils"
)

var replacer *strings.Replacer
var varPrefix = "var."
var tfFileExt = "*.tf"

var dstFile = "./variables.tf"
var varTemplate = template.Must(template.New("var_file").Parse(`{{range .}}
variable "{{ . }}" {
description = ""
Expand All @@ -35,17 +35,9 @@ func init() {
" ", "",
)
}
func main() {
if fileExists(dstFile) {
userPromt()
}

tfFiles, err := getAllFiles(tfFileExt)
if len(tfFiles) == 0 {
log.Warn("No terraform files to proceed, exiting")
os.Exit(0)
}
checkError(err)
// GenerateVars will write generated vars to file
func GenerateVars(tfFiles []string, dstFile string) {
var wg sync.WaitGroup
messages := make(chan string)
wg.Add(len(tfFiles))
Expand All @@ -69,11 +61,10 @@ func main() {
}()
wg.Wait()
f, err := os.Create(dstFile)
checkError(err)
utils.CheckError(err)

t.sortVars()
err = varTemplate.Execute(f, t.Variables)
checkError(err)
utils.CheckError(err)
log.Infof("Variables are generated to %q file", dstFile)

}
15 changes: 8 additions & 7 deletions generator_test.go → pkg/generator/vars_test.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
package main
package generator

import (
"bufio"
"os"
"testing"

"github.com/alexandrst88/terraform-variables-generator/pkg/utils"
)

var mockFile = "tf_configuration.mock"
var testExtFile = "*.mock"

func TestContainsElement(t *testing.T) {
testSlice := []string{"Terraform", "Puppet", "Ansible"}
if containsElement(testSlice, "Chef") {
if utils.ContainsElement(testSlice, "Chef") {
t.Error("Should return false, but return true")
}
}

func TestGetAllFiles(t *testing.T) {
files, err := getAllFiles(testExtFile)
checkError(err)
files, err := utils.GetAllFiles(testExtFile)
utils.CheckError(err)
if len(files) == 0 {
t.Error("Should found at least one file")
}
Expand All @@ -28,8 +29,8 @@ func TestMatchVariable(t *testing.T) {
ter := &terraformVars{}
var messages []string

file, err := getAllFiles(testExtFile)
checkError(err)
file, err := utils.GetAllFiles(testExtFile)
utils.CheckError(err)

fileHandle, _ := os.Open(file[0])
defer fileHandle.Close()
Expand Down
13 changes: 8 additions & 5 deletions file_utils.go → pkg/utils/file_utils.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package utils

import (
"os"
Expand All @@ -7,21 +7,24 @@ import (
log "github.com/sirupsen/logrus"
)

func getAllFiles(ext string) ([]string, error) {
// GetAllFiles will get all files in current directory
func GetAllFiles(ext string) ([]string, error) {
dir, err := os.Getwd()
checkError(err)
CheckError(err)

var files []string
log.Infof("Finding files in %q directory", dir)
files, err = filepath.Glob(ext)
checkError(err)
CheckError(err)

if len(files) == 0 {
log.Infof("No files with %q extensions found in %q", ext, dir)
}
return files, nil
}

func fileExists(name string) bool {
// FileExists checks if file exists
func FileExists(name string) bool {
if _, err := os.Stat(name); err == nil {
return true
}
Expand Down
40 changes: 40 additions & 0 deletions pkg/utils/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package utils

import (
"fmt"
"os"

log "github.com/sirupsen/logrus"
)

// CheckError is a convenient wrapper for error check
func CheckError(e error) {
if e != nil {
log.Fatal(e)
}
}

// ContainsElement check if slice of strings contains provided string
func ContainsElement(slice []string, value string) bool {
if len(slice) == 0 {
return false
}
for _, s := range slice {
if value == s {
return true
}
}
return false
}

// UserPromt will ask user if file needs to be overridden
func UserPromt(dstFile string) {
var response string
log.Warnf("File %q already exists, type 'yes' if you want replace it", dstFile)
fmt.Print("-> ")
_, err := fmt.Scanln(&response)
CheckError(err)
if response != "yes" {
os.Exit(0)
}
}