Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit 6b831e4

Browse files
authored
Merge pull request #165 from rebuy-de/move-config
Move config into separate package
2 parents 3ebe024 + ee6a7d3 commit 6b831e4

File tree

5 files changed

+31
-29
lines changed

5 files changed

+31
-29
lines changed

cmd/nuke.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import (
55
"time"
66

77
"github.com/rebuy-de/aws-nuke/pkg/awsutil"
8+
"github.com/rebuy-de/aws-nuke/pkg/config"
89
"github.com/rebuy-de/aws-nuke/pkg/types"
910
"github.com/rebuy-de/aws-nuke/resources"
1011
)
1112

1213
type Nuke struct {
1314
Parameters NukeParameters
1415
Account awsutil.Account
15-
Config *NukeConfig
16+
Config *config.Nuke
1617

1718
ResourceTypes types.Collection
1819

cmd/root.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"sort"
66

77
"github.com/rebuy-de/aws-nuke/pkg/awsutil"
8+
"github.com/rebuy-de/aws-nuke/pkg/config"
89
"github.com/rebuy-de/aws-nuke/resources"
910
log "github.com/sirupsen/logrus"
1011
"github.com/spf13/cobra"
@@ -52,7 +53,7 @@ func NewRootCommand() *cobra.Command {
5253

5354
n := NewNuke(params, *account)
5455

55-
n.Config, err = LoadConfig(n.Parameters.ConfigPath)
56+
n.Config, err = config.Load(n.Parameters.ConfigPath)
5657
if err != nil {
5758
return err
5859
}

cmd/config.go pkg/config/config.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cmd
1+
package config
22

33
import (
44
"fmt"
@@ -11,32 +11,32 @@ import (
1111
"gopkg.in/yaml.v2"
1212
)
1313

14-
type ResourceConfig struct {
14+
type ResourceTypes struct {
1515
Targets types.Collection `yaml:"targets"`
1616
Excludes types.Collection `yaml:"excludes"`
1717
}
1818

19-
type NukeConfig struct {
20-
AccountBlacklist []string `yaml:"account-blacklist"`
21-
Regions []string `yaml:"regions"`
22-
Accounts map[string]NukeConfigAccount `yaml:"accounts"`
23-
ResourceTypes ResourceConfig `yaml:"resource-types"`
19+
type Nuke struct {
20+
AccountBlacklist []string `yaml:"account-blacklist"`
21+
Regions []string `yaml:"regions"`
22+
Accounts map[string]Account `yaml:"accounts"`
23+
ResourceTypes ResourceTypes `yaml:"resource-types"`
2424
}
2525

26-
type NukeConfigAccount struct {
26+
type Account struct {
2727
Filters map[string][]string `yaml:"filters"`
28-
ResourceTypes ResourceConfig `yaml:"resource-types"`
28+
ResourceTypes ResourceTypes `yaml:"resource-types"`
2929
}
3030

31-
func LoadConfig(path string) (*NukeConfig, error) {
31+
func Load(path string) (*Nuke, error) {
3232
var err error
3333

3434
raw, err := ioutil.ReadFile(path)
3535
if err != nil {
3636
return nil, err
3737
}
3838

39-
config := new(NukeConfig)
39+
config := new(Nuke)
4040
err = yaml.Unmarshal(raw, config)
4141
if err != nil {
4242
return nil, err
@@ -49,11 +49,11 @@ func LoadConfig(path string) (*NukeConfig, error) {
4949
return config, nil
5050
}
5151

52-
func (c *NukeConfig) HasBlacklist() bool {
52+
func (c *Nuke) HasBlacklist() bool {
5353
return c.AccountBlacklist != nil && len(c.AccountBlacklist) > 0
5454
}
5555

56-
func (c *NukeConfig) InBlacklist(searchID string) bool {
56+
func (c *Nuke) InBlacklist(searchID string) bool {
5757
for _, blacklistID := range c.AccountBlacklist {
5858
if blacklistID == searchID {
5959
return true
@@ -63,7 +63,7 @@ func (c *NukeConfig) InBlacklist(searchID string) bool {
6363
return false
6464
}
6565

66-
func (c *NukeConfig) ValidateAccount(accountID string, aliases []string) error {
66+
func (c *Nuke) ValidateAccount(accountID string, aliases []string) error {
6767
if !c.HasBlacklist() {
6868
return fmt.Errorf("The config file contains an empty blacklist. " +
6969
"For safety reasons you need to specify at least one account ID. " +
@@ -96,7 +96,7 @@ func (c *NukeConfig) ValidateAccount(accountID string, aliases []string) error {
9696
return nil
9797
}
9898

99-
func (c *NukeConfig) resolveDeprecations() error {
99+
func (c *Nuke) resolveDeprecations() error {
100100
deprecations := map[string]string{
101101
"EC2DhcpOptions": "EC2DHCPOptions",
102102
"EC2InternetGatewayAttachement": "EC2InternetGatewayAttachment",

cmd/config_test.go pkg/config/config_test.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cmd
1+
package config
22

33
import (
44
"fmt"
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func TestConfigBlacklist(t *testing.T) {
11-
config := new(NukeConfig)
11+
config := new(Nuke)
1212

1313
if config.HasBlacklist() {
1414
t.Errorf("HasBlacklist() returned true on a nil backlist.")
@@ -44,16 +44,16 @@ func TestConfigBlacklist(t *testing.T) {
4444
}
4545

4646
func TestLoadExampleConfig(t *testing.T) {
47-
config, err := LoadConfig("test-fixtures/example.yaml")
47+
config, err := Load("test-fixtures/example.yaml")
4848
if err != nil {
4949
t.Fatal(err)
5050
}
5151

52-
expect := NukeConfig{
52+
expect := Nuke{
5353
AccountBlacklist: []string{"1234567890"},
5454
Regions: []string{"eu-west-1"},
55-
Accounts: map[string]NukeConfigAccount{
56-
"555133742": NukeConfigAccount{
55+
Accounts: map[string]Account{
56+
"555133742": Account{
5757
Filters: map[string][]string{
5858
"IAMRole": []string{
5959
"uber.admin",
@@ -74,10 +74,10 @@ func TestLoadExampleConfig(t *testing.T) {
7474
}
7575

7676
func TestResolveDeprecations(t *testing.T) {
77-
config := NukeConfig{
77+
config := Nuke{
7878
AccountBlacklist: []string{"1234567890"},
7979
Regions: []string{"eu-west-1"},
80-
Accounts: map[string]NukeConfigAccount{
80+
Accounts: map[string]Account{
8181
"555133742": {
8282
Filters: map[string][]string{
8383
"IamRole": {"uber.admin", "foo.bar"},
@@ -93,7 +93,7 @@ func TestResolveDeprecations(t *testing.T) {
9393
},
9494
}
9595

96-
expect := map[string]NukeConfigAccount{
96+
expect := map[string]Account{
9797
"555133742": {
9898
Filters: map[string][]string{
9999
"IAMRole": {"uber.admin", "foo.bar"},
@@ -118,10 +118,10 @@ func TestResolveDeprecations(t *testing.T) {
118118
t.Errorf(" Expected: %#v", expect)
119119
}
120120

121-
invalidConfig := NukeConfig{
121+
invalidConfig := Nuke{
122122
AccountBlacklist: []string{"1234567890"},
123123
Regions: []string{"eu-west-1"},
124-
Accounts: map[string]NukeConfigAccount{
124+
Accounts: map[string]Account{
125125
"555133742": {
126126
Filters: map[string][]string{
127127
"IamUserAccessKeys": {"X"},
@@ -138,7 +138,7 @@ func TestResolveDeprecations(t *testing.T) {
138138
}
139139

140140
func TestConfigValidation(t *testing.T) {
141-
config, err := LoadConfig("test-fixtures/example.yaml")
141+
config, err := Load("test-fixtures/example.yaml")
142142
if err != nil {
143143
t.Fatal(err)
144144
}
File renamed without changes.

0 commit comments

Comments
 (0)