From 475e72018e18757026e50c1f80d71b630461b7ec Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Thu, 11 Sep 2025 19:08:15 -0500 Subject: fixes for wierd package names --- Makefile | 2 +- argv.go | 1 + build.go | 100 +----------------------------------- control.read.go | 145 +++++++++++++++++++++++++++++++++++++++++++++++++++++ control.write.go | 106 +++++++++++++++++++++++++++++++++++++++ main.go | 7 +++ readControlFile.go | 145 ----------------------------------------------------- 7 files changed, 262 insertions(+), 244 deletions(-) create mode 100644 control.read.go create mode 100644 control.write.go delete mode 100644 readControlFile.go diff --git a/Makefile b/Makefile index 1e8ecd6..266aebb 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ build: goimports vet GO111MODULE="off" go build -v \ -ldflags "-X main.VERSION=${VERSION} -X main.DATE=${DATE} -X gui.GUIVERSION=${VERSION}" -install: +install: goimports GO111MODULE="off" go install -v \ -ldflags "-X main.VERSION=${VERSION} -X main.DATE=${DATE} -X gui.GUIVERSION=${VERSION}" diff --git a/argv.go b/argv.go index ef20d8e..d951886 100644 --- a/argv.go +++ b/argv.go @@ -15,6 +15,7 @@ type args struct { Commit *EmptyCmd `arg:"subcommand:commit" help:"'git commit' but errors out if on wrong branch"` Show *EmptyCmd `arg:"subcommand:show" help:"show what would be done"` Gui *EmptyCmd `arg:"subcommand:gui" help:"open the gui"` + Dump *EmptyCmd `arg:"subcommand:dump" help:"dump out the future control file"` Ldflags []string `arg:"--ldflags" help:"flags to pass to go build"` OutDir string `arg:"--dir" help:"write .deb file into this directory"` Release bool `arg:"--release" help:"build a release from the last git tag"` diff --git a/build.go b/build.go index 1fd1258..5030586 100644 --- a/build.go +++ b/build.go @@ -5,11 +5,8 @@ import ( "fmt" "os" "path/filepath" - "strconv" - "strings" "time" - "github.com/go-cmd/cmd" "go.wit.com/lib/gui/shell" "go.wit.com/lib/protobuf/gitpb" "go.wit.com/log" @@ -167,14 +164,14 @@ func buildPackage(repo *gitpb.Repo) (bool, error) { // probably deprecate this log.Info("REPO GO_DEB_CUSTOM=true means binary is not copied") } else { - fname := repo.Control["Package"] + _, fname := filepath.Split(repo.GetFullPath()) cmd := []string{"cp", fname, "files/usr/bin"} log.Info("REPO FILENAME cp", cmd) if err := repo.RunVerbose(cmd); err != nil { log.Warn("cp failed") return false, err } - cmd = []string{"strip", "files/usr/bin/" + filename} + cmd = []string{"strip", "files/usr/bin/" + fname} if err := repo.RunVerbose(cmd); err != nil { log.Warn("strip failed") return false, err @@ -245,96 +242,3 @@ func buildPackage(repo *gitpb.Repo) (bool, error) { } return true, nil } - -func writeDebianControlFile(repo *gitpb.Repo) bool { - filename := "files/DEBIAN/control" - cf, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) - if err != nil { - log.Info("open control file failed", err) - return false - } - fmt.Fprintln(cf, "Package:", repo.Control["Package"]) // c.Package.String()) - fmt.Fprintln(cf, "Source:", repo.Control["Source"]) // c.Source.String()) - fmt.Fprintln(cf, "Version:", repo.Control["Version"]) - if repo.Control["Architecture"] == "" { - repo.Control["Architecture"] = "amd64" - } - fmt.Fprintln(cf, "Architecture:", repo.Control["Architecture"]) // c.Architecture.String()) - - writeControlVar(cf, repo, "Depends") - writeControlVar(cf, repo, "Build-Depends") - writeControlVar(cf, repo, "Maintainer") - writeControlVar(cf, repo, "Packager") - writeControlVar(cf, repo, "GoPath") - writeControlVar(cf, repo, "URL") - writeControlVar(cf, repo, "Conflicts") - - stamp := time.Now().UTC().Format("2006/01/02 15:04:05 UTC") - // update to now now despite what the GUI is showing - fmt.Fprintln(cf, "Package-Build-Date:", stamp) - fmt.Fprintln(cf, "Git-Tag-Date:", "todo: get from repo") - - desc, _ := repo.Control["Description"] // c.Description.String() - parts := strings.Split(desc, "\n") - fmt.Fprintln(cf, "Description:", strings.Join(parts, "\n ")) - - log.Info("file written as:", filename) - return true -} - -func writeControlVar(f *os.File, repo *gitpb.Repo, varname string) { - val, _ := repo.Control[varname] - if val == "" { - return - } - fmt.Fprintln(f, varname+":", val) -} - -// try to guess or figure out the config file values -// if there is not a control file -func computeControlValues(repo *gitpb.Repo) bool { - if repo.Control["Package"] == "" { - // get the package name from the repo name - path := repo.Control["pathL"] // c.pathL.String() - parts := strings.Split(path, "/") - name := parts[len(parts)-1] - repo.Control["Package"] = name - } - if repo.Control["Source"] == "" { - repo.Control["Source"] = repo.Control["Package"] - } - if repo.Control["Build-Depends"] == "" { - repo.Control["Build-Depends"] = repo.Control["golang"] - } - if repo.Control["Recommends"] == "" { - repo.Control["Recommends"] = repo.Control["go-gui-toolkits"] - } - if repo.Control["Maintainer"] == "" { - repo.Control["Maintainer"] = "todo: get from ENV" - } - if repo.Control["Description"] == "" { - repo.Control["Description"] = "todo: put URL here" - } - return true -} - -// stamp := time.Now().UTC().Format("2006/01/02 15:04:05 UTC") - -func getDateStamp(tag string) string { - var r cmd.Status - r = me.repo.Run([]string{"git", "log", "-1", "--format=%at", tag}) - - out := strings.Join(r.Stdout, "\n") - out = strings.TrimSpace(out) - - // Convert the string to an integer - gitTagTimestampInt, err := strconv.ParseInt(out, 10, 64) - if err != nil { - fmt.Println("Error converting timestamp:", err) - return "git tag " + tag + " unknown" - } - - // Parse the Unix timestamp into a time.Time object - gitTagDate := time.Unix(gitTagTimestampInt, 0) - return gitTagDate.UTC().Format("2006-01-02_15:04:05_UTC") // close to RFC3339 -} diff --git a/control.read.go b/control.read.go new file mode 100644 index 0000000..d6a4c0d --- /dev/null +++ b/control.read.go @@ -0,0 +1,145 @@ +package main + +import ( + "bufio" + "os" + "path/filepath" + "strings" + "unicode" + + "go.wit.com/lib/protobuf/gitpb" + "go.wit.com/log" +) + +func trimNonNumericPrefix(s string) string { + // Find the index of the first character that IS a digit. + firstDigitIndex := strings.IndexFunc(s, unicode.IsDigit) + + // If no digit is found, IndexFunc returns -1. + // In this case, the result should be an empty string. + if firstDigitIndex == -1 { + return "" + } + + // Return the substring starting from the first digit. + return s[firstDigitIndex:] +} + +// readGitConfig reads and parses the control file +func readControlFile(repo *gitpb.Repo) error { + pairs := make(map[string]string) + var key string + + file, err := os.Open("control") + if err != nil { + log.Warn("readControlFile() could not find the file") + // return errors.New("'control': file not found") + // if this happens, make up a fake control file + pairs["Architecture"] = "amd64" // TODO: figure this out + pairs["Recommends"] = "" + pairs["Source"] = "notsure" + if me.repo == nil { + pairs["Description"] = "put something here" + } else { + pairs["Description"] = me.repo.GetGoPath() + } + if repo.Control == nil { + repo.Control = make(map[string]string) + } + for key, value := range pairs { + repo.Control[key] = value + } + if os.Getenv("GIT_AUTHOR_NAME") != "" { + author := log.Sprintf("%s <%s>", os.Getenv("GIT_AUTHOR_NAME"), os.Getenv("GIT_AUTHOR_EMAIL")) + repo.Control["Packager"] = author + } + _, fname := filepath.Split(repo.GetFullPath()) + repo.Control["Package"] = fname + repo.Control["Version"] = trimNonNumericPrefix(repo.GetCurrentVersion()) + repo.Control["URL"] = repo.URL + return nil + } + defer file.Close() + + pairs["Version"] = trimNonNumericPrefix(repo.GetCurrentVersion()) + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := scanner.Text() + + // Skip empty lines and comments + if line == "" || strings.HasPrefix(line, "#") || strings.HasPrefix(line, ";") { + continue + } + + // if line starts with a space, it's part of the last key + if strings.HasPrefix(line, " ") { + pairs[key] = pairs[key] + "\n" + strings.TrimSpace(line) + continue + } + + partsNew := strings.SplitN(line, ":", 2) + if len(partsNew) < 2 { + log.Warn("error on line:", line) + continue + } + + key = strings.TrimSpace(partsNew[0]) + value := strings.TrimSpace(partsNew[1]) + pairs[key] = value + } + if repo.Control == nil { + repo.Control = make(map[string]string) + } + for key, value := range pairs { + repo.Control[key] = value + /* + switch key { + case "Source": + c.Source.SetText(value) + case "Build-Depends": + c.BuildDepends.SetText(value) + case "Description": + c.Description.SetText(value) + case "Maintainer": + c.Maintainer.SetText(value) + case "Packager": + c.Packager.SetText(value) + case "GoPath": + c.GoPath.SetText(value) + case "URL": + c.URL.SetText(value) + case "Depends": + c.Depends.SetText(value) + case "Recommends": + c.Recommends.SetText(value) + case "Conflicts": + c.Conflicts.SetText(value) + case "Version": + c.Version.SetText(value) + case "Package": + c.Package.SetText(value) + // if c.Package.String() != value { + // log.Warn("not sure what to do with Package", c.Package.String(), value) + // } + case "Architecture": + // todo: add logic to find OS arch + if c.Architecture.String() != value { + log.Warn("attempting to set arch to", value) + c.Architecture.SetText(value) + + } + default: + log.Warn("the 'control' file has a value I don't know about") + log.Warn("error unknown key", key, "value:", value) + } + */ + } + pairs["Architecture"] = "amd64" // TODO: figure this out + + if err := scanner.Err(); err != nil { + return err + } + + return nil +} diff --git a/control.write.go b/control.write.go new file mode 100644 index 0000000..b2c31ba --- /dev/null +++ b/control.write.go @@ -0,0 +1,106 @@ +package main + +import ( + "fmt" + "os" + "strconv" + "strings" + "time" + + "github.com/go-cmd/cmd" + "go.wit.com/lib/protobuf/gitpb" + "go.wit.com/log" +) + +func writeDebianControlFile(repo *gitpb.Repo) bool { + filename := "files/DEBIAN/control" + cf, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) + if err != nil { + log.Info("open control file failed", err) + return false + } + fmt.Fprintln(cf, "Package:", repo.Control["Package"]) // c.Package.String()) + fmt.Fprintln(cf, "Source:", repo.Control["Source"]) // c.Source.String()) + fmt.Fprintln(cf, "Version:", repo.Control["Version"]) + if repo.Control["Architecture"] == "" { + repo.Control["Architecture"] = "amd64" + } + fmt.Fprintln(cf, "Architecture:", repo.Control["Architecture"]) // c.Architecture.String()) + + writeControlVar(cf, repo, "Depends") + writeControlVar(cf, repo, "Build-Depends") + writeControlVar(cf, repo, "Maintainer") + writeControlVar(cf, repo, "Packager") + writeControlVar(cf, repo, "GoPath") + writeControlVar(cf, repo, "URL") + writeControlVar(cf, repo, "Conflicts") + + stamp := time.Now().UTC().Format("2006/01/02 15:04:05 UTC") + // update to now now despite what the GUI is showing + fmt.Fprintln(cf, "Package-Build-Date:", stamp) + fmt.Fprintln(cf, "Git-Tag-Date:", "todo: get from repo") + + desc, _ := repo.Control["Description"] // c.Description.String() + parts := strings.Split(desc, "\n") + fmt.Fprintln(cf, "Description:", strings.Join(parts, "\n ")) + + log.Info("file written as:", filename) + return true +} + +func writeControlVar(f *os.File, repo *gitpb.Repo, varname string) { + val, _ := repo.Control[varname] + if val == "" { + return + } + fmt.Fprintln(f, varname+":", val) +} + +// try to guess or figure out the config file values +// if there is not a control file +func computeControlValues(repo *gitpb.Repo) bool { + if repo.Control["Package"] == "" { + // get the package name from the repo name + path := repo.Control["pathL"] // c.pathL.String() + parts := strings.Split(path, "/") + name := parts[len(parts)-1] + repo.Control["Package"] = name + } + if repo.Control["Source"] == "" { + repo.Control["Source"] = repo.Control["Package"] + } + if repo.Control["Build-Depends"] == "" { + repo.Control["Build-Depends"] = repo.Control["golang"] + } + if repo.Control["Recommends"] == "" { + repo.Control["Recommends"] = repo.Control["go-gui-toolkits"] + } + if repo.Control["Maintainer"] == "" { + repo.Control["Maintainer"] = "todo: get from ENV" + } + if repo.Control["Description"] == "" { + repo.Control["Description"] = "todo: put URL here" + } + return true +} + +// stamp := time.Now().UTC().Format("2006/01/02 15:04:05 UTC") + +func getDateStamp(tag string) string { + var r cmd.Status + r = me.repo.Run([]string{"git", "log", "-1", "--format=%at", tag}) + + out := strings.Join(r.Stdout, "\n") + out = strings.TrimSpace(out) + + // Convert the string to an integer + gitTagTimestampInt, err := strconv.ParseInt(out, 10, 64) + if err != nil { + fmt.Println("Error converting timestamp:", err) + return "git tag " + tag + " unknown" + } + + // Parse the Unix timestamp into a time.Time object + gitTagDate := time.Unix(gitTagTimestampInt, 0) + return gitTagDate.UTC().Format("2006-01-02_15:04:05_UTC") // close to RFC3339 +} diff --git a/main.go b/main.go index bb574ec..e705c6c 100644 --- a/main.go +++ b/main.go @@ -66,6 +66,13 @@ func main() { } computeControlValues(me.repo) + if argv.Dump != nil { + for v := range me.repo.Control { + log.Infof("CONTROL: %s: %s\n", v, me.repo.Control[v]) + } + okExit("") + } + if argv.Gui != nil { // only load teh toolkit if you get this far me.myGui.Start() // loads the GUI toolkit diff --git a/readControlFile.go b/readControlFile.go deleted file mode 100644 index d6a4c0d..0000000 --- a/readControlFile.go +++ /dev/null @@ -1,145 +0,0 @@ -package main - -import ( - "bufio" - "os" - "path/filepath" - "strings" - "unicode" - - "go.wit.com/lib/protobuf/gitpb" - "go.wit.com/log" -) - -func trimNonNumericPrefix(s string) string { - // Find the index of the first character that IS a digit. - firstDigitIndex := strings.IndexFunc(s, unicode.IsDigit) - - // If no digit is found, IndexFunc returns -1. - // In this case, the result should be an empty string. - if firstDigitIndex == -1 { - return "" - } - - // Return the substring starting from the first digit. - return s[firstDigitIndex:] -} - -// readGitConfig reads and parses the control file -func readControlFile(repo *gitpb.Repo) error { - pairs := make(map[string]string) - var key string - - file, err := os.Open("control") - if err != nil { - log.Warn("readControlFile() could not find the file") - // return errors.New("'control': file not found") - // if this happens, make up a fake control file - pairs["Architecture"] = "amd64" // TODO: figure this out - pairs["Recommends"] = "" - pairs["Source"] = "notsure" - if me.repo == nil { - pairs["Description"] = "put something here" - } else { - pairs["Description"] = me.repo.GetGoPath() - } - if repo.Control == nil { - repo.Control = make(map[string]string) - } - for key, value := range pairs { - repo.Control[key] = value - } - if os.Getenv("GIT_AUTHOR_NAME") != "" { - author := log.Sprintf("%s <%s>", os.Getenv("GIT_AUTHOR_NAME"), os.Getenv("GIT_AUTHOR_EMAIL")) - repo.Control["Packager"] = author - } - _, fname := filepath.Split(repo.GetFullPath()) - repo.Control["Package"] = fname - repo.Control["Version"] = trimNonNumericPrefix(repo.GetCurrentVersion()) - repo.Control["URL"] = repo.URL - return nil - } - defer file.Close() - - pairs["Version"] = trimNonNumericPrefix(repo.GetCurrentVersion()) - - scanner := bufio.NewScanner(file) - for scanner.Scan() { - line := scanner.Text() - - // Skip empty lines and comments - if line == "" || strings.HasPrefix(line, "#") || strings.HasPrefix(line, ";") { - continue - } - - // if line starts with a space, it's part of the last key - if strings.HasPrefix(line, " ") { - pairs[key] = pairs[key] + "\n" + strings.TrimSpace(line) - continue - } - - partsNew := strings.SplitN(line, ":", 2) - if len(partsNew) < 2 { - log.Warn("error on line:", line) - continue - } - - key = strings.TrimSpace(partsNew[0]) - value := strings.TrimSpace(partsNew[1]) - pairs[key] = value - } - if repo.Control == nil { - repo.Control = make(map[string]string) - } - for key, value := range pairs { - repo.Control[key] = value - /* - switch key { - case "Source": - c.Source.SetText(value) - case "Build-Depends": - c.BuildDepends.SetText(value) - case "Description": - c.Description.SetText(value) - case "Maintainer": - c.Maintainer.SetText(value) - case "Packager": - c.Packager.SetText(value) - case "GoPath": - c.GoPath.SetText(value) - case "URL": - c.URL.SetText(value) - case "Depends": - c.Depends.SetText(value) - case "Recommends": - c.Recommends.SetText(value) - case "Conflicts": - c.Conflicts.SetText(value) - case "Version": - c.Version.SetText(value) - case "Package": - c.Package.SetText(value) - // if c.Package.String() != value { - // log.Warn("not sure what to do with Package", c.Package.String(), value) - // } - case "Architecture": - // todo: add logic to find OS arch - if c.Architecture.String() != value { - log.Warn("attempting to set arch to", value) - c.Architecture.SetText(value) - - } - default: - log.Warn("the 'control' file has a value I don't know about") - log.Warn("error unknown key", key, "value:", value) - } - */ - } - pairs["Architecture"] = "amd64" // TODO: figure this out - - if err := scanner.Err(); err != nil { - return err - } - - return nil -} -- cgit v1.2.3