forked from Songmu/ecschedule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
54 lines (49 loc) · 1.24 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
package ecschedule
import (
"context"
"errors"
"fmt"
"path/filepath"
"strings"
"github.com/fujiwara/tfstate-lookup/tfstate"
)
// Plugin the plugin
type Plugin struct {
Name string `yaml:"name"`
Config map[string]interface{} `yaml:"config"`
}
func (p Plugin) setup(ctx context.Context, c *Config) error {
switch strings.ToLower(p.Name) {
case "tfstate":
return setupPluginTFState(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, "tfstate", loc)
if err != nil {
return err
}
c.templateFuncs = append(c.templateFuncs, funcs)
return nil
}