github.com/tetrafolium/tflint@v0.8.0/cmd/option.go (about)

     1  package cmd
     2  
     3  import (
     4  	"log"
     5  	"strings"
     6  
     7  	"github.com/wata727/tflint/client"
     8  	"github.com/wata727/tflint/rules/awsrules"
     9  	"github.com/wata727/tflint/tflint"
    10  )
    11  
    12  // Options is an option specified by arguments.
    13  type Options struct {
    14  	Version         bool   `short:"v" long:"version" description:"Print TFLint version"`
    15  	Format          string `short:"f" long:"format" description:"Output format" choice:"default" choice:"json" choice:"checkstyle" default:"default"`
    16  	Config          string `short:"c" long:"config" description:"Config file name" value-name:"FILE" default:".tflint.hcl"`
    17  	IgnoreModule    string `long:"ignore-module" description:"Ignore module sources" value-name:"SOURCE1,SOURCE2..."`
    18  	IgnoreRule      string `long:"ignore-rule" description:"Ignore rule names" value-name:"RULE1,RULE2..."`
    19  	Varfile         string `long:"var-file" description:"Terraform variable file names" value-name:"FILE1,FILE2..."`
    20  	Deep            bool   `long:"deep" description:"Enable deep check mode"`
    21  	AwsAccessKey    string `long:"aws-access-key" description:"AWS access key used in deep check mode" value-name:"ACCESS_KEY"`
    22  	AwsSecretKey    string `long:"aws-secret-key" description:"AWS secret key used in deep check mode" value-name:"SECRET_KEY"`
    23  	AwsProfile      string `long:"aws-profile" description:"AWS shared credential profile name used in deep check mode" value-name:"PROFILE"`
    24  	AwsRegion       string `long:"aws-region" description:"AWS region used in deep check mode" value-name:"REGION"`
    25  	ErrorWithIssues bool   `long:"error-with-issues" description:"Return error code when issues exist"`
    26  	Fast            bool   `long:"fast" description:"Ignore slow rules (aws_instance_invalid_ami only)"`
    27  	Quiet           bool   `short:"q" long:"quiet" description:"Do not output any message when no issues are found (default format only)"`
    28  }
    29  
    30  func (opts *Options) toConfig() *tflint.Config {
    31  	ignoreModule := map[string]bool{}
    32  	if opts.IgnoreModule != "" {
    33  		for _, m := range strings.Split(opts.IgnoreModule, ",") {
    34  			ignoreModule[m] = true
    35  		}
    36  	}
    37  
    38  	ignoreRule := map[string]bool{}
    39  	if opts.IgnoreRule != "" {
    40  		for _, r := range strings.Split(opts.IgnoreRule, ",") {
    41  			ignoreRule[r] = true
    42  		}
    43  	}
    44  	if opts.Fast {
    45  		// `aws_instance_invalid_ami` is very slow...
    46  		ignoreRule[awsrules.NewAwsInstanceInvalidAMIRule().Name()] = true
    47  	}
    48  
    49  	varfile := []string{}
    50  	if opts.Varfile != "" {
    51  		varfile = strings.Split(opts.Varfile, ",")
    52  	}
    53  
    54  	log.Printf("[DEBUG] CLI Options")
    55  	log.Printf("[DEBUG]   DeepCheck: %t", opts.Deep)
    56  	log.Printf("[DEBUG]   IgnoreModule: %#v", ignoreModule)
    57  	log.Printf("[DEBUG]   IgnoreRule: %#v", ignoreRule)
    58  	log.Printf("[DEBUG]   Varfile: %#v", varfile)
    59  
    60  	return &tflint.Config{
    61  		DeepCheck: opts.Deep,
    62  		AwsCredentials: client.AwsCredentials{
    63  			AccessKey: opts.AwsAccessKey,
    64  			SecretKey: opts.AwsSecretKey,
    65  			Profile:   opts.AwsProfile,
    66  			Region:    opts.AwsRegion,
    67  		},
    68  		IgnoreModule:     ignoreModule,
    69  		IgnoreRule:       ignoreRule,
    70  		Varfile:          varfile,
    71  		TerraformVersion: "",
    72  		Rules:            map[string]*tflint.RuleConfig{},
    73  	}
    74  }