/
cmd_pull.go
126 lines (109 loc) · 2.96 KB
/
cmd_pull.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
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"context"
"fmt"
"io"
"os"
tfe "github.com/hashicorp/go-tfe"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
)
type PullOption struct {
varFile string
overwrite bool
prevVarfile []byte
includeEnv bool
includeVariableSet bool
}
func NewPullOption(c *cli.Context) *PullOption {
var opt = &PullOption{}
opt.varFile = c.String("var-file")
opt.overwrite = c.Bool("overwrite") && !c.Bool("merge")
opt.prevVarfile = nil
opt.includeEnv = c.Bool("include-env")
opt.includeVariableSet = c.Bool("include-variable-set")
return opt
}
func Pull(c *cli.Context) error {
ctx := context.Background()
log.Debug().Msg("pull command")
tfeClient, err := NewTfeClient(c)
if err != nil {
log.Error().Err(err).Msg("failed to build tfe client")
return err
}
organization, workspaceName = updateTerraformCloudWorkspace(organization, workspaceName, ".")
w, err := tfeClient.Workspaces.Read(ctx, organization, workspaceName)
if err != nil {
log.Error().Err(err).Msgf("failed to access workspace %s/%s", organization, workspaceName)
return err
}
pullOpt := NewPullOption(c)
if !pullOpt.overwrite {
src, _ := os.ReadFile(pullOpt.varFile)
pullOpt.prevVarfile = src
}
log.Debug().Msgf("pullOption: %+v", pullOpt)
f, err := os.Create(pullOpt.varFile)
if err != nil {
log.Error().Err(err).Msgf("cannot open varfile: %s", pullOpt.varFile)
return err
}
defer f.Close()
return pull(ctx, w.ID, tfeClient.Variables, tfeClient.VariableSets, tfeClient.VariableSetVariables, pullOpt, f)
}
func pull(ctx context.Context, workspaceId string, tfeVariables tfe.Variables, tfeVariableSets tfe.VariableSets, tfeVariableSetVariables tfe.VariableSetVariables, pullOpt *PullOption, w io.Writer) error {
vars, err := tfeVariables.List(ctx, workspaceId, nil)
if err != nil {
log.Error().Err(err).Msg("failed to list variables")
return err
}
if pullOpt.includeVariableSet {
variableSetVariables, err := listVariableSetVariables(ctx, workspaceId, tfeVariableSets, tfeVariableSetVariables)
if err != nil {
log.Error().Err(err).Msg("failed to list VariableSetVariables")
return err
}
vars.Items = append(vars.Items, variableSetVariables...)
}
if !pullOpt.includeEnv {
vars.Items = FilterEnv(vars.Items)
}
var base []byte
if !pullOpt.overwrite {
base = pullOpt.prevVarfile
}
f, err := BuildHCLFile(vars.Items, base, pullOpt.varFile)
if err != nil {
return err
}
fmt.Fprintf(w, "%s", f.Bytes())
return nil
}
func generateComment(key string) hclwrite.Tokens {
tokens := hclwrite.Tokens{
{
Type: hclsyntax.TokenSlash,
Bytes: []byte("//"),
},
{
Type: hclsyntax.TokenIdent,
Bytes: []byte(key),
},
{
Type: hclsyntax.TokenIdent,
Bytes: []byte("="),
},
{
Type: hclsyntax.TokenIdent,
Bytes: []byte("\"***\""),
},
{
Type: hclsyntax.TokenNewline,
Bytes: []byte("\n"),
},
}
return tokens
}