Remove init option

Why would you not use it
This commit is contained in:
2024-07-20 19:51:01 +02:00
parent 10f6c5b972
commit e9b6788af6

76
main.go
View File

@@ -66,13 +66,11 @@ func main() {
} }
var name string var name string
var private, init, forceInit bool var private, forceInit bool
flag.StringVar(&name, "name", "", "Name of the repository") flag.StringVar(&name, "name", "", "Name of the repository")
flag.StringVar(&name, "n", "", "Name of the repository (shorthand)") flag.StringVar(&name, "n", "", "Name of the repository (shorthand)")
flag.BoolVar(&private, "private", false, "Make the repository private") flag.BoolVar(&private, "private", false, "Make the repository private")
flag.BoolVar(&private, "p", false, "Make the repository private (shorthand)") flag.BoolVar(&private, "p", false, "Make the repository private (shorthand)")
flag.BoolVar(&init, "init", false, "Also add remote to the new repo")
flag.BoolVar(&init, "i", false, "Also add remote to the new repo (shorthand)")
flag.BoolVar(&forceInit, "f", false, "Force assignment of the remote (deletes existing origin)") flag.BoolVar(&forceInit, "f", false, "Force assignment of the remote (deletes existing origin)")
flag.Parse() flag.Parse()
@@ -110,51 +108,49 @@ func main() {
clipboard.Write(clipboard.FmtText, []byte(repo.CloneURL)) clipboard.Write(clipboard.FmtText, []byte(repo.CloneURL))
} }
if init { cwd, err := os.Getwd()
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 { if err != nil {
Error.Fatalf("Error getting current working directory: %v", err) Error.Fatalf("Error opening git repository: %v", err)
os.Exit(1) os.Exit(1)
} }
cwd = path.Clean(cwd) } else {
localRepo, err = git.PlainInit(cwd, false)
var localRepo *git.Repository if err != nil {
_, err = os.Stat(path.Join(cwd, ".git")) Error.Fatalf("Error initializing git repository: %v", err)
if err == nil { os.Exit(1)
localRepo, err = git.PlainOpen(cwd) }
}
_, err = localRepo.Remote("origin")
if err == nil {
if forceInit {
err = localRepo.DeleteRemote("origin")
if err != nil { if err != nil {
Error.Fatalf("Error opening git repository: %v", err) Error.Fatalf("Error deleting remote from local repository: %v", err)
os.Exit(1) os.Exit(1)
} }
} else { } else {
localRepo, err = git.PlainInit(cwd, false) Error.Fatalf("Remote origin already exists in local repository")
if err != nil {
Error.Fatalf("Error initializing git repository: %v", err)
os.Exit(1)
}
}
_, err = localRepo.Remote("origin")
if err == nil {
if forceInit {
err = localRepo.DeleteRemote("origin")
if err != nil {
Error.Fatalf("Error deleting remote from local repository: %v", err)
os.Exit(1)
}
} else {
Error.Fatalf("Remote origin already exists in local repository")
os.Exit(1)
}
}
_, err = localRepo.CreateRemote(&config.RemoteConfig{
Name: "origin",
URLs: []string{repo.CloneURL},
})
if err != nil {
Error.Fatalf("Error adding remote to local repository: %v", err)
os.Exit(1) os.Exit(1)
} }
log.Printf("Remote added to local repository")
} }
_, err = localRepo.CreateRemote(&config.RemoteConfig{
Name: "origin",
URLs: []string{repo.CloneURL},
})
if err != nil {
Error.Fatalf("Error adding remote to local repository: %v", err)
os.Exit(1)
}
log.Printf("Remote added to local repository")
} }