Make config set via args or ENV-Vars (#11)

close #1

Reviewed-on: #11
Co-authored-by: 6543 <6543@obermui.de>
Co-committed-by: 6543 <6543@obermui.de>
This commit is contained in:
6543 2021-05-22 11:49:45 +00:00
parent e6c684b99c
commit 7db6bbf357

View File

@ -74,30 +74,103 @@ var CmdMerge = cli.Command{
Usage: "run script for merge subcommand",
Description: `run script for merge subcommand`,
Action: runMerge,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "token",
Usage: "GitLab Access Token",
EnvVars: []string{"TOKEN"},
Required: false,
},
&cli.StringFlag{
Name: "url",
Usage: "GitLab Base URL",
EnvVars: []string{"BASE_URL"},
Required: false,
},
&cli.IntFlag{
Name: "group-id",
Usage: "Group ID where repositories are stored",
EnvVars: []string{"GROUP_ID"},
Required: false,
},
&cli.StringFlag{
Name: "group-name",
Usage: "Group Name where repositories are stored, it's used if group-id is not set",
EnvVars: []string{"GROUP_NAME"},
Required: false,
},
&cli.StringFlag{
Name: "repo-ignore-pattern",
Usage: "Repo Ignore Pattern",
EnvVars: []string{"REPO_IGNORE_PATTERN"},
Required: false,
},

&cli.StringFlag{
Name: "branch-pattern",
Usage: "Merge Branches with patter",
EnvVars: []string{"BRANCH_PATTERN"},
Required: false,
},
},
}

// runMerge run script for merge subcommand
func runMerge(ctx *cli.Context) error {
values := loadCache()
interactive := false

// ask for infos
if err := survey.AskOne(&survey.Input{Message: "GitLab Base URL:", Default: values.baseURL}, &values.baseURL); err != nil {
return err
}
if ctx.IsSet("token") {
values.token = ctx.String("token")
} else {
interactive = true
if err := survey.AskOne(&survey.Input{Message: "GitLab Token:", Default: values.token}, &values.token); err != nil {
return err
}
}

if ctx.IsSet("url") {
values.baseURL = ctx.String("url")
} else {
interactive = true
if err := survey.AskOne(&survey.Input{Message: "GitLab Base URL:", Default: values.baseURL}, &values.baseURL); err != nil {
return err
}
}

orgID := ctx.Int("group-id")
if orgID == 0 {
if ctx.IsSet("group-name") {
values.orgName = ctx.String("group-name")
} else {
interactive = true
if err := survey.AskOne(&survey.Input{Message: "Group Name:", Default: values.orgName}, &values.orgName); err != nil {
return err
}
}
}

if ctx.IsSet("repo-ignore-pattern") {
values.repoExclude = ctx.String("repo-ignore-pattern")
} else {
interactive = true
if err := survey.AskOne(&survey.Input{Message: "Ignore Repo with patter:", Default: values.repoExclude}, &values.repoExclude); err != nil {
return err
}
}

if ctx.IsSet("branch-pattern") {
values.pullBaseBranch = ctx.String("branch-pattern")
} else {
interactive = true
if err := survey.AskOne(&survey.Input{Message: "Merge Branches with patter:", Default: values.pullBaseBranch}, &values.pullBaseBranch); err != nil {
return err
}
}

if interactive {
saveCache(values)
}

// Compile glob regex
excludeRule, err := glob.Compile(values.repoExclude)
@ -115,6 +188,7 @@ func runMerge(ctx *cli.Context) error {
return fmt.Errorf("Could not create Client: %v\n", err)
}

if orgID == 0 {
// discover GroupID by name
org, _, err := client.Groups.GetGroup(values.orgName)
if err != nil {
@ -122,12 +196,15 @@ func runMerge(ctx *cli.Context) error {
}
fmt.Printf("Found \"%s\"\n", org.WebURL)

orgID = org.ID
}

var repoList []*gitlab.Project
var page = 1
fmt.Printf("Retrieving repository list...\n")
for {
fmt.Printf("%d", page)
repos, _, err := client.Groups.ListGroupProjects(org.ID, &gitlab.ListGroupProjectsOptions{
repos, _, err := client.Groups.ListGroupProjects(orgID, &gitlab.ListGroupProjectsOptions{
ListOptions: gitlab.ListOptions{PerPage: 10, Page: page},
Archived: utils.OptBool(false),
IncludeSubgroups: utils.OptBool(false),