117 lines
2.6 KiB
Go
117 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"log"
|
|
"os"
|
|
"sync"
|
|
|
|
"git.acooldomain.co/acoolname/git-syncer/src/enumerators"
|
|
"git.acooldomain.co/acoolname/git-syncer/src/syncer"
|
|
yaml "gopkg.in/yaml.v3"
|
|
)
|
|
|
|
type AccountEnumerator string
|
|
|
|
var GITEA_ENUMERATOR AccountEnumerator = "gitea"
|
|
|
|
type Account struct {
|
|
Type AccountEnumerator `yaml:"type"`
|
|
enumerators.EnumeratorConfig `yaml:",inline"`
|
|
}
|
|
|
|
type Config struct {
|
|
Accounts []Account `yaml:"accounts"`
|
|
BaseLocation string `yaml:"base_location"`
|
|
Overrides map[string]string `yaml:"overrides,omitempty"`
|
|
}
|
|
|
|
var configPath *string
|
|
|
|
func init() {
|
|
configPath = flag.String("config-path", ".config/git-syncer/config.yaml", "path to use the config file for")
|
|
flag.Parse()
|
|
}
|
|
|
|
func main() {
|
|
config := &Config{}
|
|
configFileData, err := os.ReadFile(*configPath)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
templateConfig := Config{
|
|
BaseLocation: "path/to/root/sync",
|
|
Overrides: map[string]string{
|
|
"REPO_URL": "TARGET_DIR",
|
|
},
|
|
Accounts: []Account{
|
|
{
|
|
Type: GITEA_ENUMERATOR,
|
|
EnumeratorConfig: enumerators.EnumeratorConfig{
|
|
ApiUrl: "https://....",
|
|
Username: "change-me",
|
|
Password: "change-me",
|
|
CloneMethod: enumerators.SSHClone,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
templateConfigRaw, err := yaml.Marshal(templateConfig)
|
|
if err != nil {
|
|
log.Printf("Failed to marshal template data %e", err)
|
|
return
|
|
}
|
|
|
|
err = os.WriteFile(*configPath, templateConfigRaw, 0700)
|
|
if err != nil {
|
|
log.Printf("Failed to write template data %e", err)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
err = yaml.Unmarshal(configFileData, config)
|
|
if err != nil {
|
|
log.Printf("Failed to parse config: %e", err)
|
|
}
|
|
ctx := context.Background()
|
|
|
|
wg := &sync.WaitGroup{}
|
|
repositories := make(chan *enumerators.RepositoryEndpoint)
|
|
errChan := make(chan error)
|
|
|
|
for _, account := range config.Accounts {
|
|
var enumerator enumerators.Enumerator
|
|
var err error
|
|
switch account.Type {
|
|
case GITEA_ENUMERATOR:
|
|
enumerator, err = enumerators.CreateGiteaEnumerator(&account.EnumeratorConfig)
|
|
}
|
|
if err != nil {
|
|
log.Printf("Failed to enumerate \"%s\": %e", account.ApiUrl, err)
|
|
continue
|
|
}
|
|
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
err := enumerator(repositories)
|
|
if err != nil {
|
|
log.Println("Failed to use enumerator: ", err)
|
|
}
|
|
}()
|
|
}
|
|
|
|
go func() {
|
|
wg.Wait()
|
|
close(repositories)
|
|
close(errChan)
|
|
for err := range errChan {
|
|
log.Printf("Encountered error: %e", err)
|
|
}
|
|
}()
|
|
|
|
log.Printf("syncing")
|
|
syncer.StoreRepositories(ctx, config.BaseLocation, repositories, config.Overrides)
|
|
}
|