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 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 "" }