2018-03-21 20:43:21 +01:00
|
|
|
// Copyright 2016-2018, Pulumi Corporation. All rights reserved.
|
2016-11-16 03:26:21 +01:00
|
|
|
|
2016-11-16 18:29:44 +01:00
|
|
|
package encoding
|
2016-11-16 03:26:21 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-02-22 03:31:43 +01:00
|
|
|
"path/filepath"
|
2016-11-16 03:26:21 +01:00
|
|
|
|
2017-10-20 22:21:04 +02:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
2016-11-16 03:26:21 +01:00
|
|
|
)
|
|
|
|
|
2016-12-03 00:00:44 +01:00
|
|
|
var JSONExt = ".json"
|
|
|
|
var YAMLExt = ".yaml"
|
|
|
|
|
Support Workspaces
This change adds support for Workspaces, a convenient way of sharing settings
among many Stacks, like default cluster targets, configuration settings, and the
like, which are not meant to be distributed as part of the Stack itself.
The following things are included in this checkin:
* At workspace initialization time, detect and parse the .mu/workspace.yaml
file. This is pretty rudimentary right now and contains just the default
cluster targets. The results are stored in a new ast.Workspace type.
* Rename "target" to "cluster". This impacts many things, including ast.Target
being changed to ast.Cluster, and all related fields, the command line --target
being changed to --cluster, various internal helper functions, and so on. This
helps to reinforce the desired mental model.
* Eliminate the ast.Metadata type. Instead, the metadata moves directly onto
the Stack. This reflects the decision to make Stacks "the thing" that is
distributed, versioned, and is the granularity of dependency.
* During cluster targeting, add the workspace settings into the probing logic.
We still search in the same order: CLI > Stack > Workspace.
2016-11-22 19:41:07 +01:00
|
|
|
// Exts contains a list of all the valid marshalable extension types.
|
|
|
|
var Exts = []string{
|
2016-12-03 00:00:44 +01:00
|
|
|
JSONExt,
|
|
|
|
YAMLExt,
|
Support Workspaces
This change adds support for Workspaces, a convenient way of sharing settings
among many Stacks, like default cluster targets, configuration settings, and the
like, which are not meant to be distributed as part of the Stack itself.
The following things are included in this checkin:
* At workspace initialization time, detect and parse the .mu/workspace.yaml
file. This is pretty rudimentary right now and contains just the default
cluster targets. The results are stored in a new ast.Workspace type.
* Rename "target" to "cluster". This impacts many things, including ast.Target
being changed to ast.Cluster, and all related fields, the command line --target
being changed to --cluster, various internal helper functions, and so on. This
helps to reinforce the desired mental model.
* Eliminate the ast.Metadata type. Instead, the metadata moves directly onto
the Stack. This reflects the decision to make Stacks "the thing" that is
distributed, versioned, and is the granularity of dependency.
* During cluster targeting, add the workspace settings into the probing logic.
We still search in the same order: CLI > Stack > Workspace.
2016-11-22 19:41:07 +01:00
|
|
|
// Although ".yml" is not a sanctioned YAML extension, it is used quite broadly; so we will support it.
|
|
|
|
".yml",
|
|
|
|
}
|
|
|
|
|
2017-02-22 03:31:43 +01:00
|
|
|
// Detect auto-detects a marshaler for the given path.
|
|
|
|
func Detect(path string) (Marshaler, string) {
|
|
|
|
ext := filepath.Ext(path)
|
|
|
|
if ext == "" {
|
Make major commands more pleasant
This change eliminates the need to constantly type in the environment
name when performing major commands like configuration, planning, and
deployment. It's probably due to my age, however, I keep fat-fingering
simple commands in front of investors and I am embarrassed!
In the new model, there is a notion of a "current environment", and
I have modeled it kinda sorta just like Git's notion of "current branch."
By default, the current environment is set when you `init` something.
Otherwise, there is the `coco env select <env>` command to change it.
(Running this command w/out a new <env> will show you the current one.)
The major commands `config`, `plan`, `deploy`, and `destroy` will prefer
to use the current environment, unless it is overridden by using the
--env flag. All of the `coco env <cmd> <env>` commands still require the
explicit passing of an environment which seems reasonable since they are,
after all, about manipulating environments.
As part of this, I've overhauled the aging workspace settings cruft,
which had fallen into disrepair since the initial prototype.
2017-03-22 03:23:32 +01:00
|
|
|
ext = DefaultExt() // default to the first (preferred) marshaler.
|
2017-02-22 03:31:43 +01:00
|
|
|
}
|
|
|
|
return Marshalers[ext], ext
|
|
|
|
}
|
|
|
|
|
2016-11-16 17:19:26 +01:00
|
|
|
// Marshalers is a map of extension to a Marshaler object for that extension.
|
|
|
|
var Marshalers map[string]Marshaler
|
|
|
|
|
Make major commands more pleasant
This change eliminates the need to constantly type in the environment
name when performing major commands like configuration, planning, and
deployment. It's probably due to my age, however, I keep fat-fingering
simple commands in front of investors and I am embarrassed!
In the new model, there is a notion of a "current environment", and
I have modeled it kinda sorta just like Git's notion of "current branch."
By default, the current environment is set when you `init` something.
Otherwise, there is the `coco env select <env>` command to change it.
(Running this command w/out a new <env> will show you the current one.)
The major commands `config`, `plan`, `deploy`, and `destroy` will prefer
to use the current environment, unless it is overridden by using the
--env flag. All of the `coco env <cmd> <env>` commands still require the
explicit passing of an environment which seems reasonable since they are,
after all, about manipulating environments.
As part of this, I've overhauled the aging workspace settings cruft,
which had fallen into disrepair since the initial prototype.
2017-03-22 03:23:32 +01:00
|
|
|
// Default returns the default marshaler object.
|
|
|
|
func Default() Marshaler {
|
|
|
|
return Marshalers[DefaultExt()]
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultExt returns the default extension to use.
|
|
|
|
func DefaultExt() string {
|
|
|
|
return Exts[0]
|
|
|
|
}
|
|
|
|
|
2016-11-16 03:26:21 +01:00
|
|
|
// Marshaler is a type that knows how to marshal and unmarshal data in one format.
|
|
|
|
type Marshaler interface {
|
2016-12-03 00:00:44 +01:00
|
|
|
IsJSONLike() bool
|
|
|
|
IsYAMLLike() bool
|
2016-11-16 03:26:21 +01:00
|
|
|
Marshal(v interface{}) ([]byte, error)
|
|
|
|
Unmarshal(data []byte, v interface{}) error
|
|
|
|
}
|
|
|
|
|
2016-11-25 21:58:29 +01:00
|
|
|
var JSON Marshaler = &jsonMarshaler{}
|
|
|
|
|
2016-11-16 03:26:21 +01:00
|
|
|
type jsonMarshaler struct {
|
|
|
|
}
|
|
|
|
|
2016-12-03 00:00:44 +01:00
|
|
|
func (m *jsonMarshaler) IsJSONLike() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *jsonMarshaler) IsYAMLLike() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-11-16 03:26:21 +01:00
|
|
|
func (m *jsonMarshaler) Marshal(v interface{}) ([]byte, error) {
|
2017-02-22 03:31:43 +01:00
|
|
|
return json.MarshalIndent(v, "", " ")
|
2016-11-16 03:26:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *jsonMarshaler) Unmarshal(data []byte, v interface{}) error {
|
2017-06-06 03:11:51 +02:00
|
|
|
// IDEA: use a "strict" marshaler, so that we can warn on unrecognized keys (avoiding silly mistakes). We should
|
2016-11-19 03:12:26 +01:00
|
|
|
// set aside an officially sanctioned area in the metadata for extensibility by 3rd parties.
|
2016-11-16 03:26:21 +01:00
|
|
|
return json.Unmarshal(data, v)
|
|
|
|
}
|
|
|
|
|
2016-11-25 21:58:29 +01:00
|
|
|
var YAML Marshaler = &yamlMarshaler{}
|
|
|
|
|
2016-11-16 03:26:21 +01:00
|
|
|
type yamlMarshaler struct {
|
|
|
|
}
|
|
|
|
|
2016-12-03 00:00:44 +01:00
|
|
|
func (m *yamlMarshaler) IsJSONLike() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *yamlMarshaler) IsYAMLLike() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-11-16 03:26:21 +01:00
|
|
|
func (m *yamlMarshaler) Marshal(v interface{}) ([]byte, error) {
|
|
|
|
return yaml.Marshal(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *yamlMarshaler) Unmarshal(data []byte, v interface{}) error {
|
2017-06-06 03:11:51 +02:00
|
|
|
// IDEA: use a "strict" marshaler, so that we can warn on unrecognized keys (avoiding silly mistakes). We should
|
2016-11-19 03:12:26 +01:00
|
|
|
// set aside an officially sanctioned area in the metadata for extensibility by 3rd parties.
|
2017-10-17 20:38:03 +02:00
|
|
|
|
2016-11-16 03:26:21 +01:00
|
|
|
return yaml.Unmarshal(data, v)
|
|
|
|
}
|