From 2ae4a7db6b21eda71ae2684b3730829040de5c3b Mon Sep 17 00:00:00 2001 From: Uthman Fatih Date: Fri, 30 Oct 2026 15:00:32 +0000 Subject: [PATCH] Added simple debug flag --- src/debugMode.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ src/go.mod | 2 ++ src/go.sum | 2 ++ src/main.go | 24 ++++++++++++++++-- 4 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 src/debugMode.go create mode 100644 src/go.sum diff --git a/src/debugMode.go b/src/debugMode.go new file mode 100644 index 0000000..dde330c --- /dev/null +++ b/src/debugMode.go @@ -0,0 +1,66 @@ +package main + +import ( + "fmt" + "os" + "os/exec" + "path/filepath" + "runtime/debug" + "strings" +) + +func git(dir string, args ...string) (string, error) { + out, err := exec.Command("git", append([]string{"-C", dir}, args...)...).Output() + return strings.TrimSpace(string(out)), err +} + +// findRepo checks the current directory, then its immediate subdirectories. +func findRepo() (string, error) { + cwd, err := os.Getwd() + if err != nil { + return "", err + } + candidates := []string{cwd, filepath.Dir(cwd)} + entries, _ := os.ReadDir(cwd) + for _, e := range entries { + if e.IsDir() { + candidates = append(candidates, filepath.Join(cwd, e.Name())) + } + } + for _, dir := range candidates { + if _, err := os.Stat(filepath.Join(dir, ".git")); err == nil { + return dir, nil + } + } + return "", fmt.Errorf("no git repo in %s, its parent, or its immediate subfolders", cwd) +} + +func GetInfo() { + // Build info embedded at compile time + if bi, ok := debug.ReadBuildInfo(); ok { + fmt.Println("Go version:", bi.GoVersion) + fmt.Println("Module: ", bi.Main.Path, bi.Main.Version) + for _, s := range bi.Settings { + switch s.Key { + case "vcs", "vcs.revision", "vcs.time", "vcs.modified", + "GOOS", "GOARCH", "CGO_ENABLED", "-trimpath", "-compiler": + fmt.Printf("%-12s %s\n", s.Key+":", s.Value) + } + } + } + + // Git state of the repo found at runtime + repo, err := findRepo() + if err != nil { + fmt.Println("Git:", err) + return + } + root, _ := git(repo, "rev-parse", "--show-toplevel") + commit, err := git(repo, "rev-parse", "HEAD") + if err != nil { + fmt.Println("Git error:", err) + return + } + fmt.Println("Repo root:", root) + fmt.Println("Commit: ", commit) +} \ No newline at end of file diff --git a/src/go.mod b/src/go.mod index c245eb8..5be8655 100644 --- a/src/go.mod +++ b/src/go.mod @@ -1,3 +1,5 @@ module git.uthmn.com/boiabba/anical go 1.27.3 + +require github.com/spf13/pflag v1.0.10 diff --git a/src/go.sum b/src/go.sum new file mode 100644 index 0000000..8ec1276 --- /dev/null +++ b/src/go.sum @@ -0,0 +1,2 @@ +github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk= +github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= diff --git a/src/main.go b/src/main.go index 6835f99..9200403 100644 --- a/src/main.go +++ b/src/main.go @@ -1,7 +1,27 @@ package main -import "fmt" +import ( + "fmt" + flag "github.com/spf13/pflag" +) func main() { - fmt.Println("Cooking up v2") + fmt.Print(` + ::: :::: ::: ::::::::::: :::::::: ::: ::: + :+: :+: :+:+: :+: :+: :+: :+: :+: :+: :+: + +:+ +:+ :+:+:+ +:+ +:+ +:+ +:+ +:+ +:+ + +#++:++#++: +#+ +:+ +#+ +#+ +#+ +#++:++#++: +#+ + +#+ +#+ +#+ +#+#+# +#+ +#+ +#+ +#+ +#+ + #+# #+# #+# #+#+# #+# #+# #+# #+# #+# #+# +### ### ### #### ########### ######## ### ### ########## + `) + + var debugFlag = flag.BoolP("debug", "d", false, "Enable debug mode") + flag.Parse() + if *debugFlag { + fmt.Println("Debug mode enabled") + GetInfo() + } + + } \ No newline at end of file