Skip to content

Commit 7a6b42c

Browse files
authored
Ensure that we embed the version in the schema before packaging to the binary (#86)
1 parent 40b12da commit 7a6b42c

File tree

5 files changed

+37
-206
lines changed

5 files changed

+37
-206
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ ci-scripts
1313
sdk/dotnet/version.txt
1414
.DS_Store
1515
/provider/cmd/pulumi-resource-command/schema.go
16+
/provider/cmd/pulumi-resource-command/schema-embed.json
1617
dist/
1718

1819
rsa*

provider/cmd/pulumi-resource-command/generate.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,50 @@
1313
// limitations under the License.
1414

1515
//go:build ignore
16-
// +build ignore
1716

1817
package main
1918

2019
import (
21-
"fmt"
20+
"encoding/json"
21+
"errors"
22+
"io/fs"
2223
"io/ioutil"
2324
"log"
25+
"os"
26+
27+
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
2428
)
2529

2630
func main() {
31+
version, found := os.LookupEnv("VERSION")
32+
if !found {
33+
log.Fatal("version not found")
34+
}
35+
2736
schemaContents, err := ioutil.ReadFile("./schema.json")
2837
if err != nil {
2938
log.Fatal(err)
3039
}
3140

32-
err = ioutil.WriteFile("./schema.go", []byte(fmt.Sprintf(`package main
33-
var pulumiSchema = %#v
34-
`, schemaContents)), 0600)
41+
var packageSpec schema.PackageSpec
42+
err = json.Unmarshal(schemaContents, &packageSpec)
43+
if err != nil {
44+
log.Fatalf("cannot deserialize schema: %v", err)
45+
}
46+
47+
packageSpec.Version = version
48+
versionedContents, err := json.Marshal(packageSpec)
49+
if err != nil {
50+
log.Fatalf("cannot reserialize schema: %v", err)
51+
}
52+
53+
// Clean up schema.go as it may be present & gitignored and tolerate an error if the file isn't present.
54+
err = os.Remove("./schema.go")
55+
if err != nil && !errors.Is(err, fs.ErrNotExist) {
56+
log.Fatal(err)
57+
}
58+
59+
err = ioutil.WriteFile("./schema-embed.json", versionedContents, 0600)
3560
if err != nil {
3661
log.Fatal(err)
3762
}

provider/cmd/pulumi-resource-command/main.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
package main
1818

1919
import (
20+
_ "embed"
21+
2022
"github.com/pulumi/pulumi-command/provider/pkg/provider"
2123
"github.com/pulumi/pulumi-command/provider/pkg/version"
2224
)
2325

24-
var providerName = "command"
26+
//go:embed schema-embed.json
27+
var pulumiSchema []byte
2528

2629
func main() {
27-
provider.Serve(providerName, version.Version, pulumiSchema)
30+
provider.Serve("command", version.Version, pulumiSchema)
2831
}

provider/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/pulumi/pulumi-command/provider
22

3-
go 1.17
3+
go 1.18
44

55
require (
66
github.com/golang/protobuf v1.5.2

0 commit comments

Comments
 (0)