khuedoan-homelab/platform/gitea/files/config/main.go

94 lines
1.8 KiB
Go
Raw Normal View History

2022-03-22 12:39:22 +07:00
package main
// TODO WIP clean this up
2022-03-22 12:39:22 +07:00
import (
"log"
2022-03-22 13:08:58 +07:00
"os"
2022-03-22 12:39:22 +07:00
"code.gitea.io/sdk/gitea"
2022-03-22 13:08:58 +07:00
"gopkg.in/yaml.v2"
2022-03-22 12:39:22 +07:00
)
type Organization struct {
Name string
2022-03-22 13:08:58 +07:00
Description string
2022-03-22 12:39:22 +07:00
}
type Repository struct {
Name string
Owner string
Private bool
2022-03-22 12:39:22 +07:00
Migrate struct {
Source string
Mirror bool
}
}
type Config struct {
Organizations []Organization
Repositories []Repository
}
func main() {
2022-03-22 13:08:58 +07:00
data, err := os.ReadFile("./config.yaml")
if err != nil {
log.Fatalf("Unable to read config file: %v", err)
2022-03-22 13:08:58 +07:00
}
config := Config{}
err = yaml.Unmarshal([]byte(data), &config)
if err != nil {
log.Fatalf("error: %v", err)
}
gitea_host := os.Getenv("GITEA_HOST")
gitea_user := os.Getenv("GITEA_USER")
gitea_password := os.Getenv("GITEA_PASSWORD")
2022-03-22 12:39:22 +07:00
options := (gitea.SetBasicAuth(gitea_user, gitea_password))
client, err := gitea.NewClient(gitea_host, options)
2022-03-22 12:39:22 +07:00
if err != nil {
log.Fatal(err)
}
2022-03-22 13:08:58 +07:00
for _, org := range config.Organizations {
_, _, err = client.CreateOrg(gitea.CreateOrgOption{
Name: org.Name,
Description: org.Description,
})
2022-03-22 12:39:22 +07:00
2022-03-22 13:08:58 +07:00
if err != nil {
log.Printf("Create organization %s: %v", org.Name, err)
2022-03-22 13:08:58 +07:00
}
2022-03-22 12:39:22 +07:00
}
for _, repo := range config.Repositories {
if repo.Migrate.Source != "" {
_, _, err = client.MigrateRepo(gitea.MigrateRepoOption{
RepoName: repo.Name,
RepoOwner: repo.Owner,
CloneAddr: repo.Migrate.Source,
Service: gitea.GitServicePlain,
Mirror: repo.Migrate.Mirror,
Private: repo.Private,
MirrorInterval: "10m",
})
if err != nil {
log.Printf("Migrate %s/%s: %v", repo.Owner, repo.Name, err)
}
} else {
_, _, err = client.AdminCreateRepo(repo.Owner, gitea.CreateRepoOption{
Name: repo.Name,
// Description: "TODO",
Private: repo.Private,
})
}
2022-03-22 12:39:22 +07:00
}
}