rebuilt error handling, various changes
This commit is contained in:
parent
ca671e035f
commit
5f696c2814
154
functions.go
154
functions.go
@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
@ -14,7 +15,7 @@ import (
|
||||
)
|
||||
|
||||
// GetConfig fetch configuration
|
||||
func GetConfig(config *Config) {
|
||||
func GetConfig(config *Config) error {
|
||||
var configfile string
|
||||
|
||||
var globalconfig GlobalConfig
|
||||
@ -26,7 +27,9 @@ func GetConfig(config *Config) {
|
||||
flag.Parse()
|
||||
|
||||
cfg, err := ini.Load(configfile)
|
||||
HandleFatalError(err)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
globalsection := cfg.Section("global")
|
||||
githubsection := cfg.Section("github")
|
||||
@ -34,11 +37,15 @@ func GetConfig(config *Config) {
|
||||
|
||||
globalconfig.UserAgent = globalsection.Key("user_agent").String()
|
||||
globalconfig.RequestTimeout, err = globalsection.Key("request_timeout").Duration()
|
||||
HandleError(err)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
githubconfig.StarsPages = githubsection.Key("stars_pages").String()
|
||||
githubconfig.MaxPagesNumber, err = githubsection.Key("max_pages_number").Int()
|
||||
HandleError(err)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
githubconfig.AuthUsername = githubsection.Key("auth_username").String()
|
||||
githubconfig.AuthPassword = githubsection.Key("auth_password").String()
|
||||
@ -53,38 +60,54 @@ func GetConfig(config *Config) {
|
||||
gogsconfig.AuthToken = gogssection.Key("auth_token").String()
|
||||
gogsconfig.ContentType = gogssection.Key("content_type").String()
|
||||
gogsconfig.Mirror, err = gogssection.Key("mirror").Bool()
|
||||
HandleError(err)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*config = Config{globalconfig: globalconfig,
|
||||
githubconfig: githubconfig,
|
||||
gogsconfig: gogsconfig}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetReposFromGitHub get starred repositories from github
|
||||
func GetReposFromGitHub(config *Config) []GitHubRepo {
|
||||
func GetReposFromGitHub(config *Config) ([]GitHubRepo, error) {
|
||||
var repolist []GitHubRepo
|
||||
var repo []GitHubRepo
|
||||
|
||||
for num := 1; num <= config.githubconfig.MaxPagesNumber; 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)
|
||||
HandleError(err)
|
||||
respbody, err := GetResponseBody(resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(*respbody, &repo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, elem := range repo {
|
||||
repolist = append(repolist, elem)
|
||||
}
|
||||
}
|
||||
fmt.Println("Repositories from Github fetched !")
|
||||
return repolist
|
||||
fmt.Println(fmt.Sprintf("%d repositories from Github fetched", len(repolist)))
|
||||
return repolist, nil
|
||||
}
|
||||
|
||||
// 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)
|
||||
HandleError(err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Add("Content-Type", config.githubconfig.ContentType)
|
||||
req.Header.Set("User-Agent", config.globalconfig.UserAgent)
|
||||
@ -92,21 +115,25 @@ func InvokeGitHub(config *Config, url string) *http.Response {
|
||||
|
||||
client := &http.Client{}
|
||||
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
|
||||
func GetResponseBody(resp *http.Response) *[]byte {
|
||||
func GetResponseBody(resp *http.Response) (*[]byte, error) {
|
||||
bodyBytes, err := ioutil.ReadAll(resp.Body)
|
||||
HandleError(err)
|
||||
return &bodyBytes
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &bodyBytes, nil
|
||||
}
|
||||
|
||||
// CheckGogsExistingRepo checks if there's an existing repository in gogs
|
||||
func CheckGogsExistingRepo(config *Config, repo GitHubRepo) bool {
|
||||
var isExists bool
|
||||
func CheckGogsExistingRepo(config *Config, repo GitHubRepo) (bool, error) {
|
||||
var isexists bool
|
||||
|
||||
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{}
|
||||
resp, err := client.Do(req)
|
||||
HandleError(err)
|
||||
|
||||
if resp.StatusCode == 200 {
|
||||
isExists = true
|
||||
} else {
|
||||
isExists = false
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
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
|
||||
func GetGogsUserUID(config *Config) {
|
||||
func GetGogsUserUID(config *Config) error {
|
||||
var gogsorg GogsOrg
|
||||
|
||||
gogsrepourl := fmt.Sprintf(config.gogsconfig.OrgsURLTmpl, config.gogsconfig.DestUsername)
|
||||
|
||||
req, err := http.NewRequest("GET", gogsrepourl, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", config.gogsconfig.AuthToken)
|
||||
req.Header.Set("User-Agent", config.globalconfig.UserAgent)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
HandleError(err)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(*GetResponseBody(resp), &gogsorg)
|
||||
HandleError(err)
|
||||
if resp.StatusCode != 200 {
|
||||
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
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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...")
|
||||
|
||||
client := &http.Client{}
|
||||
|
||||
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)
|
||||
fmt.Println(fmt.Sprintf("Migrating repo %s to gogs rest api", elem.Name))
|
||||
|
||||
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.Set("User-Agent", config.globalconfig.UserAgent)
|
||||
@ -167,27 +222,22 @@ func MigrateReposToGogs(config *Config, repolist []GitHubRepo) {
|
||||
|
||||
client.Timeout = config.globalconfig.RequestTimeout
|
||||
|
||||
_, err = client.Do(req)
|
||||
HandleFatalError(err)
|
||||
fmt.Println(fmt.Sprintf("Migrating repo %s to gogs rest api", elem.Name))
|
||||
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 {
|
||||
fmt.Println(fmt.Sprintf("Not migrating, %s exists in gogs", elem.Name))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Usage displays possible arguments
|
||||
|
@ -1,9 +1,30 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var config Config
|
||||
GetConfig(&config)
|
||||
GetGogsUserUID(&config)
|
||||
repolist := GetReposFromGitHub(&config)
|
||||
MigrateReposToGogs(&config, repolist)
|
||||
var err error
|
||||
|
||||
err = GetConfig(&config)
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user