This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
85 lines (74 loc) · 1.71 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
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"regexp"
"strings"
"github.com/flynn/go-dockerclient"
"github.com/flynn/go-docopt"
)
var imageIDPattern = regexp.MustCompile(`\$image_id\[[^\]]+\]`)
func interpolateManifest(d *docker.Client, src io.Reader, dest io.Writer) error {
manifest, err := ioutil.ReadAll(src)
if err != nil {
return err
}
var replaceErr interface{}
func() {
defer func() {
replaceErr = recover()
}()
manifest = imageIDPattern.ReplaceAllFunc(manifest, func(raw []byte) []byte {
imageName := string(raw[10 : len(raw)-1])
if !strings.Contains(imageName, "/") {
imageName = "flynn/" + imageName
}
image, err := d.InspectImage(imageName)
if err != nil {
panic(fmt.Errorf("Error inspecting %s: %s", imageName, err))
}
return []byte(image.ID)
})
}()
if replaceErr != nil {
return replaceErr.(error)
}
_, err = dest.Write(manifest)
return err
}
func main() {
usage := `flynn-release generates Flynn releases.
Usage:
flynn-release manifest [--output=<dest>] <template>
Options:
-o --output Output destination file ("-" for stdout) [default: -]
`
args, _ := docopt.Parse(usage, nil, true, "", false)
dc, err := docker.NewClient("unix:///var/run/docker.sock")
if err != nil {
log.Fatal(err)
}
var dest io.Writer = os.Stdout
if name := args.String["--output"]; name != "-" && name != "" {
f, err := os.Create(name)
if err != nil {
log.Fatal(err)
}
defer f.Close()
dest = f
}
var src io.Reader = os.Stdin
if name := args.String["<template>"]; name != "-" && name != "" {
f, err := os.Open(name)
if err != nil {
log.Fatal(err)
}
src = f
}
if err := interpolateManifest(dc, src, dest); err != nil {
log.Fatal(err)
}
}