-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
126 lines (114 loc) · 3.2 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
//
// godot
// https://github.com/pmalmgren/godot
//
// Copyright © 2018 Peter Malmgren <me@petermalmgren.com>
// Distributed under the MIT License.
// See README.md for details.
//
package main
import (
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
"path/filepath"
"github.com/docker/docker/client"
"github.com/pmalmgren/godot/conf"
"github.com/pmalmgren/godot/image"
"github.com/urfave/cli"
)
const (
dockerVersion = "1.39"
)
// builds the docker image, this function does a ton of setup with temporary directories
func buildDockerimage(gdc *conf.GoDotConfig) error {
tmpDir, err := ioutil.TempDir("/tmp/", "godot-build-context")
if err != nil {
return fmt.Errorf("Error creating temporary directory: %v", err)
}
defer func() {
if err := os.RemoveAll(tmpDir); err != nil {
log.Printf("Error removing temporary directory: %v", err)
}
}()
// gdc.DockerfileRendered contains the contents of conf/dockerfile_template.go
dockerfilePath := fmt.Sprintf("%s/Dockerfile", tmpDir)
dotfileDirectory := fmt.Sprintf("%s/%s", gdc.RepoDirectory, gdc.DotfileDirectory)
err = ioutil.WriteFile(dockerfilePath, []byte(gdc.DockerfileRendered), 0666)
if err != nil {
return fmt.Errorf("Error writing to Docker build context file: %v", err)
}
context, err := image.BuildDockerContext(dockerfilePath, dotfileDirectory)
if err != nil {
return fmt.Errorf("Error creating Docker build context tarfile: %v", err)
}
defer func() {
if err := os.RemoveAll(filepath.Dir(context)); err != nil {
log.Printf("Error removing temporary build context: %v", err)
}
}()
// Initialize the Docker CLI client.
cli, err := client.NewClientWithOpts(client.WithVersion(dockerVersion))
if err != nil {
return fmt.Errorf("Error initializing Docker client: %v", err)
}
err = image.BuildDockerImage(cli, context, gdc.ImageTag)
if err != nil {
return fmt.Errorf("Error building Docker image: %v", err)
}
return nil
}
// godot builds and runs the docker image
func godot(u *url.URL) error {
tmpDir, err := ioutil.TempDir("", "/tmp")
defer func() {
if err := os.RemoveAll(tmpDir); err != nil {
log.Printf("Error removing temporary Git repo: %v", err)
}
}()
if err != nil {
return fmt.Errorf("Error creating temporary directory: %v", err)
}
repo := conf.Repository{Remote: u, RepoDirectory: tmpDir}
if err := repo.Pull(); err != nil {
return fmt.Errorf("Error reading from Git repository: %v", err)
}
gdc, err := conf.ConfigFromReadme(&repo)
if err != nil {
return fmt.Errorf("Error parsing README.md configuration: %v", err)
}
err = buildDockerimage(gdc)
if err != nil {
return fmt.Errorf("Error building Docker Image: %v", err)
}
return nil
}
func main() {
app := cli.NewApp()
app.Name = "godot"
app.Usage = "godot build your-repo"
app.Version = "0.0.1"
app.Commands = []cli.Command{
{
Name: "build",
Aliases: []string{"b"},
Action: func(ctx *cli.Context) error {
repoStr := ctx.Args().Get(len(ctx.Args()) - 1)
u, err := url.Parse(repoStr)
if err != nil {
return fmt.Errorf("Error parsing repository: %v", err)
}
if err := godot(u); err != nil {
return fmt.Errorf("Error: %v", err)
}
return nil
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}