osbuild: move result serialization from common

In the same way `osbuild.Manifest` is the input to the osbuild API,
`osbuild.Result` is the output. Move it to the `osbuild` package where
it belongs.

This is not a functional change.

Signed-off-by: Tom Gundersen <teg@jklm.no>
This commit is contained in:
Tom Gundersen 2020-08-25 21:59:39 +01:00 committed by Ondřej Budai
parent 360bf2aa04
commit ac5f69e757
7 changed files with 23 additions and 18 deletions

View file

@ -1,84 +0,0 @@
package common
import (
"encoding/json"
"fmt"
"io"
)
type assembler struct {
Name string `json:"name"`
Options json.RawMessage `json:"options"`
Success bool `json:"success"`
Output string `json:"output"`
}
type stage struct {
Name string `json:"name"`
Options json.RawMessage `json:"options"`
Success bool `json:"success"`
Output string `json:"output"`
}
type build struct {
Stages []stage `json:"stages"`
TreeID string `json:"tree_id"`
Success bool `json:"success"`
}
type ComposeResult struct {
TreeID string `json:"tree_id"`
OutputID string `json:"output_id"`
Build *build `json:"build"`
Stages []stage `json:"stages"`
Assembler *assembler `json:"assembler"`
Success bool `json:"success"`
}
func (cr *ComposeResult) Write(writer io.Writer) error {
if cr.Build == nil && len(cr.Stages) == 0 && cr.Assembler == nil {
fmt.Fprintf(writer, "The compose result is empty.\n")
}
if cr.Build != nil {
fmt.Fprintf(writer, "Build pipeline:\n")
for _, stage := range cr.Build.Stages {
fmt.Fprintf(writer, "Stage %s\n", stage.Name)
enc := json.NewEncoder(writer)
enc.SetIndent("", " ")
err := enc.Encode(stage.Options)
if err != nil {
return err
}
fmt.Fprintf(writer, "\nOutput:\n%s\n", stage.Output)
}
}
if len(cr.Stages) > 0 {
fmt.Fprintf(writer, "Stages:\n")
for _, stage := range cr.Stages {
fmt.Fprintf(writer, "Stage: %s\n", stage.Name)
enc := json.NewEncoder(writer)
enc.SetIndent("", " ")
err := enc.Encode(stage.Options)
if err != nil {
return err
}
fmt.Fprintf(writer, "\nOutput:\n%s\n", stage.Output)
}
}
if cr.Assembler != nil {
fmt.Fprintf(writer, "Assembler %s:\n", cr.Assembler.Name)
enc := json.NewEncoder(writer)
enc.SetIndent("", " ")
err := enc.Encode(cr.Assembler.Options)
if err != nil {
return err
}
fmt.Fprintf(writer, "\nOutput:\n%s\n", cr.Assembler.Output)
}
return nil
}

View file

@ -1,80 +0,0 @@
package common
import (
"bytes"
"github.com/stretchr/testify/assert"
"testing"
)
func TestWriteFull(t *testing.T) {
const testOptions = `{"msg": "test"}`
testStage := stage{
Name: "testStage",
Options: []byte(testOptions),
Success: true,
Output: "Finished",
}
testBuild := build{
Stages: []stage{testStage},
TreeID: "treeID",
Success: true,
}
testAssembler := assembler{
Name: "testAssembler",
Options: []byte(testOptions),
Success: true,
Output: "Done",
}
testComposeResult := ComposeResult{
TreeID: "TreeID",
OutputID: "OutputID",
Build: &testBuild,
Stages: []stage{testStage},
Assembler: &testAssembler,
Success: true,
}
var b bytes.Buffer
assert.NoError(t, testComposeResult.Write(&b))
expectedMessage :=
`Build pipeline:
Stage testStage
{
"msg": "test"
}
Output:
Finished
Stages:
Stage: testStage
{
"msg": "test"
}
Output:
Finished
Assembler testAssembler:
{
"msg": "test"
}
Output:
Done
`
assert.Equal(t, expectedMessage, b.String())
}
func TestWriteEmpty(t *testing.T) {
testComposeResult := ComposeResult{}
var b bytes.Buffer
assert.NoError(t, testComposeResult.Write(&b))
assert.Equal(t, "The compose result is empty.\n", b.String())
}