Add Config Cache

This commit is contained in:
6543 2021-05-05 11:38:49 +02:00
parent 2a7cc5d14f
commit fc73cc3156
Signed by: 6543
GPG Key ID: C99B82E40B027BAE

37
main.go
View File

@ -1,7 +1,9 @@
package main

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

"github.com/AlecAivazis/survey/v2"
@ -21,16 +23,41 @@ type Cache struct {
pullBaseBranch string `json:pullBaseBranch`
}

func main() {
func loadCache() (cache *Cache) {
// default values
var values = Cache{
cache = &Cache{
token: "XXXXXXXXXXXXXXXXXX",
baseURL: "https://inf-git.fh-rosenheim.de",
orgName: "vv-inf-sose21",
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, 0666)
if err != nil {
fmt.Printf("ERROR: save cache: %v\n", err)
}
}

func main() {
values := loadCache()

// ask for infos
if err := survey.AskOne(&survey.Input{Message: "GitLab Base URL:", Default: values.baseURL}, &values.baseURL); err != nil {
os.Exit(1)
@ -48,6 +75,8 @@ func main() {
os.Exit(1)
}

saveCache(values)

// Compile glob regex
excludeRule, err := glob.Compile(values.repoExclude)
if err != nil {