introduce context

This commit is contained in:
6543 2021-05-22 14:06:39 +02:00
parent afa8b71a41
commit 65f74e1516
2 changed files with 67 additions and 61 deletions

47
context/config.go Normal file
View File

@ -0,0 +1,47 @@
package context

import (
"encoding/json"
"fmt"
"io/ioutil"
)

type Cache struct {
Token string `json:"Token"`
BaseURL string `json:"BaseURL"`
OrgName string `json:"OrgName"`
RepoExclude string `json:"RepoExclude"`
PullBaseBranch string `json:"PullBaseBranch"`
}

func LoadCache() (cache *Cache) {
// default values
cache = &Cache{
Token: "XXXXXXXXXXXXXXXXXX",
BaseURL: "https://gitlab.com",
OrgName: "organisation",
RepoExclude: "exclude*",
PullBaseBranch: "devel*",
}

data, err := ioutil.ReadFile(".cache.json")
if err != nil {
return
}
var c Cache
if err := json.Unmarshal(data, &c); err != nil {
return
}
return &c
}

func SaveCache(cache *Cache) {
data, err := json.Marshal(cache)
if err != nil {
fmt.Printf("ERROR: marshal cache: %v\n", err)
}
err = ioutil.WriteFile(".cache.json", data, 0600)
if err != nil {
fmt.Printf("ERROR: save cache: %v\n", err)
}
}

View File

@ -1,9 +1,8 @@
package merge

import (
"encoding/json"
"code.obermui.de/6543/GitLab_MergeDevel2Default/context"
"fmt"
"io/ioutil"
"time"

"code.obermui.de/6543/GitLab_MergeDevel2Default/utils"
@ -18,14 +17,6 @@ var (
Version = "dev"
)

type Cache struct {
token string `json:"token"`
baseURL string `json:"baseURL"`
orgName string `json:"orgName"`
repoExclude string `json:"repoExclude"`
pullBaseBranch string `json:"pullBaseBranch"`
}

type pullCache struct {
repoID int
iid int
@ -37,38 +28,6 @@ const (
waitSecForMerge = 3
)

func loadCache() (cache *Cache) {
// default values
cache = &Cache{
token: "XXXXXXXXXXXXXXXXXX",
baseURL: "https://gitlab.com",
orgName: "organisation",
repoExclude: "exclude*",
pullBaseBranch: "devel*",
}

data, err := ioutil.ReadFile(".cache.json")
if err != nil {
return
}
var c Cache
if err := json.Unmarshal(data, &c); err != nil {
return
}
return &c
}

func saveCache(cache *Cache) {
data, err := json.Marshal(cache)
if err != nil {
fmt.Printf("ERROR: marshal cache: %v\n", err)
}
err = ioutil.WriteFile(".cache.json", data, 0600)
if err != nil {
fmt.Printf("ERROR: save cache: %v\n", err)
}
}

var CmdMerge = cli.Command{
Name: "merge",
Usage: "run script for merge subcommand",
@ -117,23 +76,23 @@ var CmdMerge = cli.Command{

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

if ctx.IsSet("token") {
values.token = ctx.String("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 {
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")
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 {
if err := survey.AskOne(&survey.Input{Message: "GitLab Base URL:", Default: values.BaseURL}, &values.BaseURL); err != nil {
return err
}
}
@ -141,58 +100,58 @@ func runMerge(ctx *cli.Context) error {
orgID := ctx.Int("group-id")
if orgID == 0 {
if ctx.IsSet("group-name") {
values.orgName = ctx.String("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 {
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")
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 {
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")
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 {
if err := survey.AskOne(&survey.Input{Message: "Merge Branches with patter:", Default: values.PullBaseBranch}, &values.PullBaseBranch); err != nil {
return err
}
}

if interactive {
saveCache(values)
context.SaveCache(values)
}

// Compile glob regex
excludeRule, err := glob.Compile(values.repoExclude)
excludeRule, err := glob.Compile(values.RepoExclude)
if err != nil {
return fmt.Errorf("not able to compile regex '%s'", values.repoExclude)
return fmt.Errorf("not able to compile regex '%s'", values.RepoExclude)
}
baseBranchRule, err := glob.Compile(values.pullBaseBranch)
baseBranchRule, err := glob.Compile(values.PullBaseBranch)
if err != nil {
return fmt.Errorf("not able to compile regex '%s'", values.pullBaseBranch)
return fmt.Errorf("not able to compile regex '%s'", values.PullBaseBranch)
}

// connect to gitlab
client, err := gitlab.NewClient(values.token, gitlab.WithBaseURL(values.baseURL))
client, err := gitlab.NewClient(values.Token, gitlab.WithBaseURL(values.BaseURL))
if err != nil {
return fmt.Errorf("Could not create Client: %v\n", err)
}

if orgID == 0 {
// discover GroupID by name
org, _, err := client.Groups.GetGroup(values.orgName)
org, _, err := client.Groups.GetGroup(values.OrgName)
if err != nil {
return fmt.Errorf("Error cant get information for Organisation \"%s\": %v", values.orgName, err)
return fmt.Errorf("Error cant get information for Organisation \"%s\": %v", values.OrgName, err)
}
fmt.Printf("Found \"%s\"\n", org.WebURL)