forked from cnabio/cnab-to-oci
/
fixup.go
70 lines (64 loc) · 1.66 KB
/
fixup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"github.com/containerd/containerd/remotes/docker"
"github.com/deislabs/duffle/pkg/bundle"
"github.com/docker/cli/cli/config"
"github.com/docker/cnab-to-oci/remotes"
"github.com/docker/distribution/reference"
"github.com/spf13/cobra"
)
type fixupOptions struct {
input string
output string
targetRef string
}
func fixupCmd() *cobra.Command {
var opts fixupOptions
cmd := &cobra.Command{
Use: "fixup <bundle file> [options]",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.input = args[0]
return runFixup(opts)
},
}
cmd.Flags().StringVarP(&opts.output, "output", "o", "fixed-bundle.json", "specify the output file")
cmd.Flags().StringVarP(&opts.targetRef, "target", "t", "", "reference where the bundle will be pushed")
return cmd
}
func createResolver() docker.ResolverBlobMounter {
cfg := config.LoadDefaultConfigFile(os.Stderr)
return remotes.CreateResolver(cfg, false)
}
func runFixup(opts fixupOptions) error {
var b bundle.Bundle
bundleJSON, err := ioutil.ReadFile(opts.input)
if err != nil {
return err
}
if err := json.Unmarshal(bundleJSON, &b); err != nil {
return err
}
resolver := createResolver()
ref, err := reference.ParseNormalizedNamed(opts.targetRef)
if err != nil {
return err
}
if err := remotes.FixupBundle(context.Background(), &b, ref, resolver); err != nil {
return err
}
bundleJSON, err = json.MarshalIndent(b, "", " ")
if err != nil {
return err
}
if opts.output == "-" {
fmt.Fprintln(os.Stdout, string(bundleJSON))
return nil
}
return ioutil.WriteFile(opts.output, bundleJSON, 0644)
}