Compare commits

..

2 Commits

Author SHA1 Message Date
219a75a1d3 Add dryrun functionality 2025-10-15 10:21:22 +02:00
b286d5578e Rework flags to use pflag 2025-10-15 10:18:49 +02:00
4 changed files with 47 additions and 46 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,2 @@
*.log
.env
*.exe

1
go.mod
View File

@@ -29,6 +29,7 @@ require (
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/skeema/knownhosts v1.2.2 // indirect
github.com/spf13/pflag v1.0.10 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 // indirect

2
go.sum
View File

@@ -81,6 +81,8 @@ github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/skeema/knownhosts v1.2.2 h1:Iug2P4fLmDw9f41PB6thxUkNUkJzB5i+1/exaj40L3A=
github.com/skeema/knownhosts v1.2.2/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo=
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=

41
main.go
View File

@@ -2,10 +2,11 @@ package main
import (
_ "embed"
"flag"
"os"
"path"
flag "github.com/spf13/pflag"
"code.gitea.io/sdk/gitea"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
@@ -14,28 +15,17 @@ import (
logger "git.site.quack-lab.dev/dave/cylogger"
)
var Version = "1.0.0"
var Version = "2.0.0"
func main() {
name := flag.String("name", "", "Name of the repository")
flag.String("n", "", "Name of the repository")
private := flag.Bool("private", false, "Make the repository private")
flag.Bool("p", false, "Make the repository private (shorthand)")
name := flag.StringP("name", "n", "", "Name of the repository")
private := flag.BoolP("private", "p", false, "Make the repository private")
noinit := flag.Bool("noinit", false, "Do not add remote to the new repo")
flag.Bool("ni", false, "Do not add remote to the new repo (shorthand)")
forceInit := flag.Bool("f", false, "Force assignment of the remote (deletes existing origin)")
remote := flag.String("remote", "origin", "Name of the remote to create for the new repository")
flag.String("r", "origin", "Name of the remote to create for the new repository (shorthand)")
version := flag.Bool("v", false, "Show version")
flag.Bool("version", false, "Show version")
help := flag.Bool("h", false, "Show help")
flag.Bool("help", false, "Show help")
forceInit := flag.BoolP("force", "f", false, "Force assignment of the remote (deletes existing origin)")
remote := flag.StringP("remote", "r", "origin", "Name of the remote to create for the new repository")
version := flag.BoolP("version", "v", false, "Show version")
dryrun := flag.Bool("dry-run", false, "Dry run")
help := flag.BoolP("help", "h", false, "Show help")
flag.Parse()
logger.InitFlag()
@@ -92,8 +82,9 @@ func main() {
logger.Info("Name: %s", *name)
logger.Info("Private: %t", *private)
logger.Info("Noinit: %t", *noinit)
logger.Info("ForceInit: %t", *forceInit)
logger.Info("Force: %t", *forceInit)
logger.Info("Remote: %s", *remote)
logger.Info("Dryrun: %t", *dryrun)
client, err := gitea.NewClient(url)
if err != nil {
@@ -109,6 +100,7 @@ func main() {
clipboard.Write(clipboard.FmtText, []byte(repo.CloneURL))
} else {
logger.Info("Repository does not exist, creating...")
if !*dryrun {
repo, _, err = client.CreateRepo(gitea.CreateRepoOption{
Name: *name,
Private: *private,
@@ -118,6 +110,7 @@ func main() {
logger.Error("Error creating repository: %v", err)
return
}
}
logger.Info("Repository created at: %s", repo.CloneURL)
clipboard.Write(clipboard.FmtText, []byte(repo.CloneURL))
}
@@ -141,23 +134,27 @@ func main() {
return
}
} else {
if !*dryrun {
localRepo, err = git.PlainInit(cwd, false)
if err != nil {
logger.Error("Error initializing git repository: %v", err)
return
}
}
}
logger.Info("Checking if remote %s exists in local repository...", *remote)
_, err = localRepo.Remote(*remote)
if err == nil {
logger.Info("Remote %s exists in local repository", *remote)
if *forceInit {
logger.Info("Deleting remote %s from local repository...", *remote)
if !*dryrun {
err = localRepo.DeleteRemote(*remote)
if err != nil {
logger.Error("Error deleting remote %s from local repository: %v", *remote, err)
return
}
}
logger.Info("Remote %s deleted from local repository", *remote)
} else {
logger.Error("Remote %s already exists in local repository", *remote)
@@ -166,6 +163,7 @@ func main() {
}
logger.Info("Adding remote %s to local repository...", *remote)
if !*dryrun {
_, err = localRepo.CreateRemote(&config.RemoteConfig{
Name: *remote,
URLs: []string{repo.CloneURL},
@@ -174,6 +172,7 @@ func main() {
logger.Error("Error adding remote %s to local repository: %v", *remote, err)
return
}
}
logger.Info("Remote added to local repository")
}
logger.Info("Done")