feat(容器命令): 添加goenv可执行程序,执行命令直接进入容器环境

This commit is contained in:
xiaoqidun 2020-09-24 01:11:18 +08:00
parent 3a9de5543b
commit 29e8ec4a2e
3 changed files with 103 additions and 1 deletions

View File

@ -1,6 +1,39 @@
---
kind: pipeline
type: docker
name: default
name: goenv
steps:
- name: build
pull: if-not-exists
image: ghcr.io/xiaoqidun/build
commands:
- build -f build.sh -i goenv.go -o release/goenv
- name: build
pull: if-not-exists
image: xiaoqidun/goenv
commands:
- ./build.sh
- name: upload
pull: if-not-exists
image: plugins/s3
settings:
bucket:
from_secret: minio_bucket
access_key:
from_secret: minio_access_key
secret_key:
from_secret: minio_secret_key
source: release/*
strip_prefix: release/
target: product/goenv
path_style: true
endpoint: https://minio.aite.xyz
---
kind: pipeline
type: docker
name: docker
steps:
- name: build

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module github.com/xiaoqidun/goenv
go 1.15

66
goenv.go Normal file
View File

@ -0,0 +1,66 @@
package main
import (
"flag"
"fmt"
"os"
"os/exec"
)
var (
author bool
)
func init() {
flag.BoolVar(&author, "author", false, "关于本软件开发者")
flag.Parse()
}
func main() {
if author {
Author()
return
}
d := &DockerGoEnv{
Image: "xiaoqidun/goenv",
Binary: "docker",
WorkDir: "/go/src/app",
AutoDelete: true,
MountWorkDir: true,
}
d.Run()
}
func Author() {
fmt.Println("welcome to our website https://aite.xyz/")
fmt.Println("----------------------------------------")
fmt.Println("腾讯扣扣88966001")
fmt.Println("电子邮箱xiaoqidun@gmail.com")
}
type DockerGoEnv struct {
Image string
Binary string
CmdArgs []string
WorkDir string
AutoDelete bool
MountWorkDir bool
}
func (d *DockerGoEnv) Run() {
d.CmdArgs = []string{"run", "-it"}
if d.AutoDelete {
d.CmdArgs = append(d.CmdArgs, "--rm")
}
if d.MountWorkDir {
if pwd, err1 := os.Getwd(); err1 == nil {
d.CmdArgs = append(d.CmdArgs, "-v", pwd+":"+d.WorkDir, "-w", d.WorkDir)
}
}
d.CmdArgs = append(d.CmdArgs, d.Image)
cmd := exec.Command(d.Binary, d.CmdArgs...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
_ = cmd.Run()
}