Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print to stderr, document inline file contents #49

Merged
merged 2 commits into from Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 12 additions & 7 deletions config/config.go
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/coreos/fcct/config/v1_0"

"github.com/coreos/go-semver/semver"
"github.com/coreos/vcontext/report"
"gopkg.in/yaml.v3"
)

Expand All @@ -42,29 +43,33 @@ func getTranslator(variant string, version semver.Version) (translator, error) {
return t, nil
}

type translator func([]byte, common.TranslateOptions) ([]byte, error)
// translators take a raw config and translate it to a raw Ignition config. The report returned should include any
// errors, warnings, etc and may or may not be fatal. If report is fatal, or other errors are encountered while translating
// translators should return an error.
type translator func([]byte, common.TranslateOptions) ([]byte, report.Report, error)

// Translate wraps all of the actual translate functions in a switch that determines the correct one to call
func Translate(input []byte, options common.TranslateOptions) ([]byte, error) {
// Translate wraps all of the actual translate functions in a switch that determines the correct one to call.
// Translate returns an error if the report had fatal errors or if other errors occured during translation.
func Translate(input []byte, options common.TranslateOptions) ([]byte, report.Report, error) {
lucab marked this conversation as resolved.
Show resolved Hide resolved
// first determine version. This will ignore most fields, so don't use strict
ver := common.Common{}
if err := yaml.Unmarshal(input, &ver); err != nil {
return nil, fmt.Errorf("Error unmarshaling yaml: %v", err)
return nil, report.Report{}, fmt.Errorf("Error unmarshaling yaml: %v", err)
}

if ver.Variant == "" {
return nil, ErrNoVariant
return nil, report.Report{}, ErrNoVariant
}

tmp, err := semver.NewVersion(ver.Version)
if err != nil {
return nil, ErrInvalidVersion
return nil, report.Report{}, ErrInvalidVersion
}
version := *tmp

translator, err := getTranslator(ver.Variant, version)
if err != nil {
return nil, err
return nil, report.Report{}, err
}

return translator(input, options)
Expand Down
19 changes: 10 additions & 9 deletions config/v1_0/fcos.go
Expand Up @@ -16,7 +16,6 @@ package v1_0

import (
"errors"
"fmt"
"reflect"

base_0_1 "github.com/coreos/fcct/base/v0_1"
Expand Down Expand Up @@ -55,12 +54,15 @@ func (c Config) Translate() (types.Config, error) {
return v3_0.Merge(distro, base), nil
}

func TranslateBytes(input []byte, options common.TranslateOptions) ([]byte, error) {
// TranslateBytes translates from a v1.0 fcc to a v3.0.0 Ignition config. It returns a report of any errors or
// warnings in the source and resultant config. If the report has fatal errors or it encounters other problems
// translating, an error is returned.
func TranslateBytes(input []byte, options common.TranslateOptions) ([]byte, report.Report, error) {
lucab marked this conversation as resolved.
Show resolved Hide resolved
cfg := Config{}

contextTree, err := common.Unmarshal(input, &cfg, options.Strict)
if err != nil {
return nil, err
return nil, report.Report{}, err
}

r := validate.Validate(cfg, "yaml")
Expand All @@ -70,24 +72,23 @@ func TranslateBytes(input []byte, options common.TranslateOptions) ([]byte, erro
r.Merge(validate.ValidateCustom(cfg, "yaml", unusedKeyCheck))
r.Correlate(contextTree)
if r.IsFatal() {
fmt.Println(r.String())
return nil, ErrInvalidConfig
return nil, r, ErrInvalidConfig
}

final, err := cfg.Translate()
if err != nil {
return nil, err
return nil, r, err
}

translatedTree := common.ToCamelCase(contextTree)
second := validate.Validate(final, "json")
second.Correlate(translatedTree)
r.Merge(second)
fmt.Println(r.String())

if r.IsFatal() {
return nil, ErrInvalidConfig
return nil, r, ErrInvalidConfig
}

return common.Marshal(final, options.Pretty)
outbytes, err := common.Marshal(final, options.Pretty)
return outbytes, r, err
}
3 changes: 2 additions & 1 deletion docs/configuration-v1_0.md
Expand Up @@ -55,7 +55,8 @@ The Fedora CoreOS configuration is a YAML document conforming to the following s
* **_overwrite_** (boolean): whether to delete preexisting nodes at the path. `source` must be specified if `overwrite` is true. Defaults to false.
* **_contents_** (object): options related to the contents of the file.
* **_compression_** (string): the type of compression used on the contents (null or gzip). Compression cannot be used with S3.
* **_source_** (string): the URL of the file contents. Supported schemes are `http`, `https`, `tftp`, `s3`, and [`data`][rfc2397]. When using `http`, it is advisable to use the verification option to ensure the contents haven't been modified. If source is omitted and a regular file already exists at the path, Ignition will do nothing. If source is omitted and no file exists, an empty file will be created.
* **_source_** (string): the URL of the file contents. Supported schemes are `http`, `https`, `tftp`, `s3`, and [`data`][rfc2397]. When using `http`, it is advisable to use the verification option to ensure the contents haven't been modified. If source is omitted and a regular file already exists at the path, Ignition will do nothing. If source is omitted and no file exists, an empty file will be created. Mutually exclusive with `inline`.
* **_inline_** (string): the contents of the file. Mutually exclusive with `source`.
* **_verification_** (object): options related to the verification of the file contents.
* **_hash_** (string): the hash of the config, in the form `<type>-<value>` where type is `sha512`.
* **_append_** (list of objects): list of contents to be appended to the file. Follows the same stucture as `contents`
Expand Down
3 changes: 2 additions & 1 deletion internal/main.go
Expand Up @@ -66,7 +66,8 @@ func main() {
fail("failed to read %s: %v\n", infile.Name(), err)
}

dataOut, err := config.Translate(dataIn, options)
dataOut, r, err := config.Translate(dataIn, options)
fmt.Fprintf(os.Stderr, "%s", r.String())
if err != nil {
fail("Error translating config: %v\n", err)
}
Expand Down