Skip to content

Commit

Permalink
merge for releasing v1.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mandelsoft committed Jul 23, 2020
2 parents 30bdddb + 7ba0a83 commit d41381a
Show file tree
Hide file tree
Showing 111 changed files with 13,222 additions and 87 deletions.
4 changes: 2 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
# unused-packages = true


[[constraint]]
name = "github.com/cloudfoundry-incubator/candiedyaml"
revision = "a41693b7b7afb422c7ecb1028458ab27da047bbb"
#[[constraint]]
# name = "github.com/cloudfoundry-incubator/candiedyaml"
# revision = "a41693b7b7afb422c7ecb1028458ab27da047bbb"

[[constraint]]
name = "github.com/spf13/cobra"
Expand All @@ -54,7 +54,7 @@
go-tests = true
unused-packages = true

[[prune.project]]
name = "github.com/cloudfoundry-incubator/candiedyaml"
go-tests = false
unused-packages = false
# [[prune.project]]
# name = "github.com/cloudfoundry-incubator/candiedyaml"
# go-tests = false
# unused-packages = false
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ spiff_darwin_amd64.zip: ensure
ensure:
dep ensure
# restore patched version of candiedyaml/decode.go
git checkout -- vendor/github.com/cloudfoundry-incubator/candiedyaml/decode.go
#git checkout -- vendor/github.com/cloudfoundry-incubator/candiedyaml/decode.go
#git checkout -- vendor/github.com/cloudfoundry-incubator/candiedyaml/emitter.go
102 changes: 95 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ Contents:
- [(( contains(list, "foobar") ))](#-containslist-foobar-)
- [(( index(list, "foobar") ))](#-indexlist-foobar-)
- [(( lastindex(list, "foobar") ))](#-lastindexlist-foobar-)
- [(( basename(path) ))](#-basenamepath-)
- [(( dirname(path) ))](#-dirnamepath-)
- [(( parseurl("http://github.com") ))](#-parseurlhttpgithubcom-)
- [(( sort(list) ))](#-sortlist-)
- [(( replace(string, "foo", "bar") ))](#-replacestring-foo-bar-)
- [(( substr(string, 1, 3) ))](#-substrstring-1-3-)
Expand Down Expand Up @@ -303,6 +306,13 @@ value of a map or an entry in a list. The expression might span multiple
lines. In any case the yaml string value *must not* end with a newline
(for example using `|-`)

If a parenthesized value should not be interpreted as an *dynaml* expression and
kept as it is in the output, it can be escaped by an exclamation mark directly
after the openeing brackets.

For example, `((! .field ))` maps to the string value `(( .field ))` and
`((!! .field ))` maps to the string value `((! .field ))`.

The following is a complete list of dynaml expressions:


Expand Down Expand Up @@ -703,11 +713,6 @@ If the corresponding value is not defined, it will return nil. This then has the
same semantics as reference expressions; a nil merge is an unresolved template.
See [`||`](#-a--b-).

**Note**: Instead of using a `<<:` insert field to place merge expressions it is
possible now to use `<<<:`, also, which allows to use regular yaml parsers for
spiff-like yaml documents. `<<:` is kept for backward compatibility.


### `<<: (( merge ))`

Merging of maps or lists with the content of the same element found in some stub.
Expand All @@ -720,8 +725,18 @@ require content in at least one stub (as always for the merge operator). Now thi
is evaluated correctly, but this would break existing manifest template sets, which use the
first variant, but mean the second. Therfore this case is explicitly handled to describe an
optional merge. If really a required merge is meant an additional explicit qualifier has to

**Note**: Instead of using a `<<:` insert field to place merge expressions it is
possible now to use `<<<:`, also, which allows to use regular yaml parsers for
spiff-like yaml documents. `<<:` is kept for backward compatibility.
be used (`(( merge required ))`).

If the merge key should not be interpreted as regular key instead of a merge
directive, it can be escaped by an excalamtion mark (`!`).

For example, a map key `<<<!` will result in a string key `<<<` and `<<<!!`
will result in a string key `<<<!`

#### Merging maps

**values.yml**
Expand Down Expand Up @@ -1465,6 +1480,75 @@ contains: (( contains("foobar", "bar") ))

yields `true`.

### `(( basename(path) ))`

The function `basename` returns the name of the last element of a path.
The argument may either be a regular path name or a URL.

e.g.:

```yaml
pathbase: (( basename("alice/bob") ))
urlbase: (( basename("http://foobar/alice/bob?any=parameter") ))
```

yields:

```yaml
pathbase: bob
urlbase: bob
```

### `(( dirname(path) ))`

The function `dirname` returns the parent directory of a path.
The argument may either be a regular path name or a URL.

e.g.:

```yaml
pathbase: (( dirname("alice/bob") ))
urlbase: (( dirname("http://foobar/alice/bob?any=parameter") ))
```

yields:

```yaml
pathbase: alice
urlbase: /alice
```

### `(( parseurl("http://github.com") ))`

This function parses a URL and yield a map with all elements of an URL.
The fields `port`, `userinfo`and `password` are optional.

e.g.:

```yaml
url: (( parseurl("https://user:[email protected]:443/mandelsoft/spiff?branch=master&tag=v1#anchor") ))
```

yields:

```yaml
url:
scheme: https
host: github.com
port: 443
path: /mandelsoft/spiff
fragment: anchor
query: branch=master&tag=v1
values:
branch: [ master ]
tag: [ v1 ]
userinfo:
username: user
password: pass
```



### `(( index(list, "foobar") ))`

Checks whether a list contains a dedicated value and returns the index of the first match.
Expand Down Expand Up @@ -3133,8 +3217,12 @@ public: |+
```

To generate an ssh public key an optional additional format argument can be set
to `ssh`. The result will then be a regular publc key format usable for ssh.
The default format is `pem` providinf the pem output format shown above.
to `ssh`. The result will then be a regular public key format usable for ssh.
The default format is `pem` providing the pem output format shown above.

RSA keys are by default marshalled in PKCS#1 format(`RSA PUBLIC KEY`) in pem.
If the the generic *PKIX* format (`PUBLIC KEY`) is required the format
argument `pkix` must be given.

Using the format `ssh` this function can also be used to convert a pem formatted
public key into an ssh key,
Expand Down
2 changes: 1 addition & 1 deletion cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

"github.com/spf13/cobra"

"github.com/cloudfoundry-incubator/candiedyaml"
"github.com/mandelsoft/spiff/compare"
"github.com/mandelsoft/spiff/legacy/candiedyaml"
"github.com/mandelsoft/spiff/yaml"
)

Expand Down
17 changes: 10 additions & 7 deletions cmd/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import (
"path"
"strings"

"github.com/cloudfoundry-incubator/candiedyaml"
"github.com/spf13/cobra"

"github.com/mandelsoft/spiff/debug"
"github.com/mandelsoft/spiff/dynaml"
"github.com/mandelsoft/spiff/flow"
"github.com/mandelsoft/spiff/legacy/candiedyaml"
"github.com/mandelsoft/spiff/yaml"
"github.com/spf13/cobra"
)

var asJSON bool
Expand All @@ -37,7 +38,7 @@ var mergeCmd = &cobra.Command{
return nil
},
Run: func(cmd *cobra.Command, args []string) {
merge(args[0], partial, asJSON, split, outputPath, selection, state, args[1:])
merge(false, args[0], partial, asJSON, split, outputPath, selection, state, nil, args[1:])
},
}

Expand Down Expand Up @@ -67,11 +68,10 @@ func fileExists(filename string) bool {
return !info.IsDir()
}

func merge(templateFilePath string, partial bool, json, split bool,
subpath string, selection []string, stateFilePath string, stubFilePaths []string) {
func merge(stdin bool, templateFilePath string, partial bool, json, split bool,
subpath string, selection []string, stateFilePath string, stubs []yaml.Node, stubFilePaths []string) {
var templateFile []byte
var err error
var stdin = false

if templateFilePath == "-" {
templateFile, err = ioutil.ReadAll(os.Stdin)
Expand Down Expand Up @@ -100,7 +100,10 @@ func merge(templateFilePath string, partial bool, json, split bool,
}
}

stubs := []yaml.Node{}
if stubs == nil {
stubs = []yaml.Node{}
}
stubs = append(stubs[:0:0], stubs...)

for _, stubFilePath := range stubFilePaths {
var stubFile []byte
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var cfgFile string
var rootCmd = &cobra.Command{
Use: "spiff",
Short: "YAML in-domain templating processor",
Version: "v1.4.1-dev",
Version: "v1.5.0",
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand Down
64 changes: 64 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package cmd

import (
"errors"
"fmt"
"github.com/mandelsoft/spiff/debug"
"github.com/mandelsoft/spiff/yaml"
"github.com/spf13/cobra"
"io/ioutil"
"log"
"os"
"path"
)

// runCmd represents the merge command
var processCmd = &cobra.Command{
Use: "process",
Aliases: []string{"r"},
Short: "Process a template with merged stubs on a document",
Long: `Merge a bunch of template files into one manifest processing a document input, printing it out.`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("requires at least two args (document and template)")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
run(args[0], args[1], partial, asJSON, split, outputPath, selection, state, args[2:])
},
}

func init() {
rootCmd.AddCommand(processCmd)

processCmd.Flags().BoolVar(&asJSON, "json", false, "print output in json format")
processCmd.Flags().BoolVar(&debug.DebugFlag, "debug", false, "Print state info")
processCmd.Flags().BoolVar(&partial, "partial", false, "Allow partial evaluation only")
processCmd.Flags().StringVar(&outputPath, "path", "", "output is taken from given path")
processCmd.Flags().StringVar(&state, "state", "", "select state file to maintain")
processCmd.Flags().StringArrayVar(&selection, "select", []string{}, "filter dedicated output fields")
}

func run(documentFilePath, templateFilePath string, partial bool, json, split bool,
subpath string, selection []string, stateFilePath string, stubFilePaths []string) {
var err error
var stdin = false
var documentFile []byte

if documentFilePath == "-" {
documentFile, err = ioutil.ReadAll(os.Stdin)
stdin = true
} else {
documentFile, err = ReadFile(documentFilePath)
}

documentYAML, err := yaml.Parse(documentFilePath, documentFile)
if err != nil {
log.Fatalln(fmt.Sprintf("error parsing template [%s]:", path.Clean(documentFilePath)), err)
}

documentYAML = yaml.NewNode(map[string]yaml.Node{"document": documentYAML}, "<"+documentFilePath+">")
stub := yaml.NewNode(map[string]yaml.Node{"document": yaml.NewNode("(( &temporary &inject (merge) ))", "<document>)")}, "<document>")
merge(stdin, templateFilePath, partial, json, split, subpath, selection, stateFilePath, []yaml.Node{stub, documentYAML}, stubFilePaths)
}
Loading

0 comments on commit d41381a

Please sign in to comment.