forked from gruntwork-io/terragrunt
-
Notifications
You must be signed in to change notification settings - Fork 2
/
extension_base.go
113 lines (94 loc) · 3.13 KB
/
extension_base.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package config
import (
"fmt"
"runtime"
"strings"
"github.com/fatih/color"
"github.com/gruntwork-io/terragrunt/options"
"github.com/gruntwork-io/terragrunt/util"
logging "github.com/op/go-logging"
)
// TerragruntExtensioner defines the interface that must be implemented by Terragrunt Extension objects
type TerragruntExtensioner interface {
config() *TerragruntConfigFile
description() string
enabled() bool
extraInfo() string
help() string
id() string
init(*TerragruntConfigFile)
logger() *logging.Logger
name() string
itemType() string
normalize()
options() *options.TerragruntOptions
}
// TerragruntExtensionBase is the base object to define object used to extend the behavior of terragrunt
type TerragruntExtensionBase struct {
_config *TerragruntConfigFile
Name string `hcl:",key"`
DisplayName string `hcl:"display_name"`
Description string `hcl:"description"`
OS []string `hcl:"os"`
Disabled bool `hcl:"disabled"`
}
func (base TerragruntExtensionBase) String() string { return base.id() }
func (base TerragruntExtensionBase) id() string { return base.Name }
func (base TerragruntExtensionBase) description() string { return base.Description }
func (base TerragruntExtensionBase) extraInfo() string { return "" }
func (base TerragruntExtensionBase) normalize() {}
func (base *TerragruntExtensionBase) init(config *TerragruntConfigFile) {
base._config = config
}
func (base TerragruntExtensionBase) run(args ...interface{}) ([]interface{}, error) {
return nil, nil
}
func (base TerragruntExtensionBase) name() string {
if base.DisplayName != "" {
return base.DisplayName
}
return base.Name
}
// Config returns the current config associated with the object
func (base TerragruntExtensionBase) config() *TerragruntConfigFile {
if base._config != nil {
return base._config
}
panic(fmt.Sprintf("No config associated with object %v", base.id()))
}
// Returns the current options set associated with the object
func (base TerragruntExtensionBase) options() *options.TerragruntOptions {
if options := base.config().options; options != nil {
return options
}
panic(fmt.Sprintf("No options set associated with object %v", base.id()))
}
// Returns the current logger to use on the object
func (base TerragruntExtensionBase) logger() *logging.Logger {
if logger := base.options().Logger; logger != nil {
return logger
}
panic(fmt.Sprintf("No logger associated with object %v", base.id()))
}
// Determines if a command is enabled
func (base TerragruntExtensionBase) enabled() bool {
return !base.Disabled && (len(base.OS) == 0 || util.ListContainsElement(base.OS, runtime.GOOS))
}
// TitleID add formating to the id of the elements
var TitleID = color.New(color.FgHiYellow).SprintFunc()
type mergeMode int
const (
mergeModePrepend mergeMode = iota
mergeModeAppend
)
func (list GenericItemList) sort() GenericItemList { return list }
type errorArray []error
func (errors errorArray) Error() string {
errorsStr := make([]string, 0, len(errors))
for i := range errors {
if errors[i] != nil {
errorsStr = append(errorsStr, errors[i].Error())
}
}
return strings.Join(errorsStr, "\n")
}