This repository has been archived by the owner on Oct 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (61 loc) · 1.54 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
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
ess "github.com/unixpickle/essentials"
)
const dockerConfigPath = "/kaniko/.docker/config.json"
func main() {
// Read and validate config.
cfg, err := newConfigFromEnv()
if err != nil {
ess.Die("Error: reading config from env:", err)
}
if err = cfg.Validate(); err != nil {
ess.Die("Error: invalid configuration:", err)
}
// Open and rewrite Docker config file.
file, err := os.OpenFile(dockerConfigPath, os.O_RDWR, 0600)
defer file.Close()
if err = cfg.EditDockerConfig(file); err != nil {
ess.Die("Error: editing Docker config:", err)
}
if cfg.PluginDryRun {
if _, err = file.Seek(0, io.SeekStart); err != nil {
ess.Die("Error: seeking file start:", err)
}
data, err := ioutil.ReadAll(file)
if err != nil {
ess.Die("Error: reading file (dry-run):", err)
}
fmt.Fprintf(os.Stderr, "Docker config file contents: '%q'\n", data)
}
if err = file.Close(); err != nil {
ess.Die("Error: closing Docker config file:", err)
}
// Build and execute command.
cmd, err := cfg.Command()
if err != nil {
ess.Die("Error: building Kaniko command:", err)
}
// During dry-run, print command to stderr and exit.
if cfg.PluginDryRun {
fmt.Fprintf(
os.Stderr,
"Should run command '%s' with arguments: %v\n",
cmd.Path, cmd.Args,
)
os.Exit(0)
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err = cmd.Run(); err != nil {
if eerr, ok := err.(*exec.ExitError); ok {
os.Exit(eerr.ExitCode())
}
ess.Die("Error: running Kaniko command:", err)
}
}