-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource_git_add.go
226 lines (196 loc) · 5.97 KB
/
resource_git_add.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* SPDX-FileCopyrightText: The terraform-provider-git Authors
* SPDX-License-Identifier: 0BSD
*/
package provider
import (
"context"
"github.com/go-git/go-git/v5"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"path/filepath"
"time"
)
type resourceGitAddType struct{}
type resourceGitAdd struct {
p gitProvider
}
type resourceGitAddSchema struct {
Directory types.String `tfsdk:"directory"`
Id types.Int64 `tfsdk:"id"`
Paths types.List `tfsdk:"add_paths"`
}
func (r *resourceGitAddType) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
MarkdownDescription: "Add file contents to the index similar to `git add`.",
Attributes: map[string]tfsdk.Attribute{
"directory": {
Description: "The path to the local Git repository.",
Type: types.StringType,
Required: true,
Validators: []tfsdk.AttributeValidator{
stringvalidator.LengthAtLeast(1),
},
PlanModifiers: []tfsdk.AttributePlanModifier{
resource.RequiresReplace(),
},
},
"id": {
Description: "The timestamp of the last addition in Unix nanoseconds.",
Type: types.Int64Type,
Computed: true,
},
"add_paths": {
Description: "The paths to add to the Git index. Values can be exact paths or glob patterns.",
Type: types.ListType{
ElemType: types.StringType,
},
Computed: true,
Optional: true,
},
},
}, nil
}
func (r *resourceGitAddType) NewResource(_ context.Context, p provider.Provider) (resource.Resource, diag.Diagnostics) {
return &resourceGitAdd{
p: *(p.(*gitProvider)),
}, nil
}
func (r *resourceGitAdd) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
tflog.Debug(ctx, "Create git_add")
var inputs resourceGitAddSchema
diags := req.Config.Get(ctx, &inputs)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
directory := inputs.Directory.Value
repository := openRepository(ctx, directory, &resp.Diagnostics)
if repository == nil {
return
}
worktree, err := getWorktree(repository, &resp.Diagnostics)
if err != nil {
return
}
if worktree == nil {
resp.Diagnostics.AddError(
"Cannot add file to bare repository",
"The repository at ["+directory+"] is bare. Create a worktree first in order to add files to it.",
)
return
}
status := getStatus(ctx, worktree, &resp.Diagnostics)
if status == nil {
return
}
paths := make([]string, len(inputs.Paths.Elems))
resp.Diagnostics.Append(inputs.Paths.ElementsAs(ctx, &paths, false)...)
if resp.Diagnostics.HasError() {
return
}
for _, pattern := range paths {
for file, fileStatus := range status {
if fileStatus.Worktree != git.Unmodified {
match, errMatch := filepath.Match(pattern, file)
if errMatch != nil {
resp.Diagnostics.AddError(
"Cannot match file path",
"Could not match pattern ["+pattern+"] because of: "+errMatch.Error(),
)
}
if match {
_, errAdd := worktree.Add(file)
if errAdd != nil {
resp.Diagnostics.AddError(
"Cannot add file",
"Could not add file ["+file+"] because of: "+errAdd.Error(),
)
}
}
}
}
}
var state resourceGitAddSchema
state.Directory = inputs.Directory
state.Id = types.Int64{Value: time.Now().UnixNano()}
state.Paths = inputs.Paths
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
func (r *resourceGitAdd) Read(ctx context.Context, _ resource.ReadRequest, _ *resource.ReadResponse) {
tflog.Debug(ctx, "Read git_add")
// NO-OP: All data is already in Terraform state
}
func (r *resourceGitAdd) Update(ctx context.Context, _ resource.UpdateRequest, _ *resource.UpdateResponse) {
tflog.Debug(ctx, "Update git_add")
// NO-OP: All attributes require replacement, thus delete/create will be called
}
func (r *resourceGitAdd) Delete(ctx context.Context, _ resource.DeleteRequest, _ *resource.DeleteResponse) {
tflog.Debug(ctx, "Delete git_add")
// NO-OP: Terraform removes the state automatically for us
}
func (r *resourceGitAdd) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
tflog.Debug(ctx, "ModifyPlan git_add")
if req.State.Raw.IsNull() {
// if we're creating the resource, no need to modify it
return
}
if req.Plan.Raw.IsNull() {
// if we're deleting the resource, no need to modify it
return
}
var inputs resourceGitAddSchema
diags := req.Config.Get(ctx, &inputs)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
directory := inputs.Directory.Value
repository := openRepository(ctx, directory, &resp.Diagnostics)
if repository == nil {
return
}
worktree, err := getWorktree(repository, &resp.Diagnostics)
if err != nil || worktree == nil {
return
}
status := getStatus(ctx, worktree, &resp.Diagnostics)
if status == nil {
return
}
paths := make([]string, len(inputs.Paths.Elems))
resp.Diagnostics.Append(inputs.Paths.ElementsAs(ctx, &paths, false)...)
if resp.Diagnostics.HasError() {
return
}
for _, pattern := range paths {
for key, val := range status {
if val.Worktree != git.Unmodified {
match, errMatch := filepath.Match(pattern, key)
if errMatch != nil {
resp.Diagnostics.AddError(
"Cannot match file path",
"Could not match pattern ["+pattern+"] because of: "+errMatch.Error(),
)
return
}
if match {
id := path.Root("id")
resp.Plan.SetAttribute(ctx, id, time.Now().UnixNano())
resp.RequiresReplace = append(resp.RequiresReplace, id)
break
}
}
}
}
}