Skip to content

Commit

Permalink
init and first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
alanpeng committed Aug 21, 2020
0 parents commit b3fe8c4
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
115 changes: 115 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -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(&registry, "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
}
14 changes: 14 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit b3fe8c4

Please sign in to comment.