Skip to content

Commit 67c33fd

Browse files
committed
feat: Added corefunc_str_kebab.
1 parent 8d42454 commit 67c33fd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+676
-159
lines changed

.pre-commit-config.yaml

+11-7
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,6 @@ repos:
7070
- id: golangci-lint
7171
# - id: go-consistent
7272
- id: gomodupdate
73-
- id: markdownlint
74-
args:
75-
- --ignore=node_modules
76-
- --ignore=.github
77-
- --ignore=.templates
78-
- --fix
79-
- '**/*.md'
8073
- id: script-must-have-extension
8174
- id: shellcheck
8275
- id: shfmt
@@ -93,6 +86,17 @@ repos:
9386
- id: yamlfmt
9487
- id: yapf
9588

89+
- repo: https://github.com/igorshubovych/markdownlint-cli
90+
rev: v0.36.0
91+
hooks:
92+
- id: markdownlint
93+
args:
94+
- --ignore=node_modules
95+
- --ignore=.github
96+
- --ignore=.templates
97+
- --fix
98+
- '**/*.md'
99+
96100
- repo: local
97101
hooks:
98102
- id: trufflehog

Makefile

+32-1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ tidy:
108108
@ $(ECHO) " "
109109
@ $(ECHO) "\033[1;33m=====> Tidy and download the Go dependencies...\033[0m"
110110
$(GO) mod tidy -go=1.21 -v
111+
112+
.PHONY: godeps
113+
## godeps: [build] Updates go.mod and downloads dependencies.
114+
godeps:
115+
@ $(ECHO) " "
116+
@ $(ECHO) "\033[1;33m=====> Upgrade the minor versions of Go dependencies...\033[0m"
111117
$(GO) get -d -u -t -v ./...
112118

113119
.PHONY: build
@@ -117,7 +123,6 @@ build: tidy
117123
@ $(ECHO) " "
118124
@ $(ECHO) "\033[1;33m=====> Building and installing the provider...\033[0m"
119125
$(GO) install -a -ldflags="-s -w" .
120-
@ $(ECHO) " "
121126
@ ls -lahF $(GOBIN)/$(BINARY_NAME)
122127

123128
#-------------------------------------------------------------------------------
@@ -259,6 +264,32 @@ lint: vuln license pre-commit
259264
## test: [test]* Runs ALL tests.
260265
test: unit examples acc bats
261266

267+
.PHONY: list-tests
268+
## list-tests: [test] Lists all of the tests that are available to run.
269+
list-tests:
270+
@ $(ECHO) " "
271+
@ $(ECHO) "\033[1;33m=====> Unit tests...\033[0m"
272+
@ $(ECHO) "make unit"
273+
@ cat ./corefunc/*_test.go | ggrep "func Test" | gsed 's/func\s//g' | gsed -r 's/\(.*//g' | gsed -r 's/Test/make unit NAME=/g'
274+
275+
@ $(ECHO) " "
276+
@ $(ECHO) "\033[1;33m=====> Terraform acceptance tests...\033[0m"
277+
@ $(ECHO) "make acc"
278+
@ cat ./corefuncprovider/*_test.go | ggrep "func TestAcc" | gsed 's/func\s//g' | gsed -r 's/\(.*//g' | gsed -r 's/TestAcc/make acc NAME=/g'
279+
280+
@ $(ECHO) " "
281+
@ $(ECHO) "\033[1;33m=====> Example tests...\033[0m"
282+
@ $(ECHO) "make examples"
283+
@ cat ./corefunc/*_test.go | ggrep "func Example" | gsed 's/func\s//g' | gsed -r 's/\(.*//g' | gsed -r 's/Example/make examples NAME=/g'
284+
285+
@ $(ECHO) " "
286+
@ $(ECHO) "\033[1;33m=====> Fuzzing tests...\033[0m"
287+
@ cat ./corefunc/*_test.go | ggrep "func Fuzz" | gsed 's/func\s//g' | gsed -r 's/\(.*//g' | gsed -r 's/Fuzz/make fuzz NAME=/g'
288+
289+
@ $(ECHO) " "
290+
@ $(ECHO) "\033[1;33m=====> BATS tests...\033[0m"
291+
@ $(ECHO) "make bats"
292+
262293
.PHONY: bats
263294
## bats: [test] Tests the output of the provider using tfschema and BATS.
264295
bats: build

bats/tfschema.bats.sh

+14-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
[ "$status" -eq 0 ]
88
[[ ${lines[0]} == "corefunc_env_ensure" ]]
99
[[ ${lines[1]} == "corefunc_str_camel" ]]
10-
[[ ${lines[2]} == "corefunc_str_pascal" ]]
11-
[[ ${lines[3]} == "corefunc_str_snake" ]]
12-
[[ ${lines[4]} == "corefunc_str_truncate_label" ]]
10+
[[ ${lines[2]} == "corefunc_str_kebab" ]]
11+
[[ ${lines[3]} == "corefunc_str_pascal" ]]
12+
[[ ${lines[4]} == "corefunc_str_snake" ]]
13+
[[ ${lines[5]} == "corefunc_str_truncate_label" ]]
14+
1315
}
1416

1517
@test "corefunc_env_ensure: attrs" {
@@ -31,6 +33,15 @@
3133
[[ ${lines[2]} == '{"name":"value","type":"string","required":false,"optional":false,"computed":true,"sensitive":false}' ]]
3234
}
3335

36+
@test "corefunc_str_kebab: attrs" {
37+
run bash -c "tfschema data show -format=json corefunc_str_kebab | jq -Mrc '.attributes[]'"
38+
39+
[ "$status" -eq 0 ]
40+
[[ ${lines[0]} == '{"name":"id","type":"number","required":false,"optional":false,"computed":true,"sensitive":false}' ]]
41+
[[ ${lines[1]} == '{"name":"string","type":"string","required":true,"optional":false,"computed":false,"sensitive":false}' ]]
42+
[[ ${lines[2]} == '{"name":"value","type":"string","required":false,"optional":false,"computed":true,"sensitive":false}' ]]
43+
}
44+
3445
@test "corefunc_str_pascal: attrs" {
3546
run bash -c "tfschema data show -format=json corefunc_str_pascal | jq -Mrc '.attributes[]'"
3647

corefuncprovider/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ func (p *coreFuncProvider) DataSources(ctx context.Context) []func() datasource.
103103
return []func() datasource.DataSource{
104104
EnvEnsureDataSource,
105105
StrCamelDataSource,
106+
StrKebabDataSource,
106107
StrPascalDataSource,
107108
StrSnakeDataSource,
108109
TruncateLabelDataSource,
+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
// Copyright 2023, Ryan Parman
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package corefuncprovider // lint:no_dupe
16+
17+
import (
18+
"context"
19+
"fmt"
20+
"strings"
21+
22+
"github.com/chanced/caps"
23+
"github.com/hashicorp/terraform-plugin-framework/datasource"
24+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
25+
"github.com/hashicorp/terraform-plugin-framework/resource"
26+
"github.com/hashicorp/terraform-plugin-framework/types"
27+
"github.com/hashicorp/terraform-plugin-log/tflog"
28+
"github.com/lithammer/dedent"
29+
)
30+
31+
// Ensure the implementation satisfies the expected interfaces.
32+
var (
33+
_ datasource.DataSource = &strKebabDataSource{}
34+
_ datasource.DataSourceWithConfigure = &strKebabDataSource{}
35+
)
36+
37+
// strKebabDataSource is the data source implementation.
38+
type (
39+
strKebabDataSource struct{}
40+
41+
// strKebabDataSourceModel maps the data source schema data.
42+
strKebabDataSourceModel struct {
43+
ID types.Int64 `tfsdk:"id"`
44+
String types.String `tfsdk:"string"`
45+
Value types.String `tfsdk:"value"`
46+
}
47+
)
48+
49+
// StrKebabDataSource is a method that exposes its paired Go function as a
50+
// Terraform Data Source.
51+
func StrKebabDataSource() datasource.DataSource { // lint:allow_return_interface
52+
return &strKebabDataSource{}
53+
}
54+
55+
// Metadata returns the data source type name.
56+
func (d *strKebabDataSource) Metadata(
57+
ctx context.Context,
58+
req datasource.MetadataRequest,
59+
resp *datasource.MetadataResponse,
60+
) {
61+
tflog.Info(ctx, "Starting StrKebab DataSource Metadata method.")
62+
63+
resp.TypeName = req.ProviderTypeName + "_str_kebab"
64+
65+
tflog.Debug(ctx, fmt.Sprintf("req.ProviderTypeName = %s", req.ProviderTypeName))
66+
tflog.Debug(ctx, fmt.Sprintf("resp.TypeName = %s", resp.TypeName))
67+
68+
tflog.Info(ctx, "Ending StrKebab DataSource Metadata method.")
69+
}
70+
71+
// Schema defines the schema for the data source.
72+
func (d *strKebabDataSource) Schema(
73+
ctx context.Context,
74+
_ datasource.SchemaRequest,
75+
resp *datasource.SchemaResponse,
76+
) {
77+
tflog.Info(ctx, "Starting StrKebab DataSource Schema method.")
78+
79+
resp.Schema = schema.Schema{
80+
MarkdownDescription: strings.TrimSpace(dedent.Dedent(`
81+
Converts a string to ` + "`" + `kebab-case` + "`" + `, removing any non-alphanumeric characters.
82+
83+
Maps to the [` + "`" + `caps.ToKebab()` + "`" + `](https://pkg.go.dev/github.com/chanced/caps#Caps.ToKebab)
84+
Go method, which can be used in ` + Terratest + `.
85+
`)),
86+
Attributes: map[string]schema.Attribute{
87+
"id": schema.Int64Attribute{
88+
Description: "Not used. Required by the " + TPF + ".",
89+
Computed: true,
90+
},
91+
"string": schema.StringAttribute{
92+
Description: "The string to convert to `kebab-case`.",
93+
Required: true,
94+
},
95+
"value": schema.StringAttribute{
96+
Description: "The value of the string.",
97+
Computed: true,
98+
},
99+
},
100+
}
101+
102+
tflog.Info(ctx, "Ending StrKebab DataSource Schema method.")
103+
}
104+
105+
// Configure adds the provider configured client to the data source.
106+
func (d *strKebabDataSource) Configure(
107+
ctx context.Context,
108+
req datasource.ConfigureRequest,
109+
_ *datasource.ConfigureResponse,
110+
) {
111+
tflog.Info(ctx, "Starting StrKebab DataSource Configure method.")
112+
113+
if req.ProviderData == nil {
114+
return
115+
}
116+
117+
tflog.Info(ctx, "Ending StrKebab DataSource Configure method.")
118+
}
119+
120+
func (d strKebabDataSource) Create(
121+
ctx context.Context,
122+
req resource.CreateRequest, // lint:allow_large_memory
123+
resp *resource.CreateResponse,
124+
) {
125+
tflog.Info(ctx, "Starting StrKebab DataSource Create method.")
126+
127+
var plan strKebabDataSourceModel
128+
129+
diags := req.Plan.Get(ctx, &plan)
130+
resp.Diagnostics.Append(diags...)
131+
132+
if resp.Diagnostics.HasError() {
133+
return
134+
}
135+
136+
tflog.Info(ctx, "Ending StrKebab DataSource Create method.")
137+
}
138+
139+
// Read refreshes the Terraform state with the latest data.
140+
func (d *strKebabDataSource) Read(
141+
ctx context.Context,
142+
_ datasource.ReadRequest, // lint:allow_large_memory
143+
resp *datasource.ReadResponse,
144+
) {
145+
tflog.Info(ctx, "Starting StrKebab DataSource Read method.")
146+
147+
var state strKebabDataSourceModel
148+
diags := resp.State.Get(ctx, &state)
149+
resp.Diagnostics.Append(diags...)
150+
151+
state.ID = types.Int64Value(1)
152+
153+
state.Value = types.StringValue(
154+
caps.ToKebab(
155+
state.String.ValueString(),
156+
),
157+
)
158+
159+
diags = resp.State.Set(ctx, &state)
160+
resp.Diagnostics.Append(diags...)
161+
162+
if resp.Diagnostics.HasError() {
163+
return
164+
}
165+
166+
tflog.Info(ctx, "Ending StrKebab DataSource Read method.")
167+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
data "corefunc_str_kebab" "kebab" {
2+
string = "{{ .Input }}"
3+
}
4+
#=> {{ .Expected }}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2023, Ryan Parman
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package corefuncprovider
16+
17+
import (
18+
"bytes"
19+
"fmt"
20+
"log"
21+
"os"
22+
"strings"
23+
"testing"
24+
"text/template"
25+
26+
"github.com/northwood-labs/terraform-provider-corefunc/testfixtures"
27+
28+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
29+
)
30+
31+
func TestAccStrKebabDataSource(t *testing.T) {
32+
funcName := traceFuncName()
33+
34+
for name, tc := range testfixtures.StrKebabTestTable {
35+
fmt.Printf(
36+
"=== RUN %s/%s\n",
37+
strings.TrimSpace(funcName),
38+
strings.TrimSpace(name),
39+
)
40+
41+
buf := new(bytes.Buffer)
42+
tmpl := template.Must(
43+
template.ParseFiles("str_kebab_data_source_fixture.tftpl"),
44+
)
45+
46+
err := tmpl.Execute(buf, tc)
47+
if err != nil {
48+
log.Fatalln(err)
49+
}
50+
51+
if os.Getenv("PROVIDER_DEBUG") != "" {
52+
fmt.Fprintln(os.Stderr, buf.String())
53+
}
54+
55+
resource.Test(t, resource.TestCase{
56+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
57+
Steps: []resource.TestStep{
58+
{
59+
Config: providerConfig + buf.String(),
60+
Check: resource.ComposeAggregateTestCheckFunc(
61+
resource.TestCheckResourceAttr("data.corefunc_str_kebab.kebab", "value", tc.Expected),
62+
),
63+
},
64+
},
65+
})
66+
}
67+
}

0 commit comments

Comments
 (0)