-
Notifications
You must be signed in to change notification settings - Fork 21
/
update.go
93 lines (79 loc) · 2.46 KB
/
update.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
package tfupdate
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"strings"
"github.com/hashicorp/hcl2/hcl"
"github.com/hashicorp/hcl2/hclwrite"
"github.com/pkg/errors"
)
// Updater is an interface which updates a version constraint in HCL.
type Updater interface {
// Update updates a version constraint.
// Note that this method will rewrite the AST passed as an argument.
Update(*hclwrite.File) error
}
// Option is a set of parameters to update.
type Option struct {
// A type of updater. Valid value is terraform or provider.
updateType string
// A target to be updated.
// If an updateType is terraform, Set a version.
// If an updateType is provider, Set a name@version.
target string
// If a recursive flag is true, it checks and updates directories recursively.
recursive bool
}
// NewUpdater is a factory method which returns an Updater implementation.
func NewUpdater(o Option) (Updater, error) {
switch o.updateType {
case "terraform":
return NewTerraformUpdater(o.target)
case "provider":
s := strings.Split(o.target, "@")
return &ProviderUpdater{
name: s[0],
version: s[1],
}, nil
case "module":
return nil, errors.Errorf("failed to new updater. module is not currently supported.")
default:
return nil, errors.Errorf("failed to new updater. unknown type: %s", o.updateType)
}
}
// NewOption returns an option.
func NewOption(updateType string, target string, recursive bool) Option {
return Option{
updateType: updateType,
target: target,
recursive: recursive,
}
}
// UpdateHCL reads HCL from io.Reader, updates version constraints
// and writes updated contents to io.Writer.
// Note that a filename is used only for an error message.
// If contents changed successfully, it returns true, or otherwise returns false.
// If an error occurs, Nothing is written to the output stream.
func UpdateHCL(r io.Reader, w io.Writer, filename string, o Option) (bool, error) {
input, err := ioutil.ReadAll(r)
if err != nil {
return false, fmt.Errorf("failed to read input: %s", err)
}
f, diags := hclwrite.ParseConfig(input, filename, hcl.Pos{Line: 1, Column: 1})
if diags.HasErrors() {
return false, fmt.Errorf("failed to parse input: %s", diags)
}
u, err := NewUpdater(o)
if err != nil {
return false, err
}
u.Update(f)
output := f.BuildTokens(nil).Bytes()
if _, err := w.Write(output); err != nil {
return false, fmt.Errorf("failed to write output: %s", err)
}
isUpdated := !bytes.Equal(input, output)
return isUpdated, nil
}