...
 
Commits (2)
    https://gitcode.net/xuxiaowei-com-cn/gitlab-go/-/commit/a81e49d159ed620b1329be96c4b9ec253bc9169c :sparkles: 导出: 自动跳过已存在的文件夹 2023-12-17T21:48:28+08:00 徐晓伟 xuxiaowei@xuxiaowei.com.cn https://gitcode.net/xuxiaowei-com-cn/gitlab-go/-/commit/8b69e6a11173360c48c8f20f6635d6c66a24a59e :sparkles: 导出: 允许失败 2023-12-17T21:58:25+08:00 徐晓伟 xuxiaowei@xuxiaowei.com.cn
......@@ -582,6 +582,8 @@ COPYRIGHT:
--export-folder value 导出文件夹
--skip-project-path value [ --skip-project-path value ] 跳过项目路径
--skip-project-wiki-path value [ --skip-project-wiki-path value ] 跳过项目wiki路径
--auto-skip-exist-folder 自动跳过已存在的文件夹 (default: false)
--allow-failure 允许失败 (default: false)
--help, -h show help
```
......
......@@ -29,4 +29,6 @@ const (
ExportFolder = "export-folder"
SkipProjectPath = "skip-project-path"
SkipProjectWikiPath = "skip-project-wiki-path"
AutoSkipExistFolder = "auto-skip-exist-folder"
AllowFailure = "allow-failure"
)
......@@ -233,3 +233,19 @@ func SkipProjectWikiPath() cli.Flag {
Usage: "跳过项目wiki路径",
}
}
func AutoSkipExistFolder() cli.Flag {
return &cli.BoolFlag{
Name: constant.AutoSkipExistFolder,
Usage: "自动跳过已存在的文件夹",
Value: false,
}
}
func AllowFailure() cli.Flag {
return &cli.BoolFlag{
Name: constant.AllowFailure,
Usage: "允许失败",
Value: false,
}
}
......@@ -24,7 +24,8 @@ func ExportAll() *cli.Command {
"1. git 仓库\n" +
"2. wiki 仓库",
Flags: append(flag.CommonTokenRequired(), flag.Owned(true),
flag.ExportFolder(true), flag.SkipProjectPath(), flag.SkipProjectWikiPath()),
flag.ExportFolder(true), flag.SkipProjectPath(), flag.SkipProjectWikiPath(),
flag.AutoSkipExistFolder(), flag.AllowFailure()),
Action: func(context *cli.Context) error {
var baseUrl = context.String(constant.BaseUrl)
......@@ -33,6 +34,8 @@ func ExportAll() *cli.Command {
var exportFolder = context.String(constant.ExportFolder)
var skipProjectPaths = context.StringSlice(constant.SkipProjectPath)
var skipProjectWikiPaths = context.StringSlice(constant.SkipProjectWikiPath)
var autoSkipExistFolder = context.Bool(constant.AutoSkipExistFolder)
var allowFailure = context.Bool(constant.AllowFailure)
baseURL, err := url.Parse(baseUrl)
if err != nil {
......@@ -61,7 +64,7 @@ func ExportAll() *cli.Command {
for index, project := range projectList {
log.Printf("Project Index: %d, WebURL: %s", index, project.WebURL)
err = Repository(exportFolder, host, token, project, skipProjectPaths)
err = Repository(allowFailure, autoSkipExistFolder, exportFolder, host, token, project, skipProjectPaths)
if err != nil {
return err
}
......@@ -70,7 +73,7 @@ func ExportAll() *cli.Command {
for index, project := range projectList {
log.Printf("Project wiki Index: %d, WebURL: %s", index, project.WebURL)
err = Wiki(exportFolder, host, token, project, skipProjectWikiPaths)
err = Wiki(allowFailure, autoSkipExistFolder, exportFolder, host, token, project, skipProjectWikiPaths)
if err != nil {
return err
}
......@@ -83,7 +86,7 @@ func ExportAll() *cli.Command {
}
}
func Repository(exportFolder string, host string, token string, project *gitlab.Project, skipProjectPaths []string) error {
func Repository(allowFailure bool, autoSkipExistFolder bool, exportFolder string, host string, token string, project *gitlab.Project, skipProjectPaths []string) error {
c := contains(skipProjectPaths, project.PathWithNamespace)
if c {
......@@ -92,6 +95,16 @@ func Repository(exportFolder string, host string, token string, project *gitlab.
}
gitPath := filepath.Join(exportFolder, "repository", host, project.PathWithNamespace)
if autoSkipExistFolder {
_, err := os.Stat(gitPath)
if os.IsNotExist(err) {
} else {
log.Printf("已启用自动跳过已存在的文件夹:%s\n", gitPath)
return nil
}
}
err := os.MkdirAll(gitPath, os.ModePerm)
if err != nil {
return err
......@@ -112,18 +125,24 @@ func Repository(exportFolder string, host string, token string, project *gitlab.
err = cmd.Run()
if err != nil {
if allowFailure {
return nil
}
return err
}
err = GitRemoteSetUrl(gitPath, HTTPURLToRepo)
if err != nil {
if allowFailure {
return nil
}
return err
}
return nil
}
func Wiki(exportFolder string, host string, token string, project *gitlab.Project, skipProjectWikiPaths []string) error {
func Wiki(allowFailure bool, autoSkipExistFolder bool, exportFolder string, host string, token string, project *gitlab.Project, skipProjectWikiPaths []string) error {
c := contains(skipProjectWikiPaths, project.PathWithNamespace)
if c {
......@@ -132,6 +151,16 @@ func Wiki(exportFolder string, host string, token string, project *gitlab.Projec
}
wikiPath := filepath.Join(exportFolder, "wiki", host, project.PathWithNamespace+".wiki")
if autoSkipExistFolder {
_, err := os.Stat(wikiPath)
if os.IsNotExist(err) {
} else {
log.Printf("已启用自动跳过已存在的文件夹:%s\n", wikiPath)
return nil
}
}
err := os.MkdirAll(wikiPath, os.ModePerm)
if err != nil {
return err
......@@ -158,11 +187,17 @@ func Wiki(exportFolder string, host string, token string, project *gitlab.Projec
err = cmd.Run()
if err != nil {
if allowFailure {
return nil
}
return err
}
err = GitRemoteSetUrl(wikiPath, HTTPURLToRepo)
if err != nil {
if allowFailure {
return nil
}
return err
}
......
......@@ -11,7 +11,8 @@ func Export() *cli.Command {
Name: "mix-export",
Usage: "导出(混合命令,多接口命令)",
Flags: append(flag.Common(), flag.Owned(false),
flag.ExportFolder(false), flag.SkipProjectPath(), flag.SkipProjectWikiPath()),
flag.ExportFolder(false), flag.SkipProjectPath(), flag.SkipProjectWikiPath(),
flag.AutoSkipExistFolder(), flag.AllowFailure()),
Subcommands: []*cli.Command{
ExportAll(),
},
......