From fde65b3e8d9c62bfe530bf4c35a86720fc40b0b4 Mon Sep 17 00:00:00 2001 From: Paul Date: Wed, 29 Jan 2020 12:54:43 +0100 Subject: [PATCH] updated g2g --- .gitignore | 2 +- Makefile | 20 ++++++++++++--- README.md | 27 ++++++++++++++------ cmd/g2g/g2g.go | 30 +++++++++++++++++++++++ g2g.go | 28 --------------------- g2g.ini.sample | 4 +-- go.mod | 2 +- config.go => src/config/main.go | 5 ++-- config_test.go => src/config/main_test.go | 2 +- functions.go => src/g2g/main.go | 24 +++++++----------- src/utils/main.go | 12 +++++++++ 11 files changed, 95 insertions(+), 61 deletions(-) create mode 100644 cmd/g2g/g2g.go delete mode 100644 g2g.go rename config.go => src/config/main.go (93%) rename config_test.go => src/config/main_test.go (97%) rename functions.go => src/g2g/main.go (89%) create mode 100644 src/utils/main.go diff --git a/.gitignore b/.gitignore index 3bd9260..97380ff 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ *.swp *.ini -g2g +/g2g diff --git a/Makefile b/Makefile index 00f24a0..0babbfa 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,19 @@ +# g2g Makefile + +GOCMD=go +GOBUILDCMD=${GOCMD} build +GOOPTIONS=-mod=vendor -ldflags="-s -w" VERSION := $(shell cat ./VERSION) -build: - go build -ldflags="-s -w" -mod=vendor +RMCMD=rm +BINNAME=g2g -release: - +SRCFILES=cmd/g2g/*.go + +all: build + +build: + ${GOBUILDCMD} ${GOOPTIONS} ${SRCFILES} + +clean: + ${RMCMD} -f ${BINNAME} diff --git a/README.md b/README.md index 28d7a76..7952de8 100644 --- a/README.md +++ b/README.md @@ -47,17 +47,30 @@ gitea_mirror=true ## License ```text -Copyright (c) 2019 PaulBSD +Copyright (c) 2020 PaulBSD All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: -1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. -2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of the fuelprices project. -``` \ No newline at end of file +The views and conclusions contained in the software and documentation are those +of the authors and should not be interpreted as representing official policies, +either expressed or implied, of the fuelprices project. +``` diff --git a/cmd/g2g/g2g.go b/cmd/g2g/g2g.go new file mode 100644 index 0000000..73affb7 --- /dev/null +++ b/cmd/g2g/g2g.go @@ -0,0 +1,30 @@ +package main + +import ( + "git.paulbsd.com/paulbsd/g2g/src/config" + "git.paulbsd.com/paulbsd/g2g/src/g2g" + "log" +) + +func main() { + var cfg config.Config + var err error + var repolist []g2g.GitHubRepo + + err = cfg.GetConfig() + if err != nil { + log.Fatal(err) + } + + err = g2g.GetGiteaUserUID(&cfg) + if err != nil { + log.Fatal(err) + } + + repolist, err = g2g.GetReposFromGitHub(&cfg) + if err != nil { + log.Fatal(err) + } + + g2g.RunMigration(&cfg, repolist) +} diff --git a/g2g.go b/g2g.go deleted file mode 100644 index 0132723..0000000 --- a/g2g.go +++ /dev/null @@ -1,28 +0,0 @@ -package main - -import ( - "log" -) - -func main() { - var config Config - var err error - var repolist []GitHubRepo - - err = config.GetConfig() - if err != nil { - log.Fatal(err) - } - - err = config.GetGiteaUserUID() - if err != nil { - log.Fatal(err) - } - - repolist, err = GetReposFromGitHub(&config) - if err != nil { - log.Fatal(err) - } - - RunMigration(&config, repolist) -} diff --git a/g2g.ini.sample b/g2g.ini.sample index 01e7fd3..1f3c5da 100644 --- a/g2g.ini.sample +++ b/g2g.ini.sample @@ -11,7 +11,7 @@ github_auth_password="pass" gitea_username="user" gitea_dest_username="user_or_org" gitea_repo_url_tmpl="https://gogs.example.com/api/v1/repos/%s/%s" -gitea_orgs_url_tmpl="https://git.paulbsd.com/api/v1/orgs/%s" +gitea_orgs_url_tmpl="https://gogs.example.com/api/v1/orgs/%s" gitea_migrate_url="https://gogs.example.com/api/v1/repos/migrate" gitea_auth_token="token xxxx" -gitea_mirror=true \ No newline at end of file +gitea_mirror=true diff --git a/go.mod b/go.mod index 8da3513..88c57ac 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module g2g +module git.paulbsd.com/paulbsd/g2g go 1.13 diff --git a/config.go b/src/config/main.go similarity index 93% rename from config.go rename to src/config/main.go index 598e4ed..062a0e1 100644 --- a/config.go +++ b/src/config/main.go @@ -1,9 +1,10 @@ -package main +package config import ( "flag" "time" + "git.paulbsd.com/paulbsd/g2g/src/utils" "gopkg.in/ini.v1" ) @@ -11,7 +12,7 @@ import ( func (config *Config) GetConfig() error { var configfile string - flag.Usage = Usage + flag.Usage = utils.Usage flag.StringVar(&configfile, "configfile", "g2g.ini", "config file to use with g2g section") flag.Parse() diff --git a/config_test.go b/src/config/main_test.go similarity index 97% rename from config_test.go rename to src/config/main_test.go index a6b460b..e5db807 100644 --- a/config_test.go +++ b/src/config/main_test.go @@ -1,4 +1,4 @@ -package main +package config import ( "fmt" diff --git a/functions.go b/src/g2g/main.go similarity index 89% rename from functions.go rename to src/g2g/main.go index c8c1238..ef31b8f 100644 --- a/functions.go +++ b/src/g2g/main.go @@ -1,20 +1,20 @@ -package main +package g2g import ( "bytes" "encoding/json" "errors" - "flag" "fmt" "io/ioutil" "log" "net/http" - "os" "sync" + + "git.paulbsd.com/paulbsd/g2g/src/config" ) // GetReposFromGitHub get starred repositories from github -func GetReposFromGitHub(config *Config) ([]GitHubRepo, error) { +func GetReposFromGitHub(config *config.Config) ([]GitHubRepo, error) { var repopartiallist []GitHubRepo var repofulllist []GitHubRepo @@ -48,7 +48,7 @@ func GetReposFromGitHub(config *Config) ([]GitHubRepo, error) { } // GetGitHubResponse fetchs starred repositories on GitHub -func GetGitHubResponse(config *Config, url string) (*http.Response, error) { +func GetGitHubResponse(config *config.Config, url string) (*http.Response, error) { req, err := http.NewRequest("GET", url, nil) if err != nil { return nil, err @@ -71,7 +71,7 @@ func GetGitHubResponse(config *Config, url string) (*http.Response, error) { } // CheckGiteaExistingRepo checks if there's an existing repository in gitea -func CheckGiteaExistingRepo(config *Config, repo GitHubRepo) (bool, error) { +func CheckGiteaExistingRepo(config *config.Config, repo GitHubRepo) (bool, error) { var isExists bool gitearepourl := fmt.Sprintf(config.GiteaRepoURLTmpl, config.GiteaDestUsername, repo.Name) @@ -97,7 +97,7 @@ func CheckGiteaExistingRepo(config *Config, repo GitHubRepo) (bool, error) { } // GetGiteaUserUID get the logued user identifier -func (config *Config) GetGiteaUserUID() error { +func GetGiteaUserUID(config *config.Config) error { var giteaorg GiteaOrg gitearepourl := fmt.Sprintf(config.GiteaOrgsURLTmpl, config.GiteaDestUsername) @@ -134,7 +134,7 @@ func (config *Config) GetGiteaUserUID() error { } // RunMigration run threads for migration -func RunMigration(config *Config, repolist []GitHubRepo) { +func RunMigration(config *config.Config, repolist []GitHubRepo) { repochan := make(chan GitHubRepo) done := make(chan bool) @@ -152,7 +152,7 @@ func RunMigration(config *Config, repolist []GitHubRepo) { } // MigrateReposToGitea migrates input repositories to gitea -func MigrateReposToGitea(config *Config, wg *sync.WaitGroup, jobs chan GitHubRepo, done chan bool, thr int) error { +func MigrateReposToGitea(config *config.Config, wg *sync.WaitGroup, jobs chan GitHubRepo, done chan bool, thr int) error { wg.Add(1) for { elem, more := <-jobs @@ -199,12 +199,6 @@ func MigrateReposToGitea(config *Config, wg *sync.WaitGroup, jobs chan GitHubRep } } -// Usage displays possible arguments -func Usage() { - flag.PrintDefaults() - os.Exit(1) -} - // GitHubRepo githubrepo struct type GitHubRepo struct { Name string `json:"name"` diff --git a/src/utils/main.go b/src/utils/main.go new file mode 100644 index 0000000..d6f52a5 --- /dev/null +++ b/src/utils/main.go @@ -0,0 +1,12 @@ +package utils + +import ( + "flag" + "os" +) + +// Usage displays possible arguments +func Usage() { + flag.PrintDefaults() + os.Exit(1) +}