Skip to content

Commit

Permalink
cli: Clean up previous yamlutil package, update new commands
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronFriel committed Nov 23, 2022
1 parent fc7b5b0 commit 5d9fcc4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 347 deletions.
67 changes: 12 additions & 55 deletions pkg/cmd/pulumi/new.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 @@ -175,41 +172,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.

0 comments on commit 5d9fcc4

Please sign in to comment.