Add PCL as a target language for pulumi convert
@@ -16,7 +16,9 @@ package main
import (
"fmt"
+ "io/ioutil"
"os"
+ "path"
"github.com/spf13/cobra"
@@ -79,6 +81,44 @@ func newConvertCmd() *cobra.Command {
return cmd
}
+// runConvert converts a Pulumi program from YAML into PCL without generating a full pcl.Program
+func runConvertPcl(cwd string, outDir string) result.Result {
+ host, err := newPluginHost()
+ if err != nil {
+ return result.FromError(fmt.Errorf("could not create plugin host: %w", err))
+ }
+ defer contract.IgnoreClose(host)
+ loader := schema.NewPluginLoader(host)
+ _, template, diags, err := yamlgen.LoadTemplate(cwd)
+ if err != nil {
+ return result.FromError(err)
+ }
+
+ if diags.HasErrors() {
+ return result.FromError(diags)
+ }
+
+ programText, diags, err := yamlgen.ConvertTemplateIL(template, loader)
+ if err != nil {
+ return result.FromError(err)
+ }
+
+ if outDir != "." {
+ err := os.MkdirAll(outDir, 0755)
+ if err != nil {
+ return result.FromError(fmt.Errorf("could not create output directory: %w", err))
+ }
+ }
+
+ outputFile := path.Join(outDir, "main.pp")
+ err = ioutil.WriteFile(outputFile, []byte(programText), 0600)
+ if err != nil {
+ return result.FromError(fmt.Errorf("could not write output program: %w", err))
+ }
+
+ return nil
+}
+
func runConvert(cwd string, language string, outDir string, generateOnly bool) result.Result {
var projectGenerator projectGeneratorFunc
switch language {
@@ -94,6 +134,15 @@ func runConvert(cwd string, language string, outDir string, generateOnly bool) r
projectGenerator = javagen.GenerateProject
case "yaml": // nolint: goconst
projectGenerator = yamlgen.GenerateProject
+ case "pulumi", "pcl":
+ if cmdutil.IsTruthy(os.Getenv("PULUMI_DEV")) {
+ // since we don't need Eject to get the full program,
+ // we can just convert the YAML directly to PCL
+ return runConvertPcl(cwd, outDir)
+ }
+
+ fallthrough
+
default:
return result.Errorf("cannot generate programs for %q language", language)
}
