Added simple debug flag

This commit is contained in:
Uthman Fatih 2026-10-30 15:00:32 +00:00
parent 0568cc2a1b
commit 2ae4a7db6b
4 changed files with 92 additions and 2 deletions

66
src/debugMode.go Normal file
View File

@ -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)
}

View File

@ -1,3 +1,5 @@
module git.uthmn.com/boiabba/anical
go 1.27.3
require github.com/spf13/pflag v1.0.10

2
src/go.sum Normal file
View File

@ -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=

View File

@ -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()
}
}