-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
173 lines (163 loc) · 4.35 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"runtime"
"strings"
)
var url string
var image string
var registry string
var repository string
var user string
var password string
func init() {
flag.StringVar(&url, "url", "", "url of file you want download")
flag.StringVar(&image, "image", "", "full adress of your image you want transfer")
flag.StringVar(®istry, "registry", "docker.io", "your registry to save your image")
flag.StringVar(&repository, "repository", "heishui", "your repository to save your image")
flag.StringVar(&user, "user", "heishui", "user of your repository")
flag.StringVar(&password, "password", "", "password of your repository")
}
func main() {
// file := GetFile("https://golang.google.cn/dl/go1.15.windows-amd64.msi")
// fmt.Println(file)
flag.Parse()
fmt.Println(runtime.GOOS)
// fmt.Println(image)
if image != "" {
cmd := exec.Command("docker", "pull", image)
stdoutStderr, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", stdoutStderr)
myImage := GetImage(image)
fmt.Println(myImage)
cmd = exec.Command("docker", "tag", image, myImage)
stdoutStderr, err = cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", stdoutStderr)
cmd = exec.Command("docker", "login", "--username="+user, "--password="+password)
fmt.Println(cmd.String())
stdoutStderr, err = cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", stdoutStderr)
cmd = exec.Command("docker", "push", myImage)
fmt.Println(cmd.String())
stdoutStderr, err = cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", stdoutStderr)
}
if url != "" {
cmd := exec.Command("wget", url)
// cmd := exec.Command("ls")
stdoutStderr, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", stdoutStderr)
file := GetFile(url)
fmt.Println(file)
f, err := os.Create("Dockerfile")
ct := "from scratch\ncopy " + file + " /"
defer func() {
if err = f.Close(); err != nil {
log.Fatal(err)
}
}()
l, err := f.WriteString(ct)
fmt.Println(l, " bytes written successfully!")
newFullImage := registry + "/" + repository + "/" + strings.ToLower(file)
cmd = exec.Command("docker", "build", "-t", newFullImage, ".")
stdoutStderr, err = cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", stdoutStderr)
cmd = exec.Command("docker", "login", "--username="+user, "--password="+password)
fmt.Println(cmd.String())
stdoutStderr, err = cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}
cmd = exec.Command("docker", "push", newFullImage)
stdoutStderr, err = cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", stdoutStderr)
Inspect(newFullImage)
}
}
// GetImage ... comment.
func GetImage(name string) string {
imageArry := strings.Split(name, "/")
newImage := imageArry[len(imageArry)-1]
newFullImage := registry + "/" + repository + "/" + newImage
return newFullImage
}
// GetFile ... fun.
func GetFile(url string) string {
FileArry := strings.Split(url, "/")
file := FileArry[len(FileArry)-1]
// fmt.Println(file)
return file
}
// Inspect comm.
func Inspect(item string) {
CheckJq()
fmt.Println(
"docker pull", item,
// "docker inspect", item, "| jq .[0].GraphDriver.Data.UpperDir",
)
fmt.Println(
// "docker pull item",
"docker inspect", item, "| jq .[0].GraphDriver.Data.UpperDir",
)
}
// CheckJq command.
func CheckJq() {
cmd := exec.Command("which", "jq")
stdoutStderr, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
// InstallPackage("jq")
}
fmt.Printf("%s\n", stdoutStderr)
}
// InstallPackage in linux.
func InstallPackage(name string) {
fmt.Println(runtime.GOOS)
fmt.Println(runtime.GOARCH)
if path, err := exec.LookPath("yum"); err == nil {
fmt.Println(path)
cmd := exec.Command("yum", "install", "-y", name)
stdoutStderr, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", stdoutStderr)
} else if path, err := exec.LookPath("apt"); err == nil {
fmt.Println(path)
cmd := exec.Command("apt", "update")
cmd.CombinedOutput()
cmd = exec.Command("apt", "install", "-y", name)
stdoutStderr, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", stdoutStderr)
} else {
fmt.Println("not support operation system")
}
}