Skip to content

Commit

Permalink
fix style
Browse files Browse the repository at this point in the history
  • Loading branch information
acc-jon committed Oct 28, 2020
1 parent 24c4948 commit 577d5ca
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 49 deletions.
18 changes: 9 additions & 9 deletions pkg/cli/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (
"fmt"
"os"

"github.com/accurics/terrascan/pkg/config"
"github.com/accurics/terrascan/pkg/logging"
"github.com/spf13/cobra"
"github.com/accurics/terrascan/pkg/config"
"github.com/accurics/terrascan/pkg/logging"
)

// RegisterCommand Registers a new command under the base command
Expand Down Expand Up @@ -59,13 +59,13 @@ func Execute() {
rootCmd.PersistentFlags().StringVarP(&OutputType, "output", "o", "yaml", "output type (json, yaml, xml)")
rootCmd.PersistentFlags().StringVarP(&ConfigFile, "config-path", "c", "", "config file path")

// Function to execute before processing commands
cobra.OnInitialize(func() {
// Set up the logger
logging.Init(LogType, LogLevel)
// Make sure we load the global config from the specified config file
config.LoadGlobalConfig(ConfigFile)
})
// Function to execute before processing commands
cobra.OnInitialize(func() {
// Set up the logger
logging.Init(LogType, LogLevel)
// Make sure we load the global config from the specified config file
config.LoadGlobalConfig(ConfigFile)
})

// parse the flags but hack around to avoid exiting with error code 2 on help
// override usage so that flag.Parse uses root command's usage instead of default one when invoked with -h
Expand Down
13 changes: 8 additions & 5 deletions pkg/config/configfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ import (
)

var (
ErrTomlLoadConfig = fmt.Errorf("failed to load toml config")
ErrNotPresent = fmt.Errorf("config file not present")
// ErrTomlLoadConfig indicates error: Failed to load toml config
ErrTomlLoadConfig = fmt.Errorf("failed to load toml config")
// ErrNotPresent indicates error: Config file not present
ErrNotPresent = fmt.Errorf("config file not present")
)

// NewNotifiers returns a list of notifiers configured in the config file
// LoadConfig loads a configuration from specified path and
// returns a *toml.Tree with the contents of the config file
func LoadConfig(configFile string) (*toml.Tree, error) {

// empty config file path
Expand All @@ -41,14 +44,14 @@ func LoadConfig(configFile string) (*toml.Tree, error) {
// check if file exists
_, err := os.Stat(configFile)
if err != nil {
zap.S().Errorf("Can't find '%s'", configFile)
zap.S().Errorf("Can't find '%s'", configFile)
return nil, ErrNotPresent
}

// parse toml config file
config, err := toml.LoadFile(configFile)
if err != nil {
zap.S().Errorf("Error loading '%s': %v", configFile, err)
zap.S().Errorf("Error loading '%s': %v", configFile, err)
return nil, ErrTomlLoadConfig
}

Expand Down
59 changes: 31 additions & 28 deletions pkg/config/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@
package config

import (
"os"
"fmt"
"github.com/pelletier/go-toml"
"go.uber.org/zap"
"github.com/pelletier/go-toml"
"go.uber.org/zap"
"os"
)

const (
policyRepoURL = "https://github.com/accurics/terrascan.git"
policyBranch = "master"
configEnvvarName = "TERRASCAN_CONFIG"
policyConfigKey = "policy"
policyConfigKey = "policy"
)

var (
policyRepoPath = os.Getenv("HOME") + "/.terrascan"
policyBasePath = policyRepoPath + "/pkg/policies/opa/rego"
errTomlKeyNotPresent = fmt.Errorf("%s key not present in toml config", policyConfigKey)
policyRepoPath = os.Getenv("HOME") + "/.terrascan"
policyBasePath = policyRepoPath + "/pkg/policies/opa/rego"
errTomlKeyNotPresent = fmt.Errorf("%s key not present in toml config", policyConfigKey)
)

func init() {
Expand All @@ -43,6 +43,9 @@ func init() {
LoadGlobalConfig(os.Getenv(configEnvvarName))
}

// LoadGlobalConfig loads policy configuration from specified configFile
// into var Global.Policy. Members of Global.Policy that are not specified
// in configFile will get default values
func LoadGlobalConfig(configFile string) {
// Start with the defaults
Global.Policy = PolicyConfig{
Expand Down Expand Up @@ -76,37 +79,37 @@ func LoadGlobalConfig(configFile string) {
func loadConfigFile(configFile string) (GlobalConfig, error) {
p := GlobalConfig{}

config, err := LoadConfig(configFile)
config, err := LoadConfig(configFile)
if err != nil {
return p, ErrNotPresent
}

keyConfig := config.Get(policyConfigKey)
if keyConfig == nil {
return p, errTomlKeyNotPresent
}
keyConfig := config.Get(policyConfigKey)
if keyConfig == nil {
return p, errTomlKeyNotPresent
}

keyTomlConfig := keyConfig.(*toml.Tree)
keyTomlConfig := keyConfig.(*toml.Tree)

// We want to treat missing keys as empty strings
str := func(x interface{}) string {
if x == nil {
return ""
}
return x.(string)
}
// We want to treat missing keys as empty strings
str := func(x interface{}) string {
if x == nil {
return ""
}
return x.(string)
}

// path = path where repo will be checked out
p.Policy.BasePath = str(keyTomlConfig.Get("path"))
// path = path where repo will be checked out
p.Policy.BasePath = str(keyTomlConfig.Get("path"))

// repo_url = git url to policy repository
p.Policy.RepoURL = str(keyTomlConfig.Get("repo_url"))
// repo_url = git url to policy repository
p.Policy.RepoURL = str(keyTomlConfig.Get("repo_url"))

// rego_subdir = subdir of <path> where rego files are located
p.Policy.RepoPath = str(keyTomlConfig.Get("rego_subdir"))
// rego_subdir = subdir of <path> where rego files are located
p.Policy.RepoPath = str(keyTomlConfig.Get("rego_subdir"))

// branch = git branch where policies are stored
p.Policy.Branch = str(keyTomlConfig.Get("branch"))
// branch = git branch where policies are stored
p.Policy.Branch = str(keyTomlConfig.Get("branch"))

return p, nil
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/notifications/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"fmt"
"reflect"

"github.com/accurics/terrascan/pkg/utils"
"github.com/accurics/terrascan/pkg/config"
"github.com/accurics/terrascan/pkg/utils"
"github.com/pelletier/go-toml"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -54,10 +54,10 @@ func NewNotifiers(configFile string) ([]Notifier, error) {

var notifiers []Notifier

config, err := config.LoadConfig(configFile)
if err != nil || config == nil{
return notifiers, err
}
config, err := config.LoadConfig(configFile)
if err != nil || config == nil {
return notifiers, err
}

// get config for 'notifications'
keyConfig := config.Get(notificationsConfigKey)
Expand Down
2 changes: 1 addition & 1 deletion pkg/notifications/notifiers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"reflect"
"testing"

"github.com/accurics/terrascan/pkg/notifications/webhook"
"github.com/accurics/terrascan/pkg/config"
"github.com/accurics/terrascan/pkg/notifications/webhook"
)

func TestNewNotifier(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/runtime/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ import (
"reflect"
"testing"

"github.com/accurics/terrascan/pkg/config"
iacProvider "github.com/accurics/terrascan/pkg/iac-providers"
"github.com/accurics/terrascan/pkg/iac-providers/output"
tfv12 "github.com/accurics/terrascan/pkg/iac-providers/terraform/v12"
"github.com/accurics/terrascan/pkg/notifications"
"github.com/accurics/terrascan/pkg/notifications/webhook"
"github.com/accurics/terrascan/pkg/policy"
"github.com/accurics/terrascan/pkg/config"
)

var (
Expand Down

0 comments on commit 577d5ca

Please sign in to comment.