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

91 lines
1.6 KiB
Go
Raw Normal View History

2022-03-22 12:39:22 +07:00
package main
import (
2022-03-22 13:08:58 +07:00
"fmt"
2022-03-22 12:39:22 +07:00
"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 User struct {
Name string
TokenSecretRef string
}
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 {
Path string
Migrate struct {
Source string
Mirror bool
}
Webhooks []string
}
type Config struct {
Users []User
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)
}
config := Config{}
err = yaml.Unmarshal([]byte(data), &config)
if err != nil {
log.Fatalf("error: %v", err)
}
fmt.Println(config)
2022-03-22 12:39:22 +07:00
// TODO
url := "https://git.khuedoan.com"
// url := "http://gitea-http:3000"
password := "thisisjustfortestingdude"
options := (gitea.SetBasicAuth("gitea_admin", password))
client, err := gitea.NewClient(url, options)
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: %s", "testing", err)
}
2022-03-22 12:39:22 +07:00
}
_, _, err = client.MigrateRepo(gitea.MigrateRepoOption{
RepoName: "homelab",
RepoOwner: "ops",
CloneAddr: "https://github.com/khuedoan/homelab",
Service: gitea.GitServicePlain,
Mirror: true,
Private: false,
MirrorInterval: "10m",
})
if err != nil {
log.Printf("Migrate %s/%s: %s", "ops", "homelab", err)
}
}