2018-05-22 19:43:36 +00:00
|
|
|
// Copyright 2016-2018, Pulumi Corporation.
|
|
|
|
//
|
|
|
|
// 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.
|
2016-11-16 02:26:21 +00:00
|
|
|
|
2016-11-16 17:29:44 +00:00
|
|
|
package encoding
|
2016-11-16 02:26:21 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-02-22 02:31:43 +00:00
|
|
|
"path/filepath"
|
2016-11-16 02:26:21 +00:00
|
|
|
|
2017-10-20 20:21:04 +00:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
2016-11-16 02:26:21 +00:00
|
|
|
)
|
|
|
|
|
2016-12-02 23:00:44 +00: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 18:41:07 +00:00
|
|
|
// Exts contains a list of all the valid marshalable extension types.
|
|
|
|
var Exts = []string{
|
2016-12-02 23:00:44 +00: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 18:41:07 +00:00
|
|
|
// Although ".yml" is not a sanctioned YAML extension, it is used quite broadly; so we will support it.
|
|
|
|
".yml",
|
|
|
|
}
|
|
|
|
|
2017-02-22 02:31:43 +00: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 02:23:32 +00:00
|
|
|
ext = DefaultExt() // default to the first (preferred) marshaler.
|
2017-02-22 02:31:43 +00:00
|
|
|
}
|
|
|
|
return Marshalers[ext], ext
|
|
|
|
}
|
|
|
|
|
2016-11-16 16:19:26 +00: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 02:23:32 +00: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 02:26:21 +00:00
|
|
|
// Marshaler is a type that knows how to marshal and unmarshal data in one format.
|
|
|
|
type Marshaler interface {
|
2016-12-02 23:00:44 +00:00
|
|
|
IsJSONLike() bool
|
|
|
|
IsYAMLLike() bool
|
2016-11-16 02:26:21 +00:00
|
|
|
Marshal(v interface{}) ([]byte, error)
|
|
|
|
Unmarshal(data []byte, v interface{}) error
|
|
|
|
}
|
|
|
|
|
2016-11-25 20:58:29 +00:00
|
|
|
var JSON Marshaler = &jsonMarshaler{}
|
|
|
|
|
2016-11-16 02:26:21 +00:00
|
|
|
type jsonMarshaler struct {
|
|
|
|
}
|
|
|
|
|
2016-12-02 23:00:44 +00:00
|
|
|
func (m *jsonMarshaler) IsJSONLike() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *jsonMarshaler) IsYAMLLike() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-11-16 02:26:21 +00:00
|
|
|
func (m *jsonMarshaler) Marshal(v interface{}) ([]byte, error) {
|
2017-02-22 02:31:43 +00:00
|
|
|
return json.MarshalIndent(v, "", " ")
|
2016-11-16 02:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *jsonMarshaler) Unmarshal(data []byte, v interface{}) error {
|
2017-06-06 01:11:51 +00:00
|
|
|
// IDEA: use a "strict" marshaler, so that we can warn on unrecognized keys (avoiding silly mistakes). We should
|
2016-11-19 02:12:26 +00:00
|
|
|
// set aside an officially sanctioned area in the metadata for extensibility by 3rd parties.
|
2016-11-16 02:26:21 +00:00
|
|
|
return json.Unmarshal(data, v)
|
|
|
|
}
|
|
|
|
|
2016-11-25 20:58:29 +00:00
|
|
|
var YAML Marshaler = &yamlMarshaler{}
|
|
|
|
|
2016-11-16 02:26:21 +00:00
|
|
|
type yamlMarshaler struct {
|
|
|
|
}
|
|
|
|
|
2016-12-02 23:00:44 +00:00
|
|
|
func (m *yamlMarshaler) IsJSONLike() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *yamlMarshaler) IsYAMLLike() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-11-16 02:26:21 +00: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 01:11:51 +00:00
|
|
|
// IDEA: use a "strict" marshaler, so that we can warn on unrecognized keys (avoiding silly mistakes). We should
|
2016-11-19 02:12:26 +00:00
|
|
|
// set aside an officially sanctioned area in the metadata for extensibility by 3rd parties.
|
2017-10-17 18:38:03 +00:00
|
|
|
|
2016-11-16 02:26:21 +00:00
|
|
|
return yaml.Unmarshal(data, v)
|
|
|
|
}
|