rebuilt error handling, various changes

This commit is contained in:
Paul 2019-07-09 16:42:04 +02:00
parent ca671e035f
commit 5f696c2814
2 changed files with 127 additions and 56 deletions

View File

@ -3,6 +3,7 @@ package main
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors"
"flag" "flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
@ -14,7 +15,7 @@ import (
) )
// GetConfig fetch configuration // GetConfig fetch configuration
func GetConfig(config *Config) { func GetConfig(config *Config) error {
var configfile string var configfile string
var globalconfig GlobalConfig var globalconfig GlobalConfig
@ -26,7 +27,9 @@ func GetConfig(config *Config) {
flag.Parse() flag.Parse()
cfg, err := ini.Load(configfile) cfg, err := ini.Load(configfile)
HandleFatalError(err) if err != nil {
return err
}
globalsection := cfg.Section("global") globalsection := cfg.Section("global")
githubsection := cfg.Section("github") githubsection := cfg.Section("github")
@ -34,11 +37,15 @@ func GetConfig(config *Config) {
globalconfig.UserAgent = globalsection.Key("user_agent").String() globalconfig.UserAgent = globalsection.Key("user_agent").String()
globalconfig.RequestTimeout, err = globalsection.Key("request_timeout").Duration() globalconfig.RequestTimeout, err = globalsection.Key("request_timeout").Duration()
HandleError(err) if err != nil {
return err
}
githubconfig.StarsPages = githubsection.Key("stars_pages").String() githubconfig.StarsPages = githubsection.Key("stars_pages").String()
githubconfig.MaxPagesNumber, err = githubsection.Key("max_pages_number").Int() githubconfig.MaxPagesNumber, err = githubsection.Key("max_pages_number").Int()
HandleError(err) if err != nil {
return err
}
githubconfig.AuthUsername = githubsection.Key("auth_username").String() githubconfig.AuthUsername = githubsection.Key("auth_username").String()
githubconfig.AuthPassword = githubsection.Key("auth_password").String() githubconfig.AuthPassword = githubsection.Key("auth_password").String()
@ -53,38 +60,54 @@ func GetConfig(config *Config) {
gogsconfig.AuthToken = gogssection.Key("auth_token").String() gogsconfig.AuthToken = gogssection.Key("auth_token").String()
gogsconfig.ContentType = gogssection.Key("content_type").String() gogsconfig.ContentType = gogssection.Key("content_type").String()
gogsconfig.Mirror, err = gogssection.Key("mirror").Bool() gogsconfig.Mirror, err = gogssection.Key("mirror").Bool()
HandleError(err) if err != nil {
return err
}
*config = Config{globalconfig: globalconfig, *config = Config{globalconfig: globalconfig,
githubconfig: githubconfig, githubconfig: githubconfig,
gogsconfig: gogsconfig} gogsconfig: gogsconfig}
return nil
} }
// GetReposFromGitHub get starred repositories from github // GetReposFromGitHub get starred repositories from github
func GetReposFromGitHub(config *Config) []GitHubRepo { func GetReposFromGitHub(config *Config) ([]GitHubRepo, error) {
var repolist []GitHubRepo var repolist []GitHubRepo
var repo []GitHubRepo var repo []GitHubRepo
for num := 1; num <= config.githubconfig.MaxPagesNumber; num++ { for num := 1; num <= config.githubconfig.MaxPagesNumber; num++ {
url := fmt.Sprintf(config.githubconfig.StarsPages, config.githubconfig.AuthUsername, num) url := fmt.Sprintf(config.githubconfig.StarsPages, config.githubconfig.AuthUsername, num)
resp := InvokeGitHub(config, url) resp, err := InvokeGitHub(config, url)
if err != nil {
return nil, err
}
err := json.Unmarshal(*GetResponseBody(resp), &repo) respbody, err := GetResponseBody(resp)
HandleError(err) if err != nil {
return nil, err
}
err = json.Unmarshal(*respbody, &repo)
if err != nil {
return nil, err
}
for _, elem := range repo { for _, elem := range repo {
repolist = append(repolist, elem) repolist = append(repolist, elem)
} }
} }
fmt.Println("Repositories from Github fetched !") fmt.Println(fmt.Sprintf("%d repositories from Github fetched", len(repolist)))
return repolist return repolist, nil
} }
// InvokeGitHub ... // InvokeGitHub ...
func InvokeGitHub(config *Config, url string) *http.Response { func InvokeGitHub(config *Config, url string) (*http.Response, error) {
req, err := http.NewRequest("GET", url, nil) req, err := http.NewRequest("GET", url, nil)
HandleError(err) if err != nil {
return nil, err
}
req.Header.Add("Content-Type", config.githubconfig.ContentType) req.Header.Add("Content-Type", config.githubconfig.ContentType)
req.Header.Set("User-Agent", config.globalconfig.UserAgent) req.Header.Set("User-Agent", config.globalconfig.UserAgent)
@ -92,21 +115,25 @@ func InvokeGitHub(config *Config, url string) *http.Response {
client := &http.Client{} client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
HandleError(err) if err != nil {
return nil, err
}
return resp return resp, nil
} }
// GetResponseBody converts http responses bodies as bytes slice // GetResponseBody converts http responses bodies as bytes slice
func GetResponseBody(resp *http.Response) *[]byte { func GetResponseBody(resp *http.Response) (*[]byte, error) {
bodyBytes, err := ioutil.ReadAll(resp.Body) bodyBytes, err := ioutil.ReadAll(resp.Body)
HandleError(err) if err != nil {
return &bodyBytes return nil, err
}
return &bodyBytes, nil
} }
// CheckGogsExistingRepo checks if there's an existing repository in gogs // CheckGogsExistingRepo checks if there's an existing repository in gogs
func CheckGogsExistingRepo(config *Config, repo GitHubRepo) bool { func CheckGogsExistingRepo(config *Config, repo GitHubRepo) (bool, error) {
var isExists bool var isexists bool
gogsrepourl := fmt.Sprintf(config.gogsconfig.RepoURLTmpl, config.gogsconfig.DestUsername, repo.Name) gogsrepourl := fmt.Sprintf(config.gogsconfig.RepoURLTmpl, config.gogsconfig.DestUsername, repo.Name)
@ -116,50 +143,78 @@ func CheckGogsExistingRepo(config *Config, repo GitHubRepo) bool {
client := &http.Client{} client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
HandleError(err) if err != nil {
return false, err
if resp.StatusCode == 200 {
isExists = true
} else {
isExists = false
} }
return isExists if resp.StatusCode == 200 {
isexists = true
} else if resp.StatusCode == 404 {
isexists = false
} else {
err = errors.New(fmt.Sprintf("Can't determine error, cancelling, error %d in gogs webservice", resp.StatusCode))
return false, err
}
return isexists, nil
} }
// GetGogsUserUID get the logued user identifier // GetGogsUserUID get the logued user identifier
func GetGogsUserUID(config *Config) { func GetGogsUserUID(config *Config) error {
var gogsorg GogsOrg var gogsorg GogsOrg
gogsrepourl := fmt.Sprintf(config.gogsconfig.OrgsURLTmpl, config.gogsconfig.DestUsername) gogsrepourl := fmt.Sprintf(config.gogsconfig.OrgsURLTmpl, config.gogsconfig.DestUsername)
req, err := http.NewRequest("GET", gogsrepourl, nil) req, err := http.NewRequest("GET", gogsrepourl, nil)
if err != nil {
return err
}
req.Header.Set("Authorization", config.gogsconfig.AuthToken) req.Header.Set("Authorization", config.gogsconfig.AuthToken)
req.Header.Set("User-Agent", config.globalconfig.UserAgent) req.Header.Set("User-Agent", config.globalconfig.UserAgent)
client := &http.Client{} client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
HandleError(err) if err != nil {
return err
}
err = json.Unmarshal(*GetResponseBody(resp), &gogsorg) if resp.StatusCode != 200 {
HandleError(err) err = errors.New("Error invoking gogs webservice")
return err
}
respbody, err := GetResponseBody(resp)
err = json.Unmarshal(*respbody, &gogsorg)
if err != nil {
err = errors.New("Failed to parse user ID from gogs webservice, check auth if possible")
return err
}
config.gogsconfig.UID = gogsorg.ID config.gogsconfig.UID = gogsorg.ID
return nil
} }
// MigrateReposToGogs migrates input repositories to gogs // MigrateReposToGogs migrates input repositories to gogs
func MigrateReposToGogs(config *Config, repolist []GitHubRepo) { func MigrateReposToGogs(config *Config, repolist []GitHubRepo) error {
fmt.Println("Migrate repos to Gogs...") fmt.Println("Migrate repos to Gogs...")
client := &http.Client{} client := &http.Client{}
for _, elem := range repolist { for _, elem := range repolist {
if !CheckGogsExistingRepo(config, elem) { existingrepo, err := CheckGogsExistingRepo(config, elem)
if err != nil {
return err
}
if !existingrepo {
jsondata := fmt.Sprintf(`{"uid" : %d, "repo_name" : "%s" , "mirror" : %v, "clone_addr" : "%s"}`, config.gogsconfig.UID, elem.Name, true, elem.CloneURL) jsondata := fmt.Sprintf(`{"uid" : %d, "repo_name" : "%s" , "mirror" : %v, "clone_addr" : "%s"}`, config.gogsconfig.UID, elem.Name, true, elem.CloneURL)
fmt.Println(fmt.Sprintf("Migrating repo %s to gogs rest api", elem.Name))
req, err := http.NewRequest("POST", config.gogsconfig.MigrateURL, bytes.NewBufferString(jsondata)) req, err := http.NewRequest("POST", config.gogsconfig.MigrateURL, bytes.NewBufferString(jsondata))
HandleError(err) if err != nil {
return err
}
req.Header.Add("Content-Type", config.gogsconfig.ContentType) req.Header.Add("Content-Type", config.gogsconfig.ContentType)
req.Header.Set("User-Agent", config.globalconfig.UserAgent) req.Header.Set("User-Agent", config.globalconfig.UserAgent)
@ -167,27 +222,22 @@ func MigrateReposToGogs(config *Config, repolist []GitHubRepo) {
client.Timeout = config.globalconfig.RequestTimeout client.Timeout = config.globalconfig.RequestTimeout
_, err = client.Do(req) fmt.Println(fmt.Sprintf("Migrating repo %s to gogs rest api", elem.Name))
HandleFatalError(err) resp, err := client.Do(req)
if err != nil {
return err
}
fmt.Println(fmt.Sprintf("Repo %s migrated with status code %d", elem.Name, resp.StatusCode))
if resp.StatusCode != 201 {
err = errors.New(fmt.Sprintf("Error when migrating repo %s to gogs, status code %d", elem.Name, resp.StatusCode))
log.Println(err)
}
} else { } else {
fmt.Println(fmt.Sprintf("Not migrating, %s exists in gogs", elem.Name)) fmt.Println(fmt.Sprintf("Not migrating, %s exists in gogs", elem.Name))
} }
} }
} return nil
// HandleError handles errors to return err
func HandleError(err error) {
if err != nil {
log.Println(err)
}
}
// HandleFatalError fatal errors
func HandleFatalError(err error) {
if err != nil {
log.Fatal(err)
os.Exit(2)
}
} }
// Usage displays possible arguments // Usage displays possible arguments

View File

@ -1,9 +1,30 @@
package main package main
import (
"log"
)
func main() { func main() {
var config Config var config Config
GetConfig(&config) var err error
GetGogsUserUID(&config)
repolist := GetReposFromGitHub(&config) err = GetConfig(&config)
MigrateReposToGogs(&config, repolist) if err != nil {
log.Fatal(err)
}
err = GetGogsUserUID(&config)
if err != nil {
log.Fatal(err)
}
repolist, err := GetReposFromGitHub(&config)
if err != nil {
log.Fatal(err)
}
err = MigrateReposToGogs(&config, repolist)
if err != nil {
log.Fatal(err)
}
} }