This repository has been archived by the owner on May 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
134 lines (120 loc) · 3.1 KB
/
main.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"bytes"
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"os/signal"
"path"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/imjasonh/diy/pkg"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v3"
)
func main() {
root := &cobra.Command{
Use: "diy",
Short: "DIY is a tool to declaratively build container images.",
RunE: func(cmd *cobra.Command, _ []string) error { return cmd.Usage() },
}
root.AddCommand(
build(),
resolve(),
)
if err := root.Execute(); err != nil {
os.Exit(1)
}
}
func build() *cobra.Command {
var fn string
var verbose bool
build := &cobra.Command{
Use: "build",
Short: "Build and push an image",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
repo := os.Getenv("DIY_REPO")
if repo == "" {
return errors.New("must set DIY_REPO env var")
}
// Parse the config YAML.
b, err := ioutil.ReadFile(fn)
if err != nil {
return err
}
var cfg pkg.Config
dec := yaml.NewDecoder(bytes.NewReader(b))
dec.KnownFields(true)
if err := dec.Decode(&cfg); err != nil {
return err
}
dstref, err := name.ParseReference(path.Join(repo, cfg.Name))
if err != nil {
return err
}
// Resolve the config and build the image.
if err := pkg.Resolve(ctx, &cfg, verbose); err != nil {
return err
}
img, err := pkg.Build(ctx, cfg, verbose)
if err != nil {
return err
}
// Push the image.
if err := remote.Write(dstref, img, remote.WithAuthFromKeychain(authn.DefaultKeychain)); err != nil {
return err
}
d, err := img.Digest()
if err != nil {
return err
}
fmt.Println(dstref.Context().Digest(d.String()))
return nil
},
}
build.Flags().StringVarP(&fn, "filename", "f", "config.yaml", "Config file describing the image")
build.Flags().BoolVarP(&verbose, "verbose", "v", false, "If true, verbosely log")
return build
}
func resolve() *cobra.Command {
var fn string
var verbose bool
resolve := &cobra.Command{
Use: "resolve",
Short: "Resolve mutable references in a config file",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
// Parse the config YAML.
b, err := ioutil.ReadFile(fn)
if err != nil {
return err
}
var cfg pkg.Config
dec := yaml.NewDecoder(bytes.NewReader(b))
dec.KnownFields(true)
if err := dec.Decode(&cfg); err != nil {
return err
}
// Resolve the config.
if err := pkg.Resolve(ctx, &cfg, verbose); err != nil {
return err
}
enc := yaml.NewEncoder(os.Stdout)
enc.SetIndent(2) // God's one true YAML indentation level.
if err := enc.Encode(cfg); err != nil {
return err
}
return enc.Close()
},
}
resolve.Flags().StringVarP(&fn, "filename", "f", "config.yaml", "Config file describing the image")
return resolve
}