-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathplugin.go
67 lines (61 loc) · 1.66 KB
/
plugin.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
package ecschedule
import (
"context"
"errors"
"fmt"
"path/filepath"
"strings"
"sync"
"github.com/fujiwara/ssm-lookup/ssm"
"github.com/fujiwara/tfstate-lookup/tfstate"
)
// Plugin the plugin
type Plugin struct {
Name string `yaml:"name"`
Config map[string]interface{} `yaml:"config"`
FuncPrefix string `yaml:"func_prefix,omitempty"`
}
func (p Plugin) setup(ctx context.Context, c *Config) error {
switch strings.ToLower(p.Name) {
case "tfstate":
return setupPluginTFState(ctx, p, c)
case "ssm":
return setupPluginSSM(ctx, p, c)
default:
return fmt.Errorf("plugin %s is not available", p.Name)
}
}
func setupPluginTFState(ctx context.Context, p Plugin, c *Config) error {
var loc string
if p.Config["path"] != nil {
path, ok := p.Config["path"].(string)
if !ok {
return errors.New("tfstate plugin requires path for tfstate file as a string")
}
if !filepath.IsAbs(path) {
path = filepath.Join(c.dir, path)
}
loc = path
} else if p.Config["url"] != nil {
u, ok := p.Config["url"].(string)
if !ok {
return errors.New("tfstate plugin requires url for tfstate URL as a string")
}
loc = u
} else {
return errors.New("tfstate plugin requires path or url for tfstate location")
}
funcs, err := tfstate.FuncMapWithName(ctx, p.FuncPrefix+"tfstate", loc)
if err != nil {
return err
}
c.templateFuncs = append(c.templateFuncs, funcs)
return nil
}
func setupPluginSSM(ctx context.Context, p Plugin, c *Config) error {
cache := &sync.Map{}
s := ssm.New(getApp(ctx).AwsConf, cache)
funcs := s.FuncMapWithName(ctx, p.FuncPrefix+"ssm")
c.templateFuncs = append(c.templateFuncs, funcs)
return nil
}