-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a "build" command that only constructs YAML files by substituting…
… imports, rather than merging them like the "import" command does
- Loading branch information
1 parent
9b753b5
commit 0f1876d
Showing
13 changed files
with
195 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,9 @@ linters: | |
- dupword | ||
- tagalign | ||
- testpackage | ||
- perfsprint | ||
- testifylint | ||
- mnd | ||
|
||
issues: | ||
exclude-rules: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
### Description: Dockerfile for yamll | ||
FROM alpine:3.16 | ||
FROM alpine:3.20.2 | ||
|
||
COPY yamll / | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
## yamll build | ||
|
||
Builds YAML files substituting imports | ||
|
||
### Synopsis | ||
|
||
Builds YAML by substituting all anchors and aliases defined in sub-YAML files defined as libraries | ||
|
||
``` | ||
yamll build [flags] | ||
``` | ||
|
||
### Examples | ||
|
||
``` | ||
yamll build --file path/to/file.yaml | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
-f, --file stringArray root yaml files to be used for importing | ||
-h, --help help for build | ||
--limiter string limiters to separate the yaml files post merging (default "---") | ||
--log-level string log level for the yamll (default "INFO") | ||
--no-color when enabled the output would not be color encoded | ||
--no-validation when enabled it skips validating the final generated YAML file | ||
--to-file string name of the file to which the final imported yaml should be written to | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [yamll](yamll.md) - A utility to facilitate the inclusion of sub-YAML files as libraries. | ||
|
||
###### Auto generated by spf13/cobra on 27-Jul-2024 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package yamll | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/goccy/go-yaml" | ||
"github.com/nikhilsbhat/yamll/pkg/errors" | ||
) | ||
|
||
func (yamlRoutes YamlRoutes) Build() (Yaml, error) { | ||
anchorRefs := strings.NewReader(yamlRoutes.getRawData()) | ||
|
||
var output []byte | ||
|
||
for _, dependencyRoute := range yamlRoutes { | ||
if !dependencyRoute.Root { | ||
continue | ||
} | ||
|
||
yamlMap := make(Data) | ||
|
||
decodeOpts := []yaml.DecodeOption{ | ||
yaml.UseOrderedMap(), | ||
yaml.Strict(), | ||
yaml.ReferenceReaders(anchorRefs), | ||
} | ||
|
||
encodeOpts := []yaml.EncodeOption{ | ||
yaml.Indent(yamlIndent), | ||
yaml.IndentSequence(true), | ||
yaml.UseLiteralStyleIfMultiline(true), | ||
} | ||
|
||
if err := yaml.UnmarshalWithOptions([]byte(dependencyRoute.DataRaw), &yamlMap, decodeOpts...); err != nil { | ||
return "", &errors.YamllError{Message: fmt.Sprintf("error deserialising YAML file: %v", err)} | ||
} | ||
|
||
yamlOut, err := yaml.MarshalWithOptions(yamlMap, encodeOpts...) | ||
if err != nil { | ||
return "", &errors.YamllError{Message: fmt.Sprintf("serialising YAML file %s errored : %v", dependencyRoute.File, err)} | ||
} | ||
|
||
output = yamlOut | ||
} | ||
|
||
return Yaml(output), nil | ||
} | ||
|
||
func (yamlRoutes YamlRoutes) getRawData() string { | ||
var rawData string | ||
|
||
for _, dependencyRoute := range yamlRoutes { | ||
rawData += fmt.Sprintf("---\n%s\n", dependencyRoute.DataRaw) | ||
} | ||
|
||
return rawData | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,20 +20,25 @@ func Test_getDependencyData2(t *testing.T) { | |
##++https://run.mocky.io/v3/92e08b25-dd1f-4dd0-bc55-9649b5b896c9` | ||
|
||
stringReader := strings.NewReader(absYamlFilePath) | ||
|
||
scanner := bufio.NewScanner(stringReader) | ||
|
||
t.Setenv("USERNAME", "nikhil") | ||
|
||
t.Setenv("PASSWORD", "super-secret-password") | ||
|
||
cfg := New(false, "DEBUG", "") | ||
|
||
cfg.SetLogger() | ||
|
||
dependencies := make([]*Dependency, 0) | ||
|
||
for scanner.Scan() { | ||
line := scanner.Text() | ||
if strings.Contains(line, "##++") { | ||
dependency, err := cfg.getDependencyData(line) | ||
assert.NoError(t, err) | ||
|
||
dependencies = append(dependencies, dependency) | ||
} | ||
} | ||
|
@@ -52,6 +57,7 @@ func TestDependency_ReadData(t *testing.T) { | |
} | ||
|
||
cfg := New(false, "DEBUG", "") | ||
|
||
cfg.SetLogger() | ||
|
||
out, err := dependency.readData(false, cfg.GetLogger()) | ||
|
@@ -82,9 +88,11 @@ func TestConfig_ResolveDependencies2(t *testing.T) { | |
func TestDependency_Git(t *testing.T) { | ||
t.Run("", func(t *testing.T) { | ||
cfg := New(false, "DEBUG", "") | ||
|
||
cfg.SetLogger() | ||
|
||
t.Setenv("GITHUB_TOKEN", "testkey") | ||
|
||
dependency := Dependency{ | ||
// Path: "git+https://github.com/nikhilsbhat/yamll@main?path=internal/fixtures/base.yaml", | ||
Path: "git+ssh://[email protected]:nikhilsbhat/yamll@main?path=internal/fixtures/base.yaml", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters