2022-03-13 21:39:22 +00:00
|
|
|
// Copyright 2016-2022, Pulumi Corporation.
|
2018-05-22 19:43:36 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2017-01-13 22:32:10 +00:00
|
|
|
|
2018-02-14 21:56:16 +00:00
|
|
|
package workspace
|
2017-01-13 22:32:10 +00:00
|
|
|
|
|
|
|
import (
|
2022-08-25 12:52:17 +00:00
|
|
|
_ "embed"
|
2018-06-25 05:47:54 +00:00
|
|
|
"encoding/json"
|
2023-01-19 20:22:54 +00:00
|
|
|
"errors"
|
2022-08-25 12:52:17 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2022-10-19 08:12:25 +00:00
|
|
|
"math"
|
2018-02-27 23:51:19 +00:00
|
|
|
"os"
|
2017-10-17 18:27:54 +00:00
|
|
|
"path/filepath"
|
2022-09-22 15:32:18 +00:00
|
|
|
"strconv"
|
2022-08-25 12:52:17 +00:00
|
|
|
"strings"
|
2017-10-17 18:27:54 +00:00
|
|
|
|
2023-11-22 05:04:14 +00:00
|
|
|
"github.com/pulumi/esc/ast"
|
|
|
|
"github.com/pulumi/esc/eval"
|
2023-12-19 22:09:38 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-multierror"
|
|
|
|
"github.com/pgavlin/fx"
|
2021-03-17 13:20:05 +00:00
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/encoding"
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/config"
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
|
2024-01-24 16:47:12 +00:00
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/util/logging"
|
2022-08-25 12:52:17 +00:00
|
|
|
"github.com/santhosh-tekuri/jsonschema/v5"
|
2023-11-22 05:04:14 +00:00
|
|
|
"golang.org/x/exp/maps"
|
2023-10-10 01:35:39 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2017-01-13 22:32:10 +00:00
|
|
|
)
|
|
|
|
|
2022-10-30 22:42:39 +00:00
|
|
|
const (
|
|
|
|
arrayTypeName = "array"
|
|
|
|
integerTypeName = "integer"
|
|
|
|
stringTypeName = "string"
|
|
|
|
booleanTypeName = "boolean"
|
|
|
|
)
|
|
|
|
|
2022-08-25 12:52:17 +00:00
|
|
|
//go:embed project.json
|
|
|
|
var projectSchema string
|
|
|
|
|
|
|
|
var ProjectSchema *jsonschema.Schema
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
compiler := jsonschema.NewCompiler()
|
|
|
|
compiler.LoadURL = func(u string) (io.ReadCloser, error) {
|
|
|
|
if u == "blob://project.json" {
|
|
|
|
return io.NopCloser(strings.NewReader(projectSchema)), nil
|
|
|
|
}
|
|
|
|
return jsonschema.LoadURL(u)
|
|
|
|
}
|
|
|
|
ProjectSchema = compiler.MustCompile("blob://project.json")
|
|
|
|
}
|
|
|
|
|
2018-02-14 21:56:16 +00:00
|
|
|
// Analyzers is a list of analyzers to run on this project.
|
|
|
|
type Analyzers []tokens.QName
|
|
|
|
|
Initial support for passing URLs to `new` and `up` (#1727)
* Initial support for passing URLs to `new` and `up`
This PR adds initial support for `pulumi new` using Git under the covers
to manage Pulumi templates, providing the same experience as before.
You can now also optionally pass a URL to a Git repository, e.g.
`pulumi new [<url>]`, including subdirectories within the repository,
and arbitrary branches, tags, or commits.
The following commands result in the same behavior from the user's
perspective:
- `pulumi new javascript`
- `pulumi new https://github.com/pulumi/templates/templates/javascript`
- `pulumi new https://github.com/pulumi/templates/tree/master/templates/javascript`
- `pulumi new https://github.com/pulumi/templates/tree/HEAD/templates/javascript`
To specify an arbitrary branch, tag, or commit:
- `pulumi new https://github.com/pulumi/templates/tree/<branch>/templates/javascript`
- `pulumi new https://github.com/pulumi/templates/tree/<tag>/templates/javascript`
- `pulumi new https://github.com/pulumi/templates/tree/<commit>/templates/javascript`
Branches and tags can include '/' separators, and `pulumi` will still
find the right subdirectory.
URLs to Gists are also supported, e.g.:
`pulumi new https://gist.github.com/justinvp/6673959ceb9d2ac5a14c6d536cb871a6`
If the specified subdirectory in the repository does not contain a
`Pulumi.yaml`, it will look for subdirectories within containing
`Pulumi.yaml` files, and prompt the user to choose a template, along the
lines of how `pulumi new` behaves when no template is specified.
The following commands result in the CLI prompting to choose a template:
- `pulumi new`
- `pulumi new https://github.com/pulumi/templates/templates`
- `pulumi new https://github.com/pulumi/templates/tree/master/templates`
- `pulumi new https://github.com/pulumi/templates/tree/HEAD/templates`
Of course, arbitrary branches, tags, or commits can be specified as well:
- `pulumi new https://github.com/pulumi/templates/tree/<branch>/templates`
- `pulumi new https://github.com/pulumi/templates/tree/<tag>/templates`
- `pulumi new https://github.com/pulumi/templates/tree/<commit>/templates`
This PR also includes initial support for passing URLs to `pulumi up`,
providing a streamlined way to deploy installable cloud applications
with Pulumi, without having to manage source code locally before doing
a deployment.
For example, `pulumi up https://github.com/justinvp/aws` can be used to
deploy a sample AWS app. The stack can be updated with different
versions, e.g.
`pulumi up https://github.com/justinvp/aws/tree/v2 -s <stack-to-update>`
Config values can optionally be passed via command line flags, e.g.
`pulumi up https://github.com/justinvp/aws -c aws:region=us-west-2 -c foo:bar=blah`
Gists can also be used, e.g.
`pulumi up https://gist.github.com/justinvp/62fde0463f243fcb49f5a7222e51bc76`
* Fix panic when hitting ^C from "choose template" prompt
* Add description to templates
When running `pulumi new` without specifying a template, include the template description along with the name in the "choose template" display.
```
$ pulumi new
Please choose a template:
aws-go A minimal AWS Go program
aws-javascript A minimal AWS JavaScript program
aws-python A minimal AWS Python program
aws-typescript A minimal AWS TypeScript program
> go A minimal Go program
hello-aws-javascript A simple AWS serverless JavaScript program
javascript A minimal JavaScript program
python A minimal Python program
typescript A minimal TypeScript program
```
* React to changes to the pulumi/templates repo.
We restructured the `pulumi/templates` repo to have all the templates in the root instead of in a `templates` subdirectory, so make the change here to no longer look for templates in `templates`.
This also fixes an issue around using `Depth: 1` that I found while testing this. When a named template is used, we attempt to clone or pull from the `pulumi/templates` repo to `~/.pulumi/templates`. Having it go in this well-known directory allows us to maintain previous behavior around allowing offline use of templates. If we use `Depth: 1` for the initial clone, it will fail when attempting to pull when there are updates to the remote repository. Unfortunately, there's no built-in `--unshallow` support in `go-git` and setting a larger `Depth` doesn't appear to help. There may be a workaround, but for now, if we're cloning the pulumi templates directory to `~/.pulumi/templates`, we won't use `Depth: 1`. For template URLs, we will continue to use `Depth: 1` as we clone those to a temp directory (which gets deleted) that we'll never try to update.
* List available templates in help text
* Address PR Feedback
* Don't show "Installing dependencies" message for `up`
* Fix secrets handling
When prompting for config, if the existing stack value is a secret, keep it a secret and mask the prompt. If the template says it should be secret, make it a secret.
* Fix ${PROJECT} and ${DESCRIPTION} handling for `up`
Templates used with `up` should already have a filled-in project name and description, but if it's a `new`-style template, that has `${PROJECT}` and/or `${DESCRIPTION}`, be helpful and just replace these with better values.
* Fix stack handling
Add a bool `setCurrent` param to `requireStack` to control whether the current stack should be saved in workspace settings. For the `up <url>` case, we don't want to save. Also, split the `up` code into two separate functions: one for the `up <url>` case and another for the normal `up` case where you have workspace in your current directory. While we may be able to combine them back into a single function, right now it's a bit cleaner being separate, even with some small amount of duplication.
* Fix panic due to nil crypter
Lazily get the crypter only if needed inside `promptForConfig`.
* Embellish comment
* Harden isPreconfiguredEmptyStack check
Fix the code to check to make sure the URL specified on the command line matches the URL stored in the `pulumi:template` config value, and that the rest of the config from the stack satisfies the config requirements of the template.
2018-08-11 01:08:16 +00:00
|
|
|
// ProjectTemplate is a Pulumi project template manifest.
|
|
|
|
type ProjectTemplate struct {
|
2023-11-18 19:04:13 +00:00
|
|
|
// DisplayName is an optional user friendly name of the template.
|
|
|
|
DisplayName string `json:"displayName,omitempty" yaml:"displayName,omitempty"`
|
2018-11-01 15:28:11 +00:00
|
|
|
// Description is an optional description of the template.
|
|
|
|
Description string `json:"description,omitempty" yaml:"description,omitempty"`
|
|
|
|
// Quickstart contains optional text to be displayed after template creation.
|
|
|
|
Quickstart string `json:"quickstart,omitempty" yaml:"quickstart,omitempty"`
|
|
|
|
// Config is an optional template config.
|
|
|
|
Config map[string]ProjectTemplateConfigValue `json:"config,omitempty" yaml:"config,omitempty"`
|
2023-11-18 19:04:13 +00:00
|
|
|
// Important indicates the template is important.
|
2019-08-28 00:56:49 +00:00
|
|
|
Important bool `json:"important,omitempty" yaml:"important,omitempty"`
|
2023-11-18 19:04:13 +00:00
|
|
|
// Metadata are key/value pairs used to attach additional metadata to a template.
|
|
|
|
Metadata map[string]string `json:"metadata,omitempty" yaml:"metadata,omitempty"`
|
Initial support for passing URLs to `new` and `up` (#1727)
* Initial support for passing URLs to `new` and `up`
This PR adds initial support for `pulumi new` using Git under the covers
to manage Pulumi templates, providing the same experience as before.
You can now also optionally pass a URL to a Git repository, e.g.
`pulumi new [<url>]`, including subdirectories within the repository,
and arbitrary branches, tags, or commits.
The following commands result in the same behavior from the user's
perspective:
- `pulumi new javascript`
- `pulumi new https://github.com/pulumi/templates/templates/javascript`
- `pulumi new https://github.com/pulumi/templates/tree/master/templates/javascript`
- `pulumi new https://github.com/pulumi/templates/tree/HEAD/templates/javascript`
To specify an arbitrary branch, tag, or commit:
- `pulumi new https://github.com/pulumi/templates/tree/<branch>/templates/javascript`
- `pulumi new https://github.com/pulumi/templates/tree/<tag>/templates/javascript`
- `pulumi new https://github.com/pulumi/templates/tree/<commit>/templates/javascript`
Branches and tags can include '/' separators, and `pulumi` will still
find the right subdirectory.
URLs to Gists are also supported, e.g.:
`pulumi new https://gist.github.com/justinvp/6673959ceb9d2ac5a14c6d536cb871a6`
If the specified subdirectory in the repository does not contain a
`Pulumi.yaml`, it will look for subdirectories within containing
`Pulumi.yaml` files, and prompt the user to choose a template, along the
lines of how `pulumi new` behaves when no template is specified.
The following commands result in the CLI prompting to choose a template:
- `pulumi new`
- `pulumi new https://github.com/pulumi/templates/templates`
- `pulumi new https://github.com/pulumi/templates/tree/master/templates`
- `pulumi new https://github.com/pulumi/templates/tree/HEAD/templates`
Of course, arbitrary branches, tags, or commits can be specified as well:
- `pulumi new https://github.com/pulumi/templates/tree/<branch>/templates`
- `pulumi new https://github.com/pulumi/templates/tree/<tag>/templates`
- `pulumi new https://github.com/pulumi/templates/tree/<commit>/templates`
This PR also includes initial support for passing URLs to `pulumi up`,
providing a streamlined way to deploy installable cloud applications
with Pulumi, without having to manage source code locally before doing
a deployment.
For example, `pulumi up https://github.com/justinvp/aws` can be used to
deploy a sample AWS app. The stack can be updated with different
versions, e.g.
`pulumi up https://github.com/justinvp/aws/tree/v2 -s <stack-to-update>`
Config values can optionally be passed via command line flags, e.g.
`pulumi up https://github.com/justinvp/aws -c aws:region=us-west-2 -c foo:bar=blah`
Gists can also be used, e.g.
`pulumi up https://gist.github.com/justinvp/62fde0463f243fcb49f5a7222e51bc76`
* Fix panic when hitting ^C from "choose template" prompt
* Add description to templates
When running `pulumi new` without specifying a template, include the template description along with the name in the "choose template" display.
```
$ pulumi new
Please choose a template:
aws-go A minimal AWS Go program
aws-javascript A minimal AWS JavaScript program
aws-python A minimal AWS Python program
aws-typescript A minimal AWS TypeScript program
> go A minimal Go program
hello-aws-javascript A simple AWS serverless JavaScript program
javascript A minimal JavaScript program
python A minimal Python program
typescript A minimal TypeScript program
```
* React to changes to the pulumi/templates repo.
We restructured the `pulumi/templates` repo to have all the templates in the root instead of in a `templates` subdirectory, so make the change here to no longer look for templates in `templates`.
This also fixes an issue around using `Depth: 1` that I found while testing this. When a named template is used, we attempt to clone or pull from the `pulumi/templates` repo to `~/.pulumi/templates`. Having it go in this well-known directory allows us to maintain previous behavior around allowing offline use of templates. If we use `Depth: 1` for the initial clone, it will fail when attempting to pull when there are updates to the remote repository. Unfortunately, there's no built-in `--unshallow` support in `go-git` and setting a larger `Depth` doesn't appear to help. There may be a workaround, but for now, if we're cloning the pulumi templates directory to `~/.pulumi/templates`, we won't use `Depth: 1`. For template URLs, we will continue to use `Depth: 1` as we clone those to a temp directory (which gets deleted) that we'll never try to update.
* List available templates in help text
* Address PR Feedback
* Don't show "Installing dependencies" message for `up`
* Fix secrets handling
When prompting for config, if the existing stack value is a secret, keep it a secret and mask the prompt. If the template says it should be secret, make it a secret.
* Fix ${PROJECT} and ${DESCRIPTION} handling for `up`
Templates used with `up` should already have a filled-in project name and description, but if it's a `new`-style template, that has `${PROJECT}` and/or `${DESCRIPTION}`, be helpful and just replace these with better values.
* Fix stack handling
Add a bool `setCurrent` param to `requireStack` to control whether the current stack should be saved in workspace settings. For the `up <url>` case, we don't want to save. Also, split the `up` code into two separate functions: one for the `up <url>` case and another for the normal `up` case where you have workspace in your current directory. While we may be able to combine them back into a single function, right now it's a bit cleaner being separate, even with some small amount of duplication.
* Fix panic due to nil crypter
Lazily get the crypter only if needed inside `promptForConfig`.
* Embellish comment
* Harden isPreconfiguredEmptyStack check
Fix the code to check to make sure the URL specified on the command line matches the URL stored in the `pulumi:template` config value, and that the rest of the config from the stack satisfies the config requirements of the template.
2018-08-11 01:08:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ProjectTemplateConfigValue is a config value included in the project template manifest.
|
|
|
|
type ProjectTemplateConfigValue struct {
|
2018-11-01 15:28:11 +00:00
|
|
|
// Description is an optional description for the config value.
|
|
|
|
Description string `json:"description,omitempty" yaml:"description,omitempty"`
|
|
|
|
// Default is an optional default value for the config value.
|
|
|
|
Default string `json:"default,omitempty" yaml:"default,omitempty"`
|
|
|
|
// Secret may be set to true to indicate that the config value should be encrypted.
|
|
|
|
Secret bool `json:"secret,omitempty" yaml:"secret,omitempty"`
|
Initial support for passing URLs to `new` and `up` (#1727)
* Initial support for passing URLs to `new` and `up`
This PR adds initial support for `pulumi new` using Git under the covers
to manage Pulumi templates, providing the same experience as before.
You can now also optionally pass a URL to a Git repository, e.g.
`pulumi new [<url>]`, including subdirectories within the repository,
and arbitrary branches, tags, or commits.
The following commands result in the same behavior from the user's
perspective:
- `pulumi new javascript`
- `pulumi new https://github.com/pulumi/templates/templates/javascript`
- `pulumi new https://github.com/pulumi/templates/tree/master/templates/javascript`
- `pulumi new https://github.com/pulumi/templates/tree/HEAD/templates/javascript`
To specify an arbitrary branch, tag, or commit:
- `pulumi new https://github.com/pulumi/templates/tree/<branch>/templates/javascript`
- `pulumi new https://github.com/pulumi/templates/tree/<tag>/templates/javascript`
- `pulumi new https://github.com/pulumi/templates/tree/<commit>/templates/javascript`
Branches and tags can include '/' separators, and `pulumi` will still
find the right subdirectory.
URLs to Gists are also supported, e.g.:
`pulumi new https://gist.github.com/justinvp/6673959ceb9d2ac5a14c6d536cb871a6`
If the specified subdirectory in the repository does not contain a
`Pulumi.yaml`, it will look for subdirectories within containing
`Pulumi.yaml` files, and prompt the user to choose a template, along the
lines of how `pulumi new` behaves when no template is specified.
The following commands result in the CLI prompting to choose a template:
- `pulumi new`
- `pulumi new https://github.com/pulumi/templates/templates`
- `pulumi new https://github.com/pulumi/templates/tree/master/templates`
- `pulumi new https://github.com/pulumi/templates/tree/HEAD/templates`
Of course, arbitrary branches, tags, or commits can be specified as well:
- `pulumi new https://github.com/pulumi/templates/tree/<branch>/templates`
- `pulumi new https://github.com/pulumi/templates/tree/<tag>/templates`
- `pulumi new https://github.com/pulumi/templates/tree/<commit>/templates`
This PR also includes initial support for passing URLs to `pulumi up`,
providing a streamlined way to deploy installable cloud applications
with Pulumi, without having to manage source code locally before doing
a deployment.
For example, `pulumi up https://github.com/justinvp/aws` can be used to
deploy a sample AWS app. The stack can be updated with different
versions, e.g.
`pulumi up https://github.com/justinvp/aws/tree/v2 -s <stack-to-update>`
Config values can optionally be passed via command line flags, e.g.
`pulumi up https://github.com/justinvp/aws -c aws:region=us-west-2 -c foo:bar=blah`
Gists can also be used, e.g.
`pulumi up https://gist.github.com/justinvp/62fde0463f243fcb49f5a7222e51bc76`
* Fix panic when hitting ^C from "choose template" prompt
* Add description to templates
When running `pulumi new` without specifying a template, include the template description along with the name in the "choose template" display.
```
$ pulumi new
Please choose a template:
aws-go A minimal AWS Go program
aws-javascript A minimal AWS JavaScript program
aws-python A minimal AWS Python program
aws-typescript A minimal AWS TypeScript program
> go A minimal Go program
hello-aws-javascript A simple AWS serverless JavaScript program
javascript A minimal JavaScript program
python A minimal Python program
typescript A minimal TypeScript program
```
* React to changes to the pulumi/templates repo.
We restructured the `pulumi/templates` repo to have all the templates in the root instead of in a `templates` subdirectory, so make the change here to no longer look for templates in `templates`.
This also fixes an issue around using `Depth: 1` that I found while testing this. When a named template is used, we attempt to clone or pull from the `pulumi/templates` repo to `~/.pulumi/templates`. Having it go in this well-known directory allows us to maintain previous behavior around allowing offline use of templates. If we use `Depth: 1` for the initial clone, it will fail when attempting to pull when there are updates to the remote repository. Unfortunately, there's no built-in `--unshallow` support in `go-git` and setting a larger `Depth` doesn't appear to help. There may be a workaround, but for now, if we're cloning the pulumi templates directory to `~/.pulumi/templates`, we won't use `Depth: 1`. For template URLs, we will continue to use `Depth: 1` as we clone those to a temp directory (which gets deleted) that we'll never try to update.
* List available templates in help text
* Address PR Feedback
* Don't show "Installing dependencies" message for `up`
* Fix secrets handling
When prompting for config, if the existing stack value is a secret, keep it a secret and mask the prompt. If the template says it should be secret, make it a secret.
* Fix ${PROJECT} and ${DESCRIPTION} handling for `up`
Templates used with `up` should already have a filled-in project name and description, but if it's a `new`-style template, that has `${PROJECT}` and/or `${DESCRIPTION}`, be helpful and just replace these with better values.
* Fix stack handling
Add a bool `setCurrent` param to `requireStack` to control whether the current stack should be saved in workspace settings. For the `up <url>` case, we don't want to save. Also, split the `up` code into two separate functions: one for the `up <url>` case and another for the normal `up` case where you have workspace in your current directory. While we may be able to combine them back into a single function, right now it's a bit cleaner being separate, even with some small amount of duplication.
* Fix panic due to nil crypter
Lazily get the crypter only if needed inside `promptForConfig`.
* Embellish comment
* Harden isPreconfiguredEmptyStack check
Fix the code to check to make sure the URL specified on the command line matches the URL stored in the `pulumi:template` config value, and that the rest of the config from the stack satisfies the config requirements of the template.
2018-08-11 01:08:16 +00:00
|
|
|
}
|
|
|
|
|
2024-01-25 20:58:12 +00:00
|
|
|
// ProjectBackend is the configuration for where the backend state is stored. If unset, will use the
|
|
|
|
// system's currently logged-in backend.
|
|
|
|
//
|
|
|
|
// Use the same URL format that is passed to "pulumi login", see
|
|
|
|
// https://www.pulumi.com/docs/cli/commands/pulumi_login/
|
|
|
|
//
|
|
|
|
// To explicitly use the Pulumi Cloud backend, use URL "https://api.pulumi.com"
|
2019-06-10 22:07:21 +00:00
|
|
|
type ProjectBackend struct {
|
|
|
|
// URL is optional field to explicitly set backend url
|
|
|
|
URL string `json:"url,omitempty" yaml:"url,omitempty"`
|
|
|
|
}
|
|
|
|
|
2021-09-29 09:43:48 +00:00
|
|
|
type ProjectOptions struct {
|
|
|
|
// Refresh is the ability to always run a refresh as part of a pulumi update / preview / destroy
|
|
|
|
Refresh string `json:"refresh,omitempty" yaml:"refresh,omitempty"`
|
|
|
|
}
|
|
|
|
|
2022-07-22 13:17:43 +00:00
|
|
|
type PluginOptions struct {
|
|
|
|
Name string `json:"name" yaml:"name"`
|
|
|
|
Version string `json:"version,omitempty" yaml:"version,omitempty"`
|
|
|
|
Path string `json:"path" yaml:"path"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Plugins struct {
|
|
|
|
Providers []PluginOptions `json:"providers,omitempty" yaml:"providers,omitempty"`
|
|
|
|
Languages []PluginOptions `json:"languages,omitempty" yaml:"languages,omitempty"`
|
|
|
|
Analyzers []PluginOptions `json:"analyzers,omitempty" yaml:"analyzers,omitempty"`
|
|
|
|
}
|
|
|
|
|
2022-09-20 18:13:45 +00:00
|
|
|
type ProjectConfigItemsType struct {
|
2022-10-05 13:10:19 +00:00
|
|
|
Type string `json:"type,omitempty" yaml:"type,omitempty"`
|
|
|
|
Items *ProjectConfigItemsType `json:"items,omitempty" yaml:"items,omitempty"`
|
2022-09-20 18:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectConfigType struct {
|
2022-10-30 22:42:39 +00:00
|
|
|
Type *string `json:"type,omitempty" yaml:"type,omitempty"`
|
2022-10-05 13:10:19 +00:00
|
|
|
Description string `json:"description,omitempty" yaml:"description,omitempty"`
|
|
|
|
Items *ProjectConfigItemsType `json:"items,omitempty" yaml:"items,omitempty"`
|
|
|
|
Default interface{} `json:"default,omitempty" yaml:"default,omitempty"`
|
2022-10-30 22:42:39 +00:00
|
|
|
Value interface{} `json:"value,omitempty" yaml:"value,omitempty"`
|
2022-10-19 08:12:25 +00:00
|
|
|
Secret bool `json:"secret,omitempty" yaml:"secret,omitempty"`
|
2022-09-20 18:13:45 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 22:42:39 +00:00
|
|
|
// IsExplicitlyTyped returns whether the project config type is explicitly typed.
|
|
|
|
// When that is the case, we validate stack config values against this type, given that
|
|
|
|
// the stack config value is namespaced by the project.
|
|
|
|
func (configType *ProjectConfigType) IsExplicitlyTyped() bool {
|
|
|
|
return configType.Type != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (configType *ProjectConfigType) TypeName() string {
|
|
|
|
if configType.Type != nil {
|
|
|
|
return *configType.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2018-11-01 15:28:11 +00:00
|
|
|
// Project is a Pulumi project manifest.
|
2017-10-17 17:48:53 +00:00
|
|
|
//
|
2017-11-16 15:49:07 +00:00
|
|
|
// We explicitly add yaml tags (instead of using the default behavior from https://github.com/ghodss/yaml which works
|
|
|
|
// in terms of the JSON tags) so we can directly marshall and unmarshall this struct using go-yaml an have the fields
|
|
|
|
// in the serialized object match the order they are defined in this struct.
|
|
|
|
//
|
|
|
|
// TODO[pulumi/pulumi#423]: use DOM based marshalling so we can roundtrip the seralized structure perfectly.
|
2018-02-14 21:56:16 +00:00
|
|
|
type Project struct {
|
2018-11-01 15:28:11 +00:00
|
|
|
// Name is a required fully qualified name.
|
|
|
|
Name tokens.PackageName `json:"name" yaml:"name"`
|
|
|
|
// Runtime is a required runtime that executes code.
|
|
|
|
Runtime ProjectRuntimeInfo `json:"runtime" yaml:"runtime"`
|
|
|
|
// Main is an optional override for the program's main entry-point location.
|
|
|
|
Main string `json:"main,omitempty" yaml:"main,omitempty"`
|
|
|
|
|
|
|
|
// Description is an optional informational description.
|
|
|
|
Description *string `json:"description,omitempty" yaml:"description,omitempty"`
|
|
|
|
// Author is an optional author that created this project.
|
|
|
|
Author *string `json:"author,omitempty" yaml:"author,omitempty"`
|
|
|
|
// Website is an optional website for additional info about this project.
|
|
|
|
Website *string `json:"website,omitempty" yaml:"website,omitempty"`
|
|
|
|
// License is the optional license governing this project's usage.
|
|
|
|
License *string `json:"license,omitempty" yaml:"license,omitempty"`
|
|
|
|
|
2022-03-13 21:39:22 +00:00
|
|
|
// Config has been renamed to StackConfigDir.
|
2022-09-20 18:13:45 +00:00
|
|
|
Config map[string]ProjectConfigType `json:"config,omitempty" yaml:"config,omitempty"`
|
2022-03-13 21:39:22 +00:00
|
|
|
|
|
|
|
// StackConfigDir indicates where to store the Pulumi.<stack-name>.yaml files, combined with the folder
|
|
|
|
// Pulumi.yaml is in.
|
|
|
|
StackConfigDir string `json:"stackConfigDir,omitempty" yaml:"stackConfigDir,omitempty"`
|
2018-11-01 15:28:11 +00:00
|
|
|
|
|
|
|
// Template is an optional template manifest, if this project is a template.
|
|
|
|
Template *ProjectTemplate `json:"template,omitempty" yaml:"template,omitempty"`
|
2019-06-10 22:07:21 +00:00
|
|
|
|
|
|
|
// Backend is an optional backend configuration
|
|
|
|
Backend *ProjectBackend `json:"backend,omitempty" yaml:"backend,omitempty"`
|
2021-09-29 09:43:48 +00:00
|
|
|
|
|
|
|
// Options is an optional set of project options
|
|
|
|
Options *ProjectOptions `json:"options,omitempty" yaml:"options,omitempty"`
|
2022-07-22 13:17:43 +00:00
|
|
|
|
|
|
|
Plugins *Plugins `json:"plugins,omitempty" yaml:"plugins,omitempty"`
|
2022-08-25 15:49:28 +00:00
|
|
|
|
|
|
|
// Handle additional keys, albeit in a way that will remove comments and trivia.
|
|
|
|
AdditionalKeys map[string]interface{} `yaml:",inline"`
|
2022-11-23 20:22:57 +00:00
|
|
|
|
|
|
|
// The original byte representation of the file, used to attempt trivia-preserving edits
|
|
|
|
raw []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (proj Project) RawValue() []byte {
|
|
|
|
return proj.raw
|
2017-01-13 22:32:10 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 22:42:39 +00:00
|
|
|
func isPrimitiveValue(value interface{}) bool {
|
2022-10-13 17:52:09 +00:00
|
|
|
switch value.(type) {
|
2022-10-30 22:42:39 +00:00
|
|
|
case string, int, bool:
|
|
|
|
return true
|
2022-10-13 17:52:09 +00:00
|
|
|
default:
|
2022-10-30 22:42:39 +00:00
|
|
|
return false
|
2022-09-21 20:58:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-30 22:42:39 +00:00
|
|
|
func isArray(value interface{}) bool {
|
|
|
|
_, ok := value.([]interface{})
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2022-09-24 12:32:11 +00:00
|
|
|
// RewriteConfigPathIntoStackConfigDir checks if the project is using the old "config" property
|
|
|
|
// to declare a path to the stack configuration directory. If that is the case, we rewrite it
|
|
|
|
// such that the value in config: {value} is moved to stackConfigDir: {value}.
|
|
|
|
// if the user defines both values as strings, we error out.
|
|
|
|
func RewriteConfigPathIntoStackConfigDir(project map[string]interface{}) (map[string]interface{}, error) {
|
|
|
|
config, hasConfig := project["config"]
|
|
|
|
_, hasStackConfigDir := project["stackConfigDir"]
|
|
|
|
|
|
|
|
if hasConfig {
|
|
|
|
configText, configIsText := config.(string)
|
|
|
|
if configIsText && hasStackConfigDir {
|
|
|
|
return nil, errors.New("Should not use both config and stackConfigDir to define the stack directory. " +
|
|
|
|
"Use only stackConfigDir instead.")
|
|
|
|
} else if configIsText && !hasStackConfigDir {
|
|
|
|
// then we have config: {value}. Move this to stackConfigDir: {value}
|
|
|
|
project["stackConfigDir"] = configText
|
|
|
|
// reset the config property
|
|
|
|
project["config"] = nil
|
|
|
|
return project, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return project, nil
|
|
|
|
}
|
|
|
|
|
2022-09-21 18:37:43 +00:00
|
|
|
// RewriteShorthandConfigValues rewrites short-hand version of configuration into a configuration type
|
|
|
|
// for example the following config block definition:
|
|
|
|
//
|
2022-10-12 13:38:21 +00:00
|
|
|
// config:
|
2022-10-30 22:42:39 +00:00
|
|
|
// instanceSize: t3.mirco
|
|
|
|
// aws:region: us-west-2
|
2022-09-21 18:37:43 +00:00
|
|
|
//
|
|
|
|
// will be rewritten into a typed value:
|
|
|
|
//
|
2022-10-12 13:38:21 +00:00
|
|
|
// config:
|
2022-10-30 22:42:39 +00:00
|
|
|
// instanceSize:
|
|
|
|
// default: t3.micro
|
|
|
|
// aws:region:
|
|
|
|
// value: us-west-2
|
|
|
|
//
|
|
|
|
// Note that short-hand values without namespaces (project config) are turned into a type
|
|
|
|
// where as short-hand values with namespaces (such as aws:region) are turned into a value.
|
2022-09-21 18:37:43 +00:00
|
|
|
func RewriteShorthandConfigValues(project map[string]interface{}) map[string]interface{} {
|
|
|
|
configMap, foundConfig := project["config"]
|
2022-10-30 22:42:39 +00:00
|
|
|
projectName := project["name"].(string)
|
2022-09-21 18:37:43 +00:00
|
|
|
if !foundConfig {
|
|
|
|
// no config defined, return as is
|
|
|
|
return project
|
|
|
|
}
|
|
|
|
|
|
|
|
config, ok := configMap.(map[string]interface{})
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return project
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range config {
|
2022-10-30 22:42:39 +00:00
|
|
|
if isPrimitiveValue(value) || isArray(value) {
|
2022-09-21 18:37:43 +00:00
|
|
|
configTypeDefinition := make(map[string]interface{})
|
2022-10-30 22:42:39 +00:00
|
|
|
if configKeyIsNamespacedByProject(projectName, key) {
|
|
|
|
// then this is a project namespaced config _type_ with a default value
|
|
|
|
configTypeDefinition["default"] = value
|
|
|
|
} else {
|
|
|
|
// then this is a non-project namespaced config _value_
|
|
|
|
configTypeDefinition["value"] = value
|
|
|
|
}
|
|
|
|
|
2022-09-21 18:37:43 +00:00
|
|
|
config[key] = configTypeDefinition
|
2022-10-30 22:42:39 +00:00
|
|
|
continue
|
2022-09-21 18:37:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return project
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cast any map[interface{}] from the yaml decoder to map[string]
|
2022-10-30 22:42:39 +00:00
|
|
|
func SimplifyMarshalledValue(raw interface{}) (interface{}, error) {
|
2022-09-02 20:16:27 +00:00
|
|
|
var cast func(value interface{}) (interface{}, error)
|
|
|
|
cast = func(value interface{}) (interface{}, error) {
|
|
|
|
if objMap, ok := value.(map[interface{}]interface{}); ok {
|
|
|
|
strMap := make(map[string]interface{})
|
|
|
|
for key, value := range objMap {
|
|
|
|
if strKey, ok := key.(string); ok {
|
|
|
|
innerValue, err := cast(value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-09-02 17:25:45 +00:00
|
|
|
}
|
2022-09-02 20:16:27 +00:00
|
|
|
strMap[strKey] = innerValue
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("expected only string keys, got '%s'", key)
|
2022-09-02 17:25:45 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-02 20:16:27 +00:00
|
|
|
return strMap, nil
|
|
|
|
} else if objArray, ok := value.([]interface{}); ok {
|
|
|
|
strArray := make([]interface{}, len(objArray))
|
|
|
|
for key, value := range objArray {
|
|
|
|
innerValue, err := cast(value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
strArray[key] = innerValue
|
|
|
|
}
|
|
|
|
return strArray, nil
|
2022-09-02 17:25:45 +00:00
|
|
|
}
|
2022-09-02 20:16:27 +00:00
|
|
|
return value, nil
|
|
|
|
}
|
2022-10-30 22:42:39 +00:00
|
|
|
|
|
|
|
return cast(raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SimplifyMarshalledProject(raw interface{}) (map[string]interface{}, error) {
|
|
|
|
result, err := SimplifyMarshalledValue(raw)
|
2022-09-02 20:16:27 +00:00
|
|
|
if err != nil {
|
2022-09-21 18:37:43 +00:00
|
|
|
return nil, err
|
2022-09-02 20:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
var obj map[string]interface{}
|
|
|
|
if obj, ok = result.(map[string]interface{}); !ok {
|
2022-10-12 13:38:21 +00:00
|
|
|
return nil, fmt.Errorf("expected project to be an object, was '%T'", result)
|
2022-09-21 18:37:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return obj, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ValidateProject(raw interface{}) error {
|
|
|
|
project, err := SimplifyMarshalledProject(raw)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-09-02 17:25:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 12:52:17 +00:00
|
|
|
// Couple of manual errors to match Validate
|
2022-09-21 18:37:43 +00:00
|
|
|
name, ok := project["name"]
|
2022-09-02 09:35:14 +00:00
|
|
|
if !ok {
|
2022-08-25 12:52:17 +00:00
|
|
|
return errors.New("project is missing a 'name' attribute")
|
2022-09-02 09:35:14 +00:00
|
|
|
}
|
|
|
|
if strName, ok := name.(string); !ok || strName == "" {
|
|
|
|
return errors.New("project is missing a non-empty string 'name' attribute")
|
2022-08-25 12:52:17 +00:00
|
|
|
}
|
2022-09-21 18:37:43 +00:00
|
|
|
if _, ok := project["runtime"]; !ok {
|
2022-08-25 12:52:17 +00:00
|
|
|
return errors.New("project is missing a 'runtime' attribute")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let everything else be caught by jsonschema
|
2022-09-21 18:37:43 +00:00
|
|
|
if err = ProjectSchema.Validate(project); err == nil {
|
2022-08-25 12:52:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
validationError, ok := err.(*jsonschema.ValidationError)
|
|
|
|
if !ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var errs *multierror.Error
|
|
|
|
var appendError func(err *jsonschema.ValidationError)
|
|
|
|
appendError = func(err *jsonschema.ValidationError) {
|
|
|
|
if err.InstanceLocation != "" && err.Message != "" {
|
|
|
|
errorf := func(path, message string, args ...interface{}) error {
|
2023-02-15 01:06:56 +00:00
|
|
|
contract.Requiref(path != "", "path", "path must not be empty")
|
2022-08-25 12:52:17 +00:00
|
|
|
return fmt.Errorf("%s: %s", path, fmt.Sprintf(message, args...))
|
|
|
|
}
|
|
|
|
|
|
|
|
errs = multierror.Append(errs, errorf("#"+err.InstanceLocation, "%v", err.Message))
|
|
|
|
}
|
|
|
|
for _, err := range err.Causes {
|
|
|
|
appendError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
appendError(validationError)
|
|
|
|
|
2022-09-02 17:25:45 +00:00
|
|
|
return errs
|
2022-08-25 12:52:17 +00:00
|
|
|
}
|
|
|
|
|
2022-09-20 21:29:16 +00:00
|
|
|
func InferFullTypeName(typeName string, itemsType *ProjectConfigItemsType) string {
|
2022-09-20 18:13:45 +00:00
|
|
|
if itemsType != nil {
|
2022-09-20 21:29:16 +00:00
|
|
|
return fmt.Sprintf("array<%v>", InferFullTypeName(itemsType.Type, itemsType.Items))
|
2022-09-20 18:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return typeName
|
|
|
|
}
|
|
|
|
|
2022-10-05 13:10:19 +00:00
|
|
|
// ValidateConfig validates the config value against its config type definition.
|
|
|
|
// We use this to validate the default config values alongside their type definition but
|
|
|
|
// also to validate config values coming from individual stacks.
|
2022-09-20 18:13:45 +00:00
|
|
|
func ValidateConfigValue(typeName string, itemsType *ProjectConfigItemsType, value interface{}) bool {
|
2022-10-30 22:42:39 +00:00
|
|
|
if typeName == stringTypeName {
|
2022-09-20 18:13:45 +00:00
|
|
|
_, ok := value.(string)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2022-10-30 22:42:39 +00:00
|
|
|
if typeName == integerTypeName {
|
2022-09-20 18:13:45 +00:00
|
|
|
_, ok := value.(int)
|
2022-10-19 08:12:25 +00:00
|
|
|
if ok {
|
|
|
|
return true
|
2022-09-22 15:32:18 +00:00
|
|
|
}
|
2022-10-19 08:12:25 +00:00
|
|
|
// Config values come from YAML which by default will return floats not int. If it's a whole number
|
|
|
|
// we'll allow it here though
|
|
|
|
f, ok := value.(float64)
|
|
|
|
if ok && f == math.Trunc(f) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// Allow strings here if they parse as integers
|
|
|
|
valueAsText, isText := value.(string)
|
|
|
|
if isText {
|
|
|
|
_, integerParseError := strconv.Atoi(valueAsText)
|
|
|
|
return integerParseError == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2022-09-20 18:13:45 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 22:42:39 +00:00
|
|
|
if typeName == booleanTypeName {
|
2022-09-21 13:52:14 +00:00
|
|
|
// check to see if the value is a literal string "true" | "false"
|
|
|
|
literalValue, ok := value.(string)
|
|
|
|
if ok && (literalValue == "true" || literalValue == "false") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok = value.(bool)
|
2022-09-20 18:13:45 +00:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
items, isArray := value.([]interface{})
|
|
|
|
|
|
|
|
if !isArray || itemsType == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate each item
|
|
|
|
for _, item := range items {
|
|
|
|
itemType := itemsType.Type
|
|
|
|
underlyingItems := itemsType.Items
|
|
|
|
if !ValidateConfigValue(itemType, underlyingItems, item) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-10-30 22:42:39 +00:00
|
|
|
func configKeyIsNamespacedByProject(projectName string, configKey string) bool {
|
|
|
|
return !strings.Contains(configKey, ":") || strings.HasPrefix(configKey, projectName+":")
|
|
|
|
}
|
|
|
|
|
2018-02-14 21:56:16 +00:00
|
|
|
func (proj *Project) Validate() error {
|
|
|
|
if proj.Name == "" {
|
|
|
|
return errors.New("project is missing a 'name' attribute")
|
2017-08-31 17:21:17 +00:00
|
|
|
}
|
2018-11-01 15:28:11 +00:00
|
|
|
if proj.Runtime.Name() == "" {
|
2018-02-14 21:56:16 +00:00
|
|
|
return errors.New("project is missing a 'runtime' attribute")
|
2017-08-31 17:21:17 +00:00
|
|
|
}
|
2018-06-25 05:47:54 +00:00
|
|
|
|
2022-10-30 22:42:39 +00:00
|
|
|
projectName := proj.Name.String()
|
2022-09-20 18:13:45 +00:00
|
|
|
for configKey, configType := range proj.Config {
|
2022-10-30 22:42:39 +00:00
|
|
|
if configType.Default != nil && configType.Value != nil {
|
2023-01-19 20:22:54 +00:00
|
|
|
return fmt.Errorf("project config '%v' cannot have both a 'default' and 'value' attribute", configKey)
|
2022-10-30 22:42:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
configTypeName := configType.TypeName()
|
|
|
|
|
|
|
|
if configKeyIsNamespacedByProject(projectName, configKey) {
|
|
|
|
// namespaced by project
|
|
|
|
if configType.IsExplicitlyTyped() && configType.TypeName() == arrayTypeName && configType.Items == nil {
|
2023-01-19 20:22:54 +00:00
|
|
|
return fmt.Errorf("The configuration key '%v' declares an array "+
|
2022-10-30 22:42:39 +00:00
|
|
|
"but does not specify the underlying type via the 'items' attribute", configKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// when we have a config _type_ with a schema
|
|
|
|
if configType.IsExplicitlyTyped() && configType.Default != nil {
|
|
|
|
if !ValidateConfigValue(configTypeName, configType.Items, configType.Default) {
|
|
|
|
inferredTypeName := InferFullTypeName(configTypeName, configType.Items)
|
2023-01-19 20:22:54 +00:00
|
|
|
return fmt.Errorf("The default value specified for configuration key '%v' is not of the expected type '%v'",
|
2022-10-30 22:42:39 +00:00
|
|
|
configKey,
|
|
|
|
inferredTypeName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// when not namespaced by project, there shouldn't be a type, only a value
|
|
|
|
if configType.IsExplicitlyTyped() {
|
2023-01-19 20:22:54 +00:00
|
|
|
return fmt.Errorf("Configuration key '%v' is not namespaced by the project and should not define a type",
|
2022-10-30 22:42:39 +00:00
|
|
|
configKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// default values are part of a type schema
|
|
|
|
// when not namespaced by project, there is no type schema, only a value
|
|
|
|
if configType.Default != nil {
|
2023-01-19 20:22:54 +00:00
|
|
|
return fmt.Errorf("Configuration key '%v' is not namespaced by the project and "+
|
2022-10-30 22:42:39 +00:00
|
|
|
"should not define a default value. "+
|
|
|
|
"Did you mean to use the 'value' attribute instead of 'default'?", configKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// when not namespaced by project, there should be a value
|
|
|
|
if configType.Value == nil {
|
2023-01-19 20:22:54 +00:00
|
|
|
return fmt.Errorf("Configuration key '%v' is namespaced and must provide an attribute 'value'", configKey)
|
2022-09-20 18:13:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-31 17:21:17 +00:00
|
|
|
return nil
|
2017-01-13 22:32:10 +00:00
|
|
|
}
|
Overhaul names versus tokens
I was sloppy in my use of names versus tokens in the original AST.
Now that we're actually binding things to concrete symbols, etc., we
need to be more precise. In particular, names are just identifiers
that must be "interpreted" in a given lexical context for them to
make any sense; whereas, tokens stand alone and can be resolved without
context other than the set of imported packages, modules, and overall
module structure. As such, names are much simpler than tokens.
As explained in the comments, tokens.Names are simple identifiers:
Name = [A-Za-z_][A-Za-z0-9_]*
and tokens.QNames are fully qualified identifiers delimited by "/":
QName = [ <Name> "/" ]* <Name>
The legal grammar for a token depends on the subset of symbols that
token is meant to represent. However, the most general case, that
accepts all specializations of tokens, is roughly as follows:
Token = <Name> |
<PackageName>
[ ":" <ModuleName>
[ "/" <ModuleMemberName>
[ "." <Class MemberName> ]
]
]
where:
PackageName = <QName>
ModuleName = <QName>
ModuleMemberName = <Name>
ClassMemberName = <Name>
Please refer to the comments in pkg/tokens/tokens.go for more details.
2017-01-20 01:57:20 +00:00
|
|
|
|
2022-10-09 14:58:33 +00:00
|
|
|
// TrustResourceDependencies returns whether this project's runtime can be trusted to accurately report
|
2019-02-12 22:49:43 +00:00
|
|
|
// dependencies. All languages supported by Pulumi today do this correctly. This option remains useful when bringing
|
|
|
|
// up new Pulumi languages.
|
2018-09-27 22:49:08 +00:00
|
|
|
func (proj *Project) TrustResourceDependencies() bool {
|
2019-02-12 22:49:43 +00:00
|
|
|
return true
|
2018-09-27 22:49:08 +00:00
|
|
|
}
|
|
|
|
|
2018-02-27 23:51:19 +00:00
|
|
|
// Save writes a project definition to a file.
|
2018-02-14 21:56:16 +00:00
|
|
|
func (proj *Project) Save(path string) error {
|
2023-02-15 01:06:56 +00:00
|
|
|
contract.Requiref(path != "", "path", "must not be empty")
|
|
|
|
contract.Requiref(proj != nil, "proj", "must not be nil")
|
2024-01-28 15:29:14 +00:00
|
|
|
|
|
|
|
err := proj.Validate()
|
|
|
|
contract.Requiref(err == nil, "proj", "Validate(): %v", err)
|
|
|
|
|
2020-05-22 22:01:15 +00:00
|
|
|
return save(path, proj, false /*mkDirAll*/)
|
2018-02-14 21:56:16 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 22:33:35 +00:00
|
|
|
type PolicyPackProject struct {
|
|
|
|
// Runtime is a required runtime that executes code.
|
|
|
|
Runtime ProjectRuntimeInfo `json:"runtime" yaml:"runtime"`
|
2020-05-22 22:01:15 +00:00
|
|
|
// Version specifies the version of the policy pack. If set, it will override the
|
|
|
|
// version specified in `package.json` for Node.js policy packs.
|
|
|
|
Version string `json:"version,omitempty" yaml:"version,omitempty"`
|
|
|
|
|
2019-10-09 22:33:35 +00:00
|
|
|
// Main is an optional override for the program's main entry-point location.
|
|
|
|
Main string `json:"main,omitempty" yaml:"main,omitempty"`
|
|
|
|
|
|
|
|
// Description is an optional informational description.
|
|
|
|
Description *string `json:"description,omitempty" yaml:"description,omitempty"`
|
|
|
|
// Author is an optional author that created this project.
|
|
|
|
Author *string `json:"author,omitempty" yaml:"author,omitempty"`
|
|
|
|
// Website is an optional website for additional info about this project.
|
|
|
|
Website *string `json:"website,omitempty" yaml:"website,omitempty"`
|
|
|
|
// License is the optional license governing this project's usage.
|
|
|
|
License *string `json:"license,omitempty" yaml:"license,omitempty"`
|
2022-11-23 20:22:57 +00:00
|
|
|
|
|
|
|
// The original byte representation of the file, used to attempt trivia-preserving edits
|
|
|
|
raw []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (proj PolicyPackProject) RawValue() []byte {
|
|
|
|
return proj.raw
|
2019-10-09 22:33:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (proj *PolicyPackProject) Validate() error {
|
|
|
|
if proj.Runtime.Name() == "" {
|
|
|
|
return errors.New("project is missing a 'runtime' attribute")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-22 22:01:15 +00:00
|
|
|
// Save writes a project definition to a file.
|
|
|
|
func (proj *PolicyPackProject) Save(path string) error {
|
2023-02-15 01:06:56 +00:00
|
|
|
contract.Requiref(path != "", "path", "must not be empty")
|
|
|
|
contract.Requiref(proj != nil, "proj", "must not be nil")
|
2020-05-22 22:01:15 +00:00
|
|
|
contract.Requiref(proj.Validate() == nil, "proj", "Validate()")
|
|
|
|
return save(path, proj, false /*mkDirAll*/)
|
|
|
|
}
|
|
|
|
|
2020-09-14 20:54:26 +00:00
|
|
|
type PluginProject struct {
|
|
|
|
// Runtime is a required runtime that executes code.
|
|
|
|
Runtime ProjectRuntimeInfo `json:"runtime" yaml:"runtime"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (proj *PluginProject) Validate() error {
|
|
|
|
if proj.Runtime.Name() == "" {
|
|
|
|
return errors.New("project is missing a 'runtime' attribute")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-10 01:35:39 +00:00
|
|
|
type Environment struct {
|
|
|
|
envs []string
|
|
|
|
message json.RawMessage
|
|
|
|
node *yaml.Node
|
|
|
|
}
|
|
|
|
|
2023-10-12 16:39:26 +00:00
|
|
|
func NewEnvironment(envs []string) *Environment {
|
|
|
|
return &Environment{envs: envs}
|
|
|
|
}
|
|
|
|
|
2023-11-22 05:04:14 +00:00
|
|
|
func (e *Environment) Definition() []byte {
|
|
|
|
switch {
|
|
|
|
case e == nil:
|
|
|
|
// If there's no environment, return nil.
|
|
|
|
return nil
|
|
|
|
case len(e.envs) != 0:
|
|
|
|
// If the environment was a list of environments, create an anonymous environment and return it.
|
|
|
|
bytes, err := json.Marshal(map[string]any{"imports": e.envs})
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return bytes
|
|
|
|
case e.message != nil:
|
|
|
|
// If the environment was encoded as JSON, return the raw JSON.
|
|
|
|
return e.message
|
|
|
|
case e.node != nil:
|
|
|
|
// Re-encode the YAML and return it.
|
|
|
|
bytes, err := yaml.Marshal(e.node)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return bytes
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Environment) Imports() []string {
|
|
|
|
def, diags, err := eval.LoadYAMLBytes("yaml", e.Definition())
|
|
|
|
if err != nil || len(diags) != 0 || def == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2023-12-19 22:09:38 +00:00
|
|
|
names := fx.ToSlice(fx.Map(fx.IterSlice(def.Imports.GetElements()), func(imp *ast.ImportDecl) string {
|
2023-11-22 05:04:14 +00:00
|
|
|
return imp.Environment.GetValue()
|
|
|
|
}))
|
|
|
|
if len(def.Values.GetEntries()) != 0 {
|
2023-12-19 22:09:38 +00:00
|
|
|
names = append(names, "yaml")
|
2023-11-22 05:04:14 +00:00
|
|
|
}
|
2023-12-19 22:09:38 +00:00
|
|
|
return names
|
2023-11-22 05:04:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Environment) Append(envs ...string) *Environment {
|
|
|
|
switch {
|
|
|
|
case e == nil:
|
|
|
|
// The stack has no environment block. Create one that imports the named environments.
|
|
|
|
return NewEnvironment(envs)
|
|
|
|
case e.message != nil:
|
|
|
|
// The environment definition is inline JSON. Append the named environments to the import list,
|
|
|
|
// creating the list if necessary.
|
|
|
|
var m map[string]any
|
2023-12-19 22:09:38 +00:00
|
|
|
if err := json.Unmarshal(e.message, &m); err == nil {
|
2023-11-22 05:04:14 +00:00
|
|
|
imports, _ := m["imports"].([]any)
|
|
|
|
anys := fx.ToSlice(fx.Map(fx.IterSlice(envs), func(e string) any { return e }))
|
|
|
|
m["imports"] = append(imports, anys...)
|
|
|
|
if new, err := json.Marshal(m); err == nil {
|
2023-12-19 22:09:38 +00:00
|
|
|
e.message = new
|
2023-11-22 05:04:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
case e.node != nil:
|
|
|
|
// The environment definition is inline YAML.
|
|
|
|
// - If there is no import list, add one, then append the named envs to the import list
|
|
|
|
// - If there is an import list, append the named envs to the import list
|
|
|
|
root := e.node
|
|
|
|
if root.Kind == yaml.MappingNode {
|
|
|
|
var imports *yaml.Node
|
|
|
|
for i := 0; i < len(root.Content); i += 2 {
|
|
|
|
key := root.Content[i]
|
|
|
|
if key.Kind == yaml.ScalarNode && key.Value == "imports" {
|
|
|
|
imports = root.Content[i+1]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if imports == nil {
|
|
|
|
root.Content = append([]*yaml.Node{
|
|
|
|
{
|
|
|
|
Kind: yaml.ScalarNode,
|
|
|
|
Style: root.Style,
|
|
|
|
Tag: "!!str",
|
|
|
|
Value: "imports",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Kind: yaml.SequenceNode,
|
|
|
|
Style: root.Style,
|
|
|
|
},
|
|
|
|
}, root.Content...)
|
|
|
|
imports = root.Content[1]
|
|
|
|
}
|
|
|
|
if imports.Kind == yaml.SequenceNode {
|
|
|
|
nodes := fx.ToSlice(fx.Map(fx.IterSlice(envs), func(env string) *yaml.Node {
|
|
|
|
return &yaml.Node{
|
|
|
|
Kind: yaml.ScalarNode,
|
|
|
|
Style: imports.Style,
|
|
|
|
Tag: "!!str",
|
|
|
|
Value: env,
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
imports.Content = append(imports.Content, nodes...)
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
default:
|
|
|
|
// The environment definition is just a list of environments. Append to the list.
|
|
|
|
e.envs = append(e.envs, envs...)
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Environment) Remove(env string) *Environment {
|
|
|
|
switch {
|
|
|
|
case e == nil:
|
|
|
|
// There is no environment block, so there's nothing to remove.
|
|
|
|
return nil
|
|
|
|
case e.message != nil:
|
|
|
|
// The environment definition is inline JSON. Find the last occurrence of the named environment in the import
|
|
|
|
// list and remove it.
|
|
|
|
var m map[string]any
|
2023-12-19 22:09:38 +00:00
|
|
|
if err := json.Unmarshal(e.message, &m); err == nil {
|
2023-11-22 05:04:14 +00:00
|
|
|
if imports, ok := m["imports"].([]any); ok {
|
|
|
|
for i := len(imports) - 1; i >= 0; i-- {
|
|
|
|
match := false
|
|
|
|
switch e := imports[i].(type) {
|
|
|
|
case string:
|
|
|
|
match = e == env
|
|
|
|
case map[string]any:
|
|
|
|
match = len(e) == 1 && maps.Keys(e)[0] == env
|
|
|
|
}
|
|
|
|
if match {
|
|
|
|
m["imports"] = append(imports[:i], imports[i+1:]...)
|
|
|
|
if new, err := json.Marshal(m); err == nil {
|
2023-12-19 22:09:38 +00:00
|
|
|
e.message = new
|
2023-11-22 05:04:14 +00:00
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
case e.node != nil:
|
|
|
|
// The environment definition is inline YAML. Find the last occurrence of the named environment in the import
|
|
|
|
// list and remove it.
|
|
|
|
root := e.node
|
|
|
|
if root.Kind == yaml.MappingNode {
|
|
|
|
for i := 0; i < len(root.Content); i += 2 {
|
|
|
|
key := root.Content[i]
|
|
|
|
if key.Kind == yaml.ScalarNode && key.Value == "imports" {
|
|
|
|
value := root.Content[i+1]
|
|
|
|
if value.Kind == yaml.SequenceNode {
|
|
|
|
for j := len(value.Content) - 1; j >= 0; j-- {
|
|
|
|
n := value.Content[j]
|
|
|
|
|
|
|
|
match := false
|
|
|
|
switch n.Kind {
|
|
|
|
case yaml.ScalarNode:
|
|
|
|
match = n.Value == env
|
|
|
|
case yaml.MappingNode:
|
|
|
|
match = len(n.Content) == 2 && n.Content[0].Value == env
|
turn on the golangci-lint exhaustive linter (#15028)
Turn on the golangci-lint exhaustive linter. This is the first step
towards catching more missing cases during development rather than
in tests, or in production.
This might be best reviewed commit-by-commit, as the first commit turns
on the linter with the `default-signifies-exhaustive: true` option set,
which requires a lot less changes in the current codebase.
I think it's probably worth doing the second commit as well, as that
will get us the real benefits, even though we end up with a little bit
more churn. However it means all the `switch` statements are covered,
which isn't the case after the first commit, since we do have a lot of
`default` statements that just call `assert.Fail`.
Fixes #14601
## Checklist
- [x] I have run `make tidy` to update any new dependencies
- [x] I have run `make lint` to verify my code passes the lint check
- [x] I have formatted my code using `gofumpt`
<!--- 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.
-->
- [ ] 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 Cloud,
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
Cloud API version
<!-- @Pulumi employees: If yes, you must submit corresponding changes in
the service repo. -->
2024-01-17 16:50:41 +00:00
|
|
|
case yaml.SequenceNode, yaml.AliasNode, yaml.DocumentNode:
|
|
|
|
// These nodes never match, so we can ignore them here.
|
2023-11-22 05:04:14 +00:00
|
|
|
}
|
|
|
|
if match {
|
|
|
|
value.Content = append(value.Content[:j], value.Content[j+1:]...)
|
|
|
|
if len(value.Content) == 0 {
|
|
|
|
root.Content = append(root.Content[:i], root.Content[i+2:]...)
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
default:
|
|
|
|
// The environment definition is just a list of environments. Find the last occurrence of the named environment
|
|
|
|
// in the list and remove it.
|
|
|
|
for i := len(e.envs) - 1; i >= 0; i-- {
|
|
|
|
n := e.envs[i]
|
|
|
|
if n == env {
|
|
|
|
e.envs = append(e.envs[:i], e.envs[i+1:]...)
|
|
|
|
if len(e.envs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-10 01:35:39 +00:00
|
|
|
func (e Environment) MarshalJSON() ([]byte, error) {
|
2023-11-22 05:04:14 +00:00
|
|
|
if e.message == nil {
|
|
|
|
return json.Marshal(e.envs)
|
|
|
|
}
|
2023-10-10 01:35:39 +00:00
|
|
|
return json.Marshal(e.message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Environment) UnmarshalJSON(b []byte) error {
|
|
|
|
if err := json.Unmarshal(b, &e.envs); err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return json.Unmarshal(b, &e.message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e Environment) MarshalYAML() (any, error) {
|
2023-10-12 16:39:26 +00:00
|
|
|
if e.node == nil {
|
|
|
|
return e.envs, nil
|
|
|
|
}
|
2023-10-10 01:35:39 +00:00
|
|
|
return e.node, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Environment) UnmarshalYAML(n *yaml.Node) error {
|
|
|
|
if err := n.Decode(&e.envs); err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
e.node = n
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-14 21:56:16 +00:00
|
|
|
// ProjectStack holds stack specific information about a project.
|
|
|
|
type ProjectStack struct {
|
2019-08-02 23:12:16 +00:00
|
|
|
// SecretsProvider is this stack's secrets provider.
|
|
|
|
SecretsProvider string `json:"secretsprovider,omitempty" yaml:"secretsprovider,omitempty"`
|
|
|
|
// EncryptedKey is the KMS-encrypted ciphertext for the data key used for secrets encryption.
|
|
|
|
// Only used for cloud-based secrets providers.
|
|
|
|
EncryptedKey string `json:"encryptedkey,omitempty" yaml:"encryptedkey,omitempty"`
|
|
|
|
// EncryptionSalt is this stack's base64 encoded encryption salt. Only used for
|
|
|
|
// passphrase-based secrets providers.
|
2018-11-01 15:28:11 +00:00
|
|
|
EncryptionSalt string `json:"encryptionsalt,omitempty" yaml:"encryptionsalt,omitempty"`
|
|
|
|
// Config is an optional config bag.
|
|
|
|
Config config.Map `json:"config,omitempty" yaml:"config,omitempty"`
|
2023-10-10 01:35:39 +00:00
|
|
|
// Environment is an optional environment definition or list of environments.
|
|
|
|
Environment *Environment `json:"environment,omitempty" yaml:"environment,omitempty"`
|
2022-11-23 20:22:57 +00:00
|
|
|
|
|
|
|
// The original byte representation of the file, used to attempt trivia-preserving edits
|
|
|
|
raw []byte
|
|
|
|
}
|
|
|
|
|
2023-10-10 01:35:39 +00:00
|
|
|
func (ps ProjectStack) EnvironmentBytes() []byte {
|
2023-11-22 05:04:14 +00:00
|
|
|
return ps.Environment.Definition()
|
2023-10-10 01:35:39 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 20:22:57 +00:00
|
|
|
func (ps ProjectStack) RawValue() []byte {
|
|
|
|
return ps.raw
|
2018-02-14 21:56:16 +00:00
|
|
|
}
|
|
|
|
|
2018-02-27 23:51:19 +00:00
|
|
|
// Save writes a project definition to a file.
|
|
|
|
func (ps *ProjectStack) Save(path string) error {
|
2023-02-15 01:06:56 +00:00
|
|
|
contract.Requiref(path != "", "path", "must not be empty")
|
|
|
|
contract.Requiref(ps != nil, "ps", "must not be nil")
|
2020-05-22 22:01:15 +00:00
|
|
|
return save(path, ps, true /*mkDirAll*/)
|
2018-02-14 21:56:16 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 05:47:54 +00:00
|
|
|
type ProjectRuntimeInfo struct {
|
|
|
|
name string
|
2018-08-06 18:05:44 +00:00
|
|
|
options map[string]interface{}
|
2018-06-25 05:47:54 +00:00
|
|
|
}
|
|
|
|
|
2018-08-06 18:05:44 +00:00
|
|
|
func NewProjectRuntimeInfo(name string, options map[string]interface{}) ProjectRuntimeInfo {
|
2018-06-25 05:47:54 +00:00
|
|
|
return ProjectRuntimeInfo{
|
|
|
|
name: name,
|
|
|
|
options: options,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (info *ProjectRuntimeInfo) Name() string {
|
|
|
|
return info.name
|
|
|
|
}
|
|
|
|
|
2018-08-06 18:05:44 +00:00
|
|
|
func (info *ProjectRuntimeInfo) Options() map[string]interface{} {
|
2018-06-25 05:47:54 +00:00
|
|
|
return info.options
|
|
|
|
}
|
|
|
|
|
2020-05-22 22:01:15 +00:00
|
|
|
func (info *ProjectRuntimeInfo) SetOption(key string, value interface{}) {
|
|
|
|
if info.options == nil {
|
|
|
|
info.options = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
info.options[key] = value
|
|
|
|
}
|
|
|
|
|
2022-10-11 16:12:29 +00:00
|
|
|
func (info ProjectRuntimeInfo) MarshalYAML() (interface{}, error) {
|
2018-06-25 05:47:54 +00:00
|
|
|
if info.options == nil || len(info.options) == 0 {
|
|
|
|
return info.name, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return map[string]interface{}{
|
|
|
|
"name": info.name,
|
|
|
|
"options": info.options,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-10-11 16:12:29 +00:00
|
|
|
func (info ProjectRuntimeInfo) MarshalJSON() ([]byte, error) {
|
2018-06-25 05:47:54 +00:00
|
|
|
if info.options == nil || len(info.options) == 0 {
|
|
|
|
return json.Marshal(info.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(map[string]interface{}{
|
|
|
|
"name": info.name,
|
|
|
|
"options": info.options,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (info *ProjectRuntimeInfo) UnmarshalJSON(data []byte) error {
|
|
|
|
if err := json.Unmarshal(data, &info.name); err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var payload struct {
|
2018-08-06 18:05:44 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Options map[string]interface{} `json:"options"`
|
2018-06-25 05:47:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(data, &payload); err == nil {
|
|
|
|
info.name = payload.Name
|
|
|
|
info.options = payload.Options
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New("runtime section must be a string or an object with name and options attributes")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (info *ProjectRuntimeInfo) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
if err := unmarshal(&info.name); err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var payload struct {
|
2018-08-06 18:05:44 +00:00
|
|
|
Name string `yaml:"name"`
|
|
|
|
Options map[string]interface{} `yaml:"options"`
|
2018-06-25 05:47:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := unmarshal(&payload); err == nil {
|
|
|
|
info.name = payload.Name
|
|
|
|
info.options = payload.Options
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New("runtime section must be a string or an object with name and options attributes")
|
|
|
|
}
|
|
|
|
|
2017-10-17 18:27:54 +00:00
|
|
|
func marshallerForPath(path string) (encoding.Marshaler, error) {
|
|
|
|
ext := filepath.Ext(path)
|
|
|
|
m, has := encoding.Marshalers[ext]
|
|
|
|
if !has {
|
2023-01-19 20:22:54 +00:00
|
|
|
return nil, fmt.Errorf("no marshaler found for file format '%v'", ext)
|
2017-10-17 18:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
2020-05-22 22:01:15 +00:00
|
|
|
|
|
|
|
func save(path string, value interface{}, mkDirAll bool) error {
|
2023-02-15 01:06:56 +00:00
|
|
|
contract.Requiref(path != "", "path", "must not be empty")
|
|
|
|
contract.Requiref(value != nil, "value", "must not be nil")
|
2020-05-22 22:01:15 +00:00
|
|
|
|
|
|
|
m, err := marshallerForPath(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := m.Marshal(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if mkDirAll {
|
2023-03-03 16:36:39 +00:00
|
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
2020-05-22 22:01:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-06 00:07:45 +00:00
|
|
|
//nolint:gosec
|
2023-03-03 16:36:39 +00:00
|
|
|
return os.WriteFile(path, b, 0o644)
|
2020-05-22 22:01:15 +00:00
|
|
|
}
|
2024-01-24 16:47:12 +00:00
|
|
|
|
|
|
|
// To mitigate an import cycle, we define this here.
|
|
|
|
const PulumiTagsConfigKey = "pulumi:tags"
|
|
|
|
|
|
|
|
// AddConfigStackTags sets the project tags config to the given map of tags.
|
|
|
|
func (proj *Project) AddConfigStackTags(tags map[string]string) {
|
|
|
|
if proj.Config == nil {
|
|
|
|
proj.Config = map[string]ProjectConfigType{}
|
|
|
|
}
|
|
|
|
configTags, has := proj.Config["pulumi:tags"]
|
|
|
|
if !has {
|
|
|
|
configTags = ProjectConfigType{
|
|
|
|
Value: map[string]string{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if configTags.Value == nil {
|
|
|
|
configTags.Value = map[string]string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
tagMap, ok := configTags.Value.(map[string]string)
|
|
|
|
if !ok {
|
|
|
|
logging.Warningf("overwriting non-object `%s` project config", "pulumi:tags")
|
|
|
|
tagMap = map[string]string{}
|
|
|
|
}
|
|
|
|
for k, v := range tags {
|
|
|
|
tagMap[k] = v
|
|
|
|
}
|
|
|
|
proj.Config["pulumi:tags"] = configTags
|
|
|
|
}
|