Skip to content

Commit

Permalink
Merge #11011
Browse files Browse the repository at this point in the history
11011: Add PCL as a target language for pulumi convert r=Zaid-Ajaj a=Zaid-Ajaj

<!--- 
Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation.
-->

# Description

<!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. -->

Every time I need to debug `pulumi convert` I have to hack YAML program-gen and make it output the intermediate PCL program text to stdout and work from there. Here, I've added PCL as a proper target language for `pulumi convert` (without needing to parse the program text and generate `pcl.Program` from it) 

I tried a simpler approach:
```go
projectGenerator = func(directory string, project workspace.Project, p *pcl.Program) error {
	programText := fmt.Sprintf("%v", p)
	outputFile := path.Join(directory, "main.pp")
	err := ioutil.WriteFile(outputFile, []byte(programText), 0600)
	if err != nil {
		return fmt.Errorf("could not write output program: %w", err)
	}
	return nil
}
```
but the default formatter for `pcl.Program` writes out invalid PCL (including non-formatted type metadata) so I went with this approach instead 

## Checklist

<!--- Please provide details if the checkbox below is to be left unchecked. -->
- [ ] I have added tests that prove my fix is effective or that my feature works
<!--- 
User-facing changes require a CHANGELOG entry.
-->
- [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the Pulumi Service,
then the service should honor older versions of the CLI where this change would not exist.
You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Service API version
  <!-- `@Pulumi` employees: If yes, you must submit corresponding changes in the service repo. -->


Co-authored-by: Zaid Ajaj <zaid.naom@gmail.com>
  • Loading branch information
bors[bot] and Zaid-Ajaj committed Oct 14, 2022
2 parents 2c4318b + 99dd24d commit 39e2ed5
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions pkg/cmd/pulumi/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"path"

"github.com/spf13/cobra"

Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down

0 comments on commit 39e2ed5

Please sign in to comment.