Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement YAML roundtripping, comment preserving edits #11456

Merged
merged 7 commits into from Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,4 @@
changes:
- type: feat
scope: cli/config,new,package
description: Preserve comments on editing of project and config files.
67 changes: 12 additions & 55 deletions pkg/cmd/pulumi/new.go
Expand Up @@ -27,8 +27,6 @@ import (
"strings"
"unicode"

"gopkg.in/yaml.v3"

survey "github.com/AlecAivazis/survey/v2"
surveycore "github.com/AlecAivazis/survey/v2/core"
"github.com/opentracing/opentracing-go"
Expand All @@ -38,7 +36,6 @@ import (
"github.com/pulumi/pulumi/pkg/v3/backend/display"
"github.com/pulumi/pulumi/pkg/v3/backend/state"
"github.com/pulumi/pulumi/pkg/v3/engine"
"github.com/pulumi/pulumi/pkg/v3/util/yamlutil"
"github.com/pulumi/pulumi/sdk/v3/go/common/apitype"
"github.com/pulumi/pulumi/sdk/v3/go/common/diag/colors"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/config"
Expand Down Expand Up @@ -257,63 +254,23 @@ func runNew(ctx context.Context, args newArgs) error {
fmt.Println()

// Load the project, update the name & description, remove the template section, and save it.
proj, path, err := readProjectWithPath()
root := filepath.Dir(path)
proj, root, err := readProject()
if err != nil {
return err
}

if filepath.Ext(path) == ".yaml" {
filedata, err := os.ReadFile(path)
if err != nil {
return err
}
var workspaceDocument yaml.Node
err = yaml.Unmarshal(filedata, &workspaceDocument)
if err != nil {
return err
}

proj.Name = tokens.PackageName(args.name)
err = yamlutil.Insert(&workspaceDocument, "name", args.name)
if err != nil {
return err
}
proj.Description = &args.description
err = yamlutil.Insert(&workspaceDocument, "description", args.description)
if err != nil {
return err
}
proj.Template = nil
err = yamlutil.Delete(&workspaceDocument, "template")
if err != nil {
return err
}
if proj.Runtime.Name() == "python" {
// If the template does give virtualenv use it, else default to "venv"
if len(proj.Runtime.Options()) == 0 {
proj.Runtime.SetOption("virtualenv", "venv")
err = yamlutil.Insert(&workspaceDocument, "runtime", strings.TrimSpace(`
name: python
options:
virtualenv: venv
`))
if err != nil {
return err
}
}
}

contract.Assert(len(workspaceDocument.Content) == 1)
projFile, err := yaml.Marshal(workspaceDocument.Content[0])
if err != nil {
return err
proj.Name = tokens.PackageName(args.name)
proj.Description = &args.description
proj.Template = nil
// Workaround for python, most of our templates don't specify a venv but we want to use one
if proj.Runtime.Name() == "python" {
// If the template does give virtualenv use it, else default to "venv"
if _, has := proj.Runtime.Options()["virtualenv"]; !has {
proj.Runtime.SetOption("virtualenv", "venv")
}
}

err = os.WriteFile(path, projFile, 0600)
if err != nil {
return err
}
if err = workspace.SaveProject(proj); err != nil {
return fmt.Errorf("saving project: %w", err)
}

appendFileName := "Pulumi.yaml.append"
Expand Down
44 changes: 8 additions & 36 deletions pkg/cmd/pulumi/policy_new.go
Expand Up @@ -19,7 +19,6 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"sort"
"strings"

Expand All @@ -28,14 +27,12 @@ import (
"github.com/opentracing/opentracing-go"
"github.com/pulumi/pulumi/pkg/v3/backend/display"
"github.com/pulumi/pulumi/pkg/v3/engine"
"github.com/pulumi/pulumi/pkg/v3/util/yamlutil"
"github.com/pulumi/pulumi/sdk/v3/go/common/diag/colors"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/cmdutil"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)

type newPolicyArgs struct {
Expand Down Expand Up @@ -178,41 +175,16 @@ func runNewPolicyPack(ctx context.Context, args newPolicyArgs) error {
return err
}

if filepath.Ext(projPath) == ".yaml" {
filedata, err := os.ReadFile(projPath)
if err != nil {
return err
}
var workspaceDocument yaml.Node
err = yaml.Unmarshal(filedata, &workspaceDocument)
if err != nil {
return err
}
if proj.Runtime.Name() == "python" {
// If the template does give virtualenv use it, else default to "venv"
if len(proj.Runtime.Options()) == 0 {
proj.Runtime.SetOption("virtualenv", "venv")
err = yamlutil.Insert(&workspaceDocument, "runtime", strings.TrimSpace(`
name: python
options:
virtualenv: venv
`))
if err != nil {
return err
}
}
}

contract.Assert(len(workspaceDocument.Content) == 1)
projFile, err := yaml.Marshal(workspaceDocument.Content[0])
if err != nil {
return err
// Workaround for python, most of our templates don't specify a venv but we want to use one
if proj.Runtime.Name() == "python" {
// If the template does give virtualenv use it, else default to "venv"
if _, has := proj.Runtime.Options()["virtualenv"]; !has {
proj.Runtime.SetOption("virtualenv", "venv")
}
}

err = os.WriteFile(projPath, projFile, 0600)
if err != nil {
return err
}
if err = proj.Save(projPath); err != nil {
return fmt.Errorf("saving project: %w", err)
}

// Install dependencies.
Expand Down
177 changes: 0 additions & 177 deletions pkg/util/yamlutil/node_tools.go

This file was deleted.