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
1 changed files with 33 additions and 4 deletions

37
main.go
View File

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


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


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


func main() { func loadCache() (cache *Cache) {
// default values // default values
var values = Cache{ cache = &Cache{
token: "XXXXXXXXXXXXXXXXXX", token: "XXXXXXXXXXXXXXXXXX",
baseURL: "https://inf-git.fh-rosenheim.de", baseURL: "https://gitlab.com",
orgName: "vv-inf-sose21", orgName: "organisation",
repoExclude: "exclude*", repoExclude: "exclude*",
pullBaseBranch: "devel*", 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 // ask for infos
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 {
os.Exit(1) os.Exit(1)
@ -48,6 +75,8 @@ func main() {
os.Exit(1) os.Exit(1)
} }


saveCache(values)

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