From b3fe8c49788467bc04aee395e8aa542a1e880ca3 Mon Sep 17 00:00:00 2001 From: Alan Peng Date: Fri, 21 Aug 2020 16:21:14 +0800 Subject: [PATCH] init and first commit --- README.md | 5 +++ main.go | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++ main_test.go | 14 +++++++ 3 files changed, 134 insertions(+) create mode 100644 README.md create mode 100644 main.go create mode 100644 main_test.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..5fa76d9 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +download file or transfer docker image from [katacode](https://katacoda.com/courses/ubuntu/playground) or machine abroad + +# useage: +go get githu.com/xilu0/transfer +transfer --uerser=heishui --password="" --usr=https://golang.google.cn/dl/go1.15.windows-amd64.msi --image=vmware/photon:1.0 diff --git a/main.go b/main.go new file mode 100644 index 0000000..1f00b4d --- /dev/null +++ b/main.go @@ -0,0 +1,115 @@ +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) + } +} +func GetImage(name string) string { + newImage := strings.Split(name, "/")[2] + newFullImage := registry + "/" + repository + "/" + newImage + return newFullImage +} + +func GetFile(url string) string { + FileArry := strings.Split(url, "/") + file := FileArry[len(FileArry)-1] + // fmt.Println(file) + return file +} diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..0767495 --- /dev/null +++ b/main_test.go @@ -0,0 +1,14 @@ +package main + +import ( + "testing" +) + +// func TestGetFile(t *testing.T) { +// file := GetFile("https://golang.google.cn/dl/go1.15.windows-amd64.msi") +// fmt.Println(file) +// } + +func TestWriteDockerfile(t *testing.T) { + WriteDockerfile() +}