From 7090618dbdc526827244079673edadbeaeef9a1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Majdand=C5=BEi=C4=87?= Date: Sat, 20 Jul 2024 19:51:33 +0200 Subject: [PATCH] Actually invert the init flag --- main.go | 80 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 42 insertions(+), 38 deletions(-) diff --git a/main.go b/main.go index b9d517c..f5db384 100644 --- a/main.go +++ b/main.go @@ -66,11 +66,13 @@ func main() { } var name string - var private, forceInit bool + 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.Parse() @@ -108,49 +110,51 @@ func main() { clipboard.Write(clipboard.FmtText, []byte(repo.CloneURL)) } - cwd, err := os.Getwd() - if err != nil { - Error.Fatalf("Error getting current working directory: %v", err) - os.Exit(1) - } - cwd = path.Clean(cwd) + 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("origin") - if err == nil { - if forceInit { - err = localRepo.DeleteRemote("origin") + 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 deleting remote from local repository: %v", err) + Error.Fatalf("Error opening git repository: %v", err) os.Exit(1) } } else { - Error.Fatalf("Remote origin already exists in local repository") + localRepo, err = git.PlainInit(cwd, false) + 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) } + 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") }