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

Interpret TFState #48

Merged
merged 3 commits into from
Feb 5, 2017
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
8 changes: 5 additions & 3 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,11 @@ func (cli *CLI) Run(args []string) int {
if !cli.testMode {
cli.loader = loader.NewLoader(c.Debug)
}
cli.loader.LoadState()
if flags.NArg() > 0 {
err = cli.loader.LoadFile(flags.Arg(0))
err = cli.loader.LoadTemplate(flags.Arg(0))
} else {
err = cli.loader.LoadAllFile(".")
err = cli.loader.LoadAllTemplate(".")
}
if err != nil {
fmt.Fprintln(cli.errStream, err)
Expand All @@ -143,7 +144,8 @@ func (cli *CLI) Run(args []string) int {

// If disabled test mode, generates real detector
if !cli.testMode {
cli.detector, err = detector.NewDetector(cli.loader.DumpFiles(), c)
listMap, state := cli.loader.Dump()
cli.detector, err = detector.NewDetector(listMap, state, c)
}
if err != nil {
fmt.Fprintln(cli.errStream, err)
Expand Down
12 changes: 8 additions & 4 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ func TestCLIRun(t *testing.T) {
}
var loaderDefaultBehavior = func(ctrl *gomock.Controller) loader.LoaderIF {
loader := mock.NewMockLoaderIF(ctrl)
loader.EXPECT().LoadAllFile(".").Return(nil)
loader.EXPECT().LoadState()
loader.EXPECT().LoadAllTemplate(".").Return(nil)
return loader
}
var detectorNoErrorNoIssuesBehavior = func(ctrl *gomock.Controller) detector.DetectorIF {
Expand Down Expand Up @@ -153,7 +154,8 @@ func TestCLIRun(t *testing.T) {
Command: "./tflint",
LoaderGenerator: func(ctrl *gomock.Controller) loader.LoaderIF {
loader := mock.NewMockLoaderIF(ctrl)
loader.EXPECT().LoadAllFile(".").Return(errors.New("loading error!"))
loader.EXPECT().LoadState()
loader.EXPECT().LoadAllTemplate(".").Return(errors.New("loading error!"))
return loader
},
DetectorGenerator: func(ctrl *gomock.Controller) detector.DetectorIF { return mock.NewMockDetectorIF(ctrl) },
Expand Down Expand Up @@ -186,7 +188,8 @@ func TestCLIRun(t *testing.T) {
Command: "./tflint test_template.tf",
LoaderGenerator: func(ctrl *gomock.Controller) loader.LoaderIF {
loader := mock.NewMockLoaderIF(ctrl)
loader.EXPECT().LoadFile("test_template.tf").Return(nil)
loader.EXPECT().LoadState()
loader.EXPECT().LoadTemplate("test_template.tf").Return(nil)
return loader
},
DetectorGenerator: detectorNoErrorNoIssuesBehavior,
Expand All @@ -201,7 +204,8 @@ func TestCLIRun(t *testing.T) {
Command: "./tflint test_template.tf",
LoaderGenerator: func(ctrl *gomock.Controller) loader.LoaderIF {
loader := mock.NewMockLoaderIF(ctrl)
loader.EXPECT().LoadFile("test_template.tf").Return(errors.New("loading error!"))
loader.EXPECT().LoadState()
loader.EXPECT().LoadTemplate("test_template.tf").Return(errors.New("loading error!"))
return loader
},
DetectorGenerator: func(ctrl *gomock.Controller) detector.DetectorIF { return mock.NewMockDetectorIF(ctrl) },
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (c *Config) LoadConfig(filename string) error {
}

l := loader.NewLoader(c.Debug)
if err := l.LoadFile(filename); err != nil {
if err := l.LoadTemplate(filename); err != nil {
return nil
}

Expand Down
7 changes: 5 additions & 2 deletions detector/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/wata727/tflint/evaluator"
"github.com/wata727/tflint/issue"
"github.com/wata727/tflint/logger"
"github.com/wata727/tflint/state"
)

type DetectorIF interface {
Expand All @@ -20,6 +21,7 @@ type DetectorIF interface {

type Detector struct {
ListMap map[string]*ast.ObjectList
State *state.TFState
Config *config.Config
AwsClient *config.AwsClient
EvalConfig *evaluator.Evaluator
Expand Down Expand Up @@ -59,14 +61,15 @@ var detectors = map[string]string{
"aws_elasticache_cluster_previous_type": "CreateAwsElastiCacheClusterPreviousTypeDetector",
}

func NewDetector(listMap map[string]*ast.ObjectList, c *config.Config) (*Detector, error) {
func NewDetector(listMap map[string]*ast.ObjectList, state *state.TFState, c *config.Config) (*Detector, error) {
evalConfig, err := evaluator.NewEvaluator(listMap, c)
if err != nil {
return nil, err
}

return &Detector{
ListMap: listMap,
State: state,
Config: c,
AwsClient: c.NewAwsClient(),
EvalConfig: evalConfig,
Expand Down Expand Up @@ -142,7 +145,7 @@ func (d *Detector) Detect() []*issue.Issue {
continue
}
d.Logger.Info(fmt.Sprintf("detect module `%s`", name))
moduleDetector, err := NewDetector(m.ListMap, d.Config)
moduleDetector, err := NewDetector(m.ListMap, d.State, d.Config)
if err != nil {
d.Logger.Error(err)
continue
Expand Down
57 changes: 46 additions & 11 deletions loader/loader.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package loader

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
Expand All @@ -10,29 +11,33 @@ import (
"github.com/hashicorp/hcl/hcl/ast"
"github.com/hashicorp/hcl/hcl/parser"
"github.com/wata727/tflint/logger"
"github.com/wata727/tflint/state"
)

type LoaderIF interface {
LoadFile(filename string) error
LoadTemplate(filename string) error
LoadModuleFile(moduleKey string, source string) error
LoadAllFile(dir string) error
DumpFiles() map[string]*ast.ObjectList
LoadAllTemplate(dir string) error
Dump() (map[string]*ast.ObjectList, *state.TFState)
LoadState()
}

type Loader struct {
Logger *logger.Logger
ListMap map[string]*ast.ObjectList
State *state.TFState
}

func NewLoader(debug bool) *Loader {
return &Loader{
Logger: logger.Init(debug),
ListMap: make(map[string]*ast.ObjectList),
State: &state.TFState{},
}
}

func (l *Loader) LoadFile(filename string) error {
list, err := load(filename, l.Logger)
func (l *Loader) LoadTemplate(filename string) error {
list, err := loadHCL(filename, l.Logger)
if err != nil {
return err
}
Expand All @@ -55,7 +60,7 @@ func (l *Loader) LoadModuleFile(moduleKey string, source string) error {
}

for _, file := range files {
list, err := load(file, l.Logger)
list, err := loadHCL(file, l.Logger)
if err != nil {
return err
}
Expand All @@ -67,7 +72,7 @@ func (l *Loader) LoadModuleFile(moduleKey string, source string) error {
return nil
}

func (l *Loader) LoadAllFile(dir string) error {
func (l *Loader) LoadAllTemplate(dir string) error {
if _, err := os.Stat(dir); err != nil {
return err
}
Expand All @@ -78,7 +83,7 @@ func (l *Loader) LoadAllFile(dir string) error {
}

for _, file := range files {
err := l.LoadFile(file)
err := l.LoadTemplate(file)
if err != nil {
return err
}
Expand All @@ -87,11 +92,41 @@ func (l *Loader) LoadAllFile(dir string) error {
return nil
}

func (l *Loader) DumpFiles() map[string]*ast.ObjectList {
return l.ListMap
func (l *Loader) LoadState() {
l.Logger.Info("Load tfstate...")
var statePath string
// stat local state
if _, err := os.Stat(state.LocalStatePath); err != nil {
l.Logger.Error(err)
// stat remote state
if _, err := os.Stat(state.RemoteStatePath); err != nil {
l.Logger.Error(err)
return
} else {
l.Logger.Info("Remote state detected")
statePath = state.RemoteStatePath
}
} else {
l.Logger.Info("Local state detected")
statePath = state.LocalStatePath
}

jsonBytes, err := ioutil.ReadFile(statePath)
if err != nil {
l.Logger.Error(err)
return
}
if err := json.Unmarshal(jsonBytes, l.State); err != nil {
l.Logger.Error(err)
return
}
}

func (l *Loader) Dump() (map[string]*ast.ObjectList, *state.TFState) {
return l.ListMap, l.State
}

func load(filename string, l *logger.Logger) (*ast.ObjectList, error) {
func loadHCL(filename string, l *logger.Logger) (*ast.ObjectList, error) {
l.Info(fmt.Sprintf("load `%s`", filename))
b, err := ioutil.ReadFile(filename)
if err != nil {
Expand Down
Loading