Skip to content

Commit 6d1472f

Browse files
authored
Output relative paths for migrate code template references (#330)
* Output relative paths for code template references * Add Changelog entry
1 parent e38d09b commit 6d1472f

File tree

5 files changed

+49
-39
lines changed

5 files changed

+49
-39
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: BUG FIXES
2+
body: 'migrate: use relative paths (from provider directory) instead of absolute paths
3+
for migrated code templates'
4+
time: 2024-01-24T13:15:19.830885-05:00
5+
custom:
6+
Issue: "330"

cmd/tfplugindocs/testdata/scripts/schema-json/migrate/time_provider_success_docs_website.txtar

+16-16
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ These resources provide an optional map argument called `triggers` that can be p
690690

691691
For example:
692692

693-
{{tffile "$WORK/examples/example_1.tf"}}
693+
{{tffile "examples/example_1.tf"}}
694694

695695
`triggers` are *not* treated as sensitive attributes; a value used for `triggers` will be displayed in Terraform UI output as plaintext.
696696

@@ -713,7 +713,7 @@ Given an RFC3339 timestamp string, will parse and return an object representatio
713713

714714
## Example Usage
715715

716-
{{tffile "$WORK/examples/functions/rfc3339_parse/example_1.tf"}}
716+
{{tffile "examples/functions/rfc3339_parse/example_1.tf"}}
717717

718718
## Signature
719719

@@ -745,11 +745,11 @@ Manages an offset time resource, which keeps an UTC timestamp stored in the Terr
745745

746746
### Basic Usage
747747

748-
{{tffile "$WORK/examples/resources/offset/example_1.tf"}}
748+
{{tffile "examples/resources/offset/example_1.tf"}}
749749

750750
### Triggers Usage
751751

752-
{{tffile "$WORK/examples/resources/offset/example_2.tf"}}
752+
{{tffile "examples/resources/offset/example_2.tf"}}
753753

754754
## Argument Reference
755755

@@ -784,7 +784,7 @@ In addition to all arguments above, the following attributes are exported:
784784

785785
This resource can be imported using the base UTC RFC3339 timestamp and offset years, months, days, hours, minutes, and seconds, separated by commas (`,`), e.g.
786786

787-
{{codefile "shell" "$WORK/examples/resources/offset/import_1.sh"}}
787+
{{codefile "shell" "examples/resources/offset/import_1.sh"}}
788788

789789
The `triggers` argument cannot be imported.
790790
-- exp-templates/resources/rotating.md.tmpl --
@@ -808,7 +808,7 @@ Manages a rotating time resource, which keeps a rotating UTC timestamp stored in
808808

809809
This example configuration will rotate (destroy/create) the resource every 30 days.
810810

811-
{{tffile "$WORK/examples/resources/rotating/example_1.tf"}}
811+
{{tffile "examples/resources/rotating/example_1.tf"}}
812812

813813
## Argument Reference
814814

@@ -842,11 +842,11 @@ In addition to all arguments above, the following attributes are exported:
842842

843843
This resource can be imported using the base UTC RFC3339 value and rotation years, months, days, hours, and minutes, separated by commas (`,`), e.g. for 30 days
844844

845-
{{codefile "shell" "$WORK/examples/resources/rotating/import_1.sh"}}
845+
{{codefile "shell" "examples/resources/rotating/import_1.sh"}}
846846

847847
Otherwise, to import with the rotation RFC3339 value, the base UTC RFC3339 value and rotation UTC RFC3339 value, separated by commas (`,`), e.g.
848848

849-
{{codefile "shell" "$WORK/examples/resources/rotating/import_2.sh"}}
849+
{{codefile "shell" "examples/resources/rotating/import_2.sh"}}
850850

851851
The `triggers` argument cannot be imported.
852852
-- exp-templates/resources/sleep.md.tmpl --
@@ -870,15 +870,15 @@ Manages a resource that delays creation and/or destruction, typically for furthe
870870

871871
### Delay Create Usage
872872

873-
{{tffile "$WORK/examples/resources/sleep/example_1.tf"}}
873+
{{tffile "examples/resources/sleep/example_1.tf"}}
874874

875875
### Delay Destroy Usage
876876

877-
{{tffile "$WORK/examples/resources/sleep/example_2.tf"}}
877+
{{tffile "examples/resources/sleep/example_2.tf"}}
878878

879879
### Triggers Usage
880880

881-
{{tffile "$WORK/examples/resources/sleep/example_3.tf"}}
881+
{{tffile "examples/resources/sleep/example_3.tf"}}
882882

883883
## Argument Reference
884884

@@ -900,11 +900,11 @@ This resource can be imported with the `create_duration` and `destroy_duration`,
900900

901901
e.g. For 30 seconds create duration with no destroy duration:
902902

903-
{{codefile "shell" "$WORK/examples/resources/sleep/import_1.sh"}}
903+
{{codefile "shell" "examples/resources/sleep/import_1.sh"}}
904904

905905
e.g. For 30 seconds destroy duration with no create duration:
906906

907-
{{codefile "shell" "$WORK/examples/resources/sleep/import_2.sh"}}
907+
{{codefile "shell" "examples/resources/sleep/import_2.sh"}}
908908

909909
The `triggers` argument cannot be imported.
910910
-- exp-templates/resources/static.md.tmpl --
@@ -928,11 +928,11 @@ Manages a static time resource, which keeps a locally sourced UTC timestamp stor
928928

929929
### Basic Usage
930930

931-
{{tffile "$WORK/examples/resources/static/example_1.tf"}}
931+
{{tffile "examples/resources/static/example_1.tf"}}
932932

933933
### Triggers Usage
934934

935-
{{tffile "$WORK/examples/resources/static/example_2.tf"}}
935+
{{tffile "examples/resources/static/example_2.tf"}}
936936

937937
## Argument Reference
938938

@@ -959,6 +959,6 @@ In addition to all arguments above, the following attributes are exported:
959959

960960
This resource can be imported using the UTC RFC3339 value, e.g.
961961

962-
{{codefile "shell" "$WORK/examples/resources/static/import_1.sh"}}
962+
{{codefile "shell" "examples/resources/static/import_1.sh"}}
963963

964964
The `triggers` argument cannot be imported.

cmd/tfplugindocs/testdata/scripts/schema-json/migrate/time_provider_success_legacy_website.txtar

+16-16
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ These resources provide an optional map argument called `triggers` that can be p
694694

695695
For example:
696696

697-
{{tffile "$WORK/examples/example_1.tf"}}
697+
{{tffile "examples/example_1.tf"}}
698698

699699
`triggers` are *not* treated as sensitive attributes; a value used for `triggers` will be displayed in Terraform UI output as plaintext.
700700

@@ -717,7 +717,7 @@ Given an RFC3339 timestamp string, will parse and return an object representatio
717717

718718
## Example Usage
719719

720-
{{tffile "$WORK/examples/functions/rfc3339_parse/example_1.tf"}}
720+
{{tffile "examples/functions/rfc3339_parse/example_1.tf"}}
721721

722722
## Signature
723723

@@ -749,11 +749,11 @@ Manages an offset time resource, which keeps an UTC timestamp stored in the Terr
749749

750750
### Basic Usage
751751

752-
{{tffile "$WORK/examples/resources/offset/example_1.tf"}}
752+
{{tffile "examples/resources/offset/example_1.tf"}}
753753

754754
### Triggers Usage
755755

756-
{{tffile "$WORK/examples/resources/offset/example_2.tf"}}
756+
{{tffile "examples/resources/offset/example_2.tf"}}
757757

758758
## Argument Reference
759759

@@ -788,7 +788,7 @@ In addition to all arguments above, the following attributes are exported:
788788

789789
This resource can be imported using the base UTC RFC3339 timestamp and offset years, months, days, hours, minutes, and seconds, separated by commas (`,`), e.g.
790790

791-
{{codefile "shell" "$WORK/examples/resources/offset/import_1.sh"}}
791+
{{codefile "shell" "examples/resources/offset/import_1.sh"}}
792792

793793
The `triggers` argument cannot be imported.
794794
-- exp-templates/resources/rotating.md.tmpl --
@@ -812,7 +812,7 @@ Manages a rotating time resource, which keeps a rotating UTC timestamp stored in
812812

813813
This example configuration will rotate (destroy/create) the resource every 30 days.
814814

815-
{{tffile "$WORK/examples/resources/rotating/example_1.tf"}}
815+
{{tffile "examples/resources/rotating/example_1.tf"}}
816816

817817
## Argument Reference
818818

@@ -846,11 +846,11 @@ In addition to all arguments above, the following attributes are exported:
846846

847847
This resource can be imported using the base UTC RFC3339 value and rotation years, months, days, hours, and minutes, separated by commas (`,`), e.g. for 30 days
848848

849-
{{codefile "shell" "$WORK/examples/resources/rotating/import_1.sh"}}
849+
{{codefile "shell" "examples/resources/rotating/import_1.sh"}}
850850

851851
Otherwise, to import with the rotation RFC3339 value, the base UTC RFC3339 value and rotation UTC RFC3339 value, separated by commas (`,`), e.g.
852852

853-
{{codefile "shell" "$WORK/examples/resources/rotating/import_2.sh"}}
853+
{{codefile "shell" "examples/resources/rotating/import_2.sh"}}
854854

855855
The `triggers` argument cannot be imported.
856856
-- exp-templates/resources/sleep.md.tmpl --
@@ -874,15 +874,15 @@ Manages a resource that delays creation and/or destruction, typically for furthe
874874

875875
### Delay Create Usage
876876

877-
{{tffile "$WORK/examples/resources/sleep/example_1.tf"}}
877+
{{tffile "examples/resources/sleep/example_1.tf"}}
878878

879879
### Delay Destroy Usage
880880

881-
{{tffile "$WORK/examples/resources/sleep/example_2.tf"}}
881+
{{tffile "examples/resources/sleep/example_2.tf"}}
882882

883883
### Triggers Usage
884884

885-
{{tffile "$WORK/examples/resources/sleep/example_3.tf"}}
885+
{{tffile "examples/resources/sleep/example_3.tf"}}
886886

887887
## Argument Reference
888888

@@ -904,11 +904,11 @@ This resource can be imported with the `create_duration` and `destroy_duration`,
904904

905905
e.g. For 30 seconds create duration with no destroy duration:
906906

907-
{{codefile "shell" "$WORK/examples/resources/sleep/import_1.sh"}}
907+
{{codefile "shell" "examples/resources/sleep/import_1.sh"}}
908908

909909
e.g. For 30 seconds destroy duration with no create duration:
910910

911-
{{codefile "shell" "$WORK/examples/resources/sleep/import_2.sh"}}
911+
{{codefile "shell" "examples/resources/sleep/import_2.sh"}}
912912

913913
The `triggers` argument cannot be imported.
914914
-- exp-templates/resources/static.md.tmpl --
@@ -932,11 +932,11 @@ Manages a static time resource, which keeps a locally sourced UTC timestamp stor
932932

933933
### Basic Usage
934934

935-
{{tffile "$WORK/examples/resources/static/example_1.tf"}}
935+
{{tffile "examples/resources/static/example_1.tf"}}
936936

937937
### Triggers Usage
938938

939-
{{tffile "$WORK/examples/resources/static/example_2.tf"}}
939+
{{tffile "examples/resources/static/example_2.tf"}}
940940

941941
## Argument Reference
942942

@@ -963,6 +963,6 @@ In addition to all arguments above, the following attributes are exported:
963963

964964
This resource can be imported using the UTC RFC3339 value, e.g.
965965

966-
{{codefile "shell" "$WORK/examples/resources/static/import_1.sh"}}
966+
{{codefile "shell" "examples/resources/static/import_1.sh"}}
967967

968968
The `triggers` argument cannot be imported.

internal/provider/migrate.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -294,16 +294,16 @@ func (m *migrator) ExtractCodeExamples(content []byte, newRelDir string, templat
294294
exampleCount++
295295
ext = ".tf"
296296
exampleName = "example_" + strconv.Itoa(exampleCount) + ext
297-
examplePath = filepath.Join(m.ProviderExamplesDir(), newRelDir, exampleName)
297+
examplePath = filepath.Join(m.examplesDir, newRelDir, exampleName)
298298
template = fmt.Sprintf("{{tffile \"%s\"}}", examplePath)
299-
m.infof("creating example file %q", examplePath)
299+
m.infof("creating example file %q", filepath.Join(m.providerDir, examplePath))
300300
case "console":
301301
importCount++
302302
ext = ".sh"
303303
exampleName = "import_" + strconv.Itoa(importCount) + ext
304-
examplePath = filepath.Join(m.ProviderExamplesDir(), newRelDir, exampleName)
304+
examplePath = filepath.Join(m.examplesDir, newRelDir, exampleName)
305305
template = fmt.Sprintf("{{codefile \"shell\" \"%s\"}}", examplePath)
306-
m.infof("creating import file %q", examplePath)
306+
m.infof("creating import file %q", filepath.Join(m.providerDir, examplePath))
307307
default:
308308
// Render node as is
309309
m.infof("skipping code block with unknown language %q", lang)

internal/provider/util.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,13 @@ func resourceSchema(schemas map[string]*tfjson.Schema, providerShortName, templa
8686

8787
func writeFile(path string, data string) error {
8888
dir, _ := filepath.Split(path)
89-
err := os.MkdirAll(dir, 0755)
90-
if err != nil {
91-
return fmt.Errorf("unable to make dir %q: %w", dir, err)
89+
90+
var err error
91+
if dir != "" {
92+
err = os.MkdirAll(dir, 0755)
93+
if err != nil {
94+
return fmt.Errorf("unable to make dir %q: %w", dir, err)
95+
}
9296
}
9397

9498
err = os.WriteFile(path, []byte(data), 0644)

0 commit comments

Comments
 (0)