168 lines
4.4 KiB
Go
168 lines
4.4 KiB
Go
package main
|
|
|
|
import (
|
|
_ "embed"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
"github.com/go-git/go-git/v5"
|
|
"github.com/go-git/go-git/v5/config"
|
|
"github.com/joho/godotenv"
|
|
"golang.design/x/clipboard"
|
|
)
|
|
|
|
var Error *log.Logger
|
|
|
|
func init() {
|
|
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
|
|
// logFile, err := os.Create("main.log")
|
|
// if err != nil {
|
|
// log.Printf("Error creating log file: %v", err)
|
|
// os.Exit(1)
|
|
// }
|
|
logger := io.MultiWriter(os.Stdout)
|
|
log.SetOutput(logger)
|
|
|
|
Error = log.New(io.MultiWriter(os.Stderr, os.Stdout),
|
|
fmt.Sprintf("%sERROR:%s ", "\033[0;101m", "\033[0m"),
|
|
log.Lmicroseconds|log.Lshortfile)
|
|
}
|
|
|
|
//go:embed .env
|
|
var env string
|
|
|
|
func main() {
|
|
err := clipboard.Init()
|
|
if err != nil {
|
|
Error.Fatalf("Error initializing clipboard: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
envvar, err := godotenv.Parse(strings.NewReader(env))
|
|
if err != nil {
|
|
Error.Fatalf("Error parsing .env file: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
username, ok := envvar["GITEA_USER"]
|
|
if !ok {
|
|
Error.Fatalf("GITEA_USER environment variable is required")
|
|
os.Exit(1)
|
|
}
|
|
password, ok := envvar["GITEA_PASSWORD"]
|
|
if !ok {
|
|
Error.Fatalf("GITEA_PASSWORD environment variable is required")
|
|
os.Exit(1)
|
|
}
|
|
url, ok := envvar["GITEA_URL"]
|
|
if !ok {
|
|
Error.Fatalf("GITEA_URL environment variable is required")
|
|
os.Exit(1)
|
|
}
|
|
|
|
if username == "" || password == "" {
|
|
Error.Fatalf("GITEA_USER and GITEA_PASSWORD environment variables are required")
|
|
os.Exit(1)
|
|
}
|
|
|
|
var name, remote string
|
|
var private, noinit, forceInit bool
|
|
flag.StringVar(&name, "name", "", "Name of the repository")
|
|
flag.StringVar(&name, "n", "", "Name of the repository (shorthand)")
|
|
flag.BoolVar(&private, "private", false, "Make the repository private")
|
|
flag.BoolVar(&private, "p", false, "Make the repository private (shorthand)")
|
|
flag.BoolVar(&noinit, "noinit", false, "Do not add remote to the new repo")
|
|
flag.BoolVar(&noinit, "ni", false, "Do not add remote to the new repo (shorthand)")
|
|
flag.BoolVar(&forceInit, "f", false, "Force assignment of the remote (deletes existing origin)")
|
|
flag.StringVar(&remote, "remote", "origin", "Namw of the remote to create for the new repository")
|
|
flag.StringVar(&remote, "r", "origin", "Namw of the remote to create for the new repository (shorthand)")
|
|
flag.Parse()
|
|
|
|
if len(flag.Args()) > 0 {
|
|
name = flag.Args()[0]
|
|
}
|
|
|
|
if name == "" {
|
|
Error.Fatalf("Repository name is required")
|
|
os.Exit(1)
|
|
}
|
|
|
|
client, err := gitea.NewClient(url)
|
|
if err != nil {
|
|
Error.Fatalf("Error creating client: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
client.SetBasicAuth("dave", "D7u@NHh^9d33ue!xVAEu")
|
|
|
|
repo, _, err := client.GetRepo("dave", name)
|
|
if err == nil {
|
|
log.Printf("Repository already exists at:\n%s", repo.CloneURL)
|
|
clipboard.Write(clipboard.FmtText, []byte(repo.CloneURL))
|
|
} else {
|
|
repo, _, err = client.CreateRepo(gitea.CreateRepoOption{
|
|
Name: name,
|
|
Private: private,
|
|
DefaultBranch: "master",
|
|
})
|
|
if err != nil {
|
|
Error.Fatalf("Error creating repository: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
log.Printf("Repository created at:\n%s", repo.CloneURL)
|
|
clipboard.Write(clipboard.FmtText, []byte(repo.CloneURL))
|
|
}
|
|
|
|
if !noinit {
|
|
cwd, err := os.Getwd()
|
|
if err != nil {
|
|
Error.Fatalf("Error getting current working directory: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
cwd = path.Clean(cwd)
|
|
|
|
var localRepo *git.Repository
|
|
_, err = os.Stat(path.Join(cwd, ".git"))
|
|
if err == nil {
|
|
localRepo, err = git.PlainOpen(cwd)
|
|
if err != nil {
|
|
Error.Fatalf("Error opening git repository: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
} else {
|
|
localRepo, err = git.PlainInit(cwd, false)
|
|
if err != nil {
|
|
Error.Fatalf("Error initializing git repository: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
_, err = localRepo.Remote(remote)
|
|
if err == nil {
|
|
if forceInit {
|
|
err = localRepo.DeleteRemote(remote)
|
|
if err != nil {
|
|
Error.Fatalf("Error deleting remote %s from local repository: %v", remote, err)
|
|
os.Exit(1)
|
|
}
|
|
} else {
|
|
Error.Fatalf("Remote %s already exists in local repository", remote)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
_, err = localRepo.CreateRemote(&config.RemoteConfig{
|
|
Name: remote,
|
|
URLs: []string{repo.CloneURL},
|
|
})
|
|
if err != nil {
|
|
Error.Fatalf("Error adding remote %s to local repository: %v", remote, err)
|
|
os.Exit(1)
|
|
}
|
|
log.Printf("Remote added to local repository")
|
|
}
|
|
}
|