func GetImage(strLocalTag string) (strImage string, strTag string) { var strName string nPos := strings.Index(strLocalTag, "/") strName = common.SubstrAfter(strLocalTag, nPos) nPos = strings.Index(strName, ":") strImage = common.SubstrBefore(strName, nPos) strTag = common.SubstrAfter(strName, nPos) return strImage, strTag }
func createDockerfile(template string, codePath string) (strcodePathPrev string, strLocalFile string, strRemoteFile string) { dockerfile_template, _ := common.Config().String("image", "dockerfile_template") dockerfile, _ := common.Config().String("image", "dockerfile") datetime := time.Now().Format("2006-01-02") strLocalFile = dockerfile + "/" + datetime + "/" + template + "/Dockerfile" pos := strings.LastIndex(codePath, "/") strcodePathPrev = common.SubstrBefore(codePath, pos) if "" == strcodePathPrev { strcodePathPrev = "/" } relativePath := "./" + common.SubstrAfter(codePath, pos) addContent := "\n" + "ADD " + relativePath + " /data/" + template + "_code" + "\n" //读取模版,生成目标Dockerfile文件 templateContent := common.ReadFile(dockerfile_template + "/" + template + "/Dockerfile") newContent := addNewContent(templateContent, "EXPOSE,CMD", addContent) createFile(strLocalFile, newContent) strRemoteFile = strcodePathPrev + "/Dockerfile" return strcodePathPrev, strLocalFile, strRemoteFile }
//创建文件并写入内容,如文件已存在,覆盖旧文件 func createFile(filePath, strData string) (code int, result string) { if strData == "" { return 1, "content不能为空" } pos := strings.LastIndex(filePath, "/") fileDirectory := common.SubstrBefore(filePath, pos) if !common.IsDirExists(fileDirectory) { err := os.MkdirAll(fileDirectory, os.ModePerm) //生成多级目录 if err != nil { fmt.Println("创建目录("+fileDirectory+")失败:", err) return 1, "创建目录(" + fileDirectory + ")失败" } } if !common.SaveFile(filePath, strData) { return 1, "创建文件失败" } return 0, "创建文件成功" }
func addNewContent(oldContent, addFlag, addContent string) string { if oldContent == "" || addFlag == "" || addContent == "" { fmt.Println("oldContent、addFlag、addContent不能为空") return "" } for _, f := range strings.Split(addFlag, ",") { if f == "" { continue } pos := strings.Index(oldContent, f) if pos == -1 { continue } return common.SubstrBefore(oldContent, pos) + addContent + common.SubstrAfter(oldContent, pos-1) } return "" }