Skip to content

Commit

Permalink
cmd/otk: New otk external: osbuild-make-ostree-source
Browse files Browse the repository at this point in the history
A new command for otk that creates the org.osbuild.ostree source object
based on the resolved ostree commit data generated from the
osbuild-resolve-ostree-commit command.
  • Loading branch information
achilleas-k authored and mvo5 committed Oct 11, 2024
1 parent ebe5fd3 commit 66f21b1
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions cmd/otk/osbuild-make-ostree-source/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package main

import (
"encoding/json"
"fmt"
"io"
"os"

"github.com/osbuild/images/pkg/osbuild"
"github.com/osbuild/images/pkg/ostree"
)

// TODO: move structs to common package with resolver external

type Input struct {
Tree InputTree `json:"tree"`
}

type InputTree struct {
Const InputConst `json:"const"`
}

type InputConst struct {
// Ref of the commit (can be empty).
Ref string `json:"ref,omitempty"`

// URL of the repo where the commit can be fetched.
URL string `json:"url"`

// Secrets type to use when pulling the ostree commit content
// (e.g. org.osbuild.rhsm.consumer).
Secrets string `json:"secrets,omitempty"`

// Checksum of the commit.
Checksum string `json:"checksum"`
}

type Output struct {
OSTreeSource osbuild.OSTreeSource `json:"org.osbuild.ostree"`
}

func run(r io.Reader, w io.Writer) error {
var inp Input
if err := json.NewDecoder(r).Decode(&inp); err != nil {
return err
}

ostreeSource := osbuild.NewOSTreeSource()
ostreeSource.AddItem(ostree.CommitSpec{
Ref: inp.Tree.Const.Ref,
URL: inp.Tree.Const.URL,
Secrets: inp.Tree.Const.Secrets,
Checksum: inp.Tree.Const.Checksum,
})

output := Output{
OSTreeSource: *ostreeSource,
}
out := map[string]interface{}{
"tree": output,
}
outputJson, err := json.MarshalIndent(out, "", " ")
if err != nil {
return fmt.Errorf("cannot marshal response: %w", err)
}
fmt.Fprintf(w, "%s\n", outputJson)
return nil
}

func main() {
if err := run(os.Stdin, os.Stdout); err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err.Error())
os.Exit(1)
}
}

0 comments on commit 66f21b1

Please sign in to comment.