2024-01-09 15:34:53 -06:00
|
|
|
// This is a simple example
|
|
|
|
package repostatus
|
|
|
|
|
2024-01-18 00:57:43 -06:00
|
|
|
import (
|
|
|
|
"errors"
|
2024-01-09 15:34:53 -06:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2024-01-13 00:43:26 -06:00
|
|
|
"os/user"
|
|
|
|
"path/filepath"
|
2024-01-18 00:57:43 -06:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2024-01-09 15:34:53 -06:00
|
|
|
|
|
|
|
"go.wit.com/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
func fullpath(repo string) string {
|
|
|
|
return "/home/jcarr/go/src/" + repo
|
|
|
|
}
|
|
|
|
|
|
|
|
func run(path string, thing string, cmdline string) string {
|
|
|
|
parts := strings.Split(cmdline, " ")
|
|
|
|
// Create the command
|
|
|
|
cmd := exec.Command(thing, parts...)
|
|
|
|
|
|
|
|
// Set the working directory
|
|
|
|
cmd.Dir = fullpath(path)
|
|
|
|
|
|
|
|
// Execute the command
|
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err, "cmd error'd out", parts)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp := string(output)
|
|
|
|
tmp = strings.TrimSpace(tmp)
|
|
|
|
|
|
|
|
// Print the output
|
|
|
|
log.Log(WARN, "run()", path, thing, cmdline, "=", tmp)
|
|
|
|
return tmp
|
|
|
|
}
|
|
|
|
|
2024-01-11 15:56:50 -06:00
|
|
|
// goes in one directory so it gets remote branch names
|
2024-01-09 15:34:53 -06:00
|
|
|
func listFiles(directory string) []string {
|
|
|
|
var files []string
|
|
|
|
fileInfo, err := os.ReadDir(directory)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range fileInfo {
|
2024-01-11 15:56:50 -06:00
|
|
|
if file.IsDir() {
|
|
|
|
dirname := file.Name()
|
|
|
|
newdir, _ := os.ReadDir(directory + "/" + dirname)
|
|
|
|
for _, file := range newdir {
|
2024-01-18 00:57:43 -06:00
|
|
|
if !file.IsDir() {
|
|
|
|
files = append(files, dirname+"/"+file.Name())
|
2024-01-11 15:56:50 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2024-01-09 15:34:53 -06:00
|
|
|
files = append(files, file.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return files
|
|
|
|
}
|
2024-01-09 19:35:05 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
// string handling examples that might be helpful for normalizeInt()
|
|
|
|
isAlpha := regexp.MustCompile(`^[A-Za-z]+$`).MatchString
|
|
|
|
|
|
|
|
for _, username := range []string{"userone", "user2", "user-three"} {
|
|
|
|
if !isAlpha(username) {
|
|
|
|
log.Log(GUI, "%q is not valid\n", username)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const alpha = "abcdefghijklmnopqrstuvwxyz"
|
|
|
|
|
|
|
|
func alphaOnly(s string) bool {
|
|
|
|
for _, char := range s {
|
|
|
|
if !strings.Contains(alpha, strings.ToLower(string(char))) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
func normalizeVersion(s string) string {
|
|
|
|
// reg, err := regexp.Compile("[^a-zA-Z0-9]+")
|
|
|
|
parts := strings.Split(s, "-")
|
2024-01-18 00:57:43 -06:00
|
|
|
if len(parts) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
2024-01-09 19:35:05 -06:00
|
|
|
reg, err := regexp.Compile("[^0-9.]+")
|
|
|
|
if err != nil {
|
|
|
|
log.Log(WARN, "normalizeVersion() regexp.Compile() ERROR =", err)
|
|
|
|
return parts[0]
|
|
|
|
}
|
|
|
|
clean := reg.ReplaceAllString(parts[0], "")
|
|
|
|
log.Log(WARN, "normalizeVersion() s =", clean)
|
|
|
|
return clean
|
|
|
|
}
|
2024-01-10 00:15:27 -06:00
|
|
|
|
|
|
|
func splitVersion(version string) (a, b, c string) {
|
|
|
|
tmp := normalizeVersion(version)
|
|
|
|
parts := strings.Split(tmp, ".")
|
|
|
|
switch len(parts) {
|
|
|
|
case 1:
|
|
|
|
return parts[0], "", ""
|
|
|
|
case 2:
|
|
|
|
return parts[0], parts[1], ""
|
|
|
|
default:
|
|
|
|
return parts[0], parts[1], parts[2]
|
|
|
|
}
|
|
|
|
}
|
2024-01-10 18:18:01 -06:00
|
|
|
|
2024-01-15 17:03:06 -06:00
|
|
|
// temp hack. fix this
|
2024-01-11 15:56:50 -06:00
|
|
|
func runCmd(path string, parts []string) (error, bool, string) {
|
2024-01-15 08:12:04 -06:00
|
|
|
fulldir := fullpath(path)
|
|
|
|
return RunCmd(fulldir, parts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RunCmd(workingpath string, parts []string) (error, bool, string) {
|
2024-01-10 18:18:01 -06:00
|
|
|
if len(parts) == 0 {
|
|
|
|
log.Warn("command line was empty")
|
2024-01-11 15:56:50 -06:00
|
|
|
return errors.New("empty"), false, ""
|
2024-01-10 18:18:01 -06:00
|
|
|
}
|
|
|
|
if parts[0] == "" {
|
|
|
|
log.Warn("command line was empty")
|
2024-01-11 15:56:50 -06:00
|
|
|
return errors.New("empty"), false, ""
|
2024-01-10 18:18:01 -06:00
|
|
|
}
|
|
|
|
thing := parts[0]
|
|
|
|
parts = parts[1:]
|
|
|
|
|
2024-01-15 08:12:04 -06:00
|
|
|
log.Warn("working path =", workingpath, "thing =", thing, "cmdline =", parts)
|
2024-01-10 18:18:01 -06:00
|
|
|
// Create the command
|
|
|
|
cmd := exec.Command(thing, parts...)
|
|
|
|
|
|
|
|
// Set the working directory
|
2024-01-15 08:12:04 -06:00
|
|
|
cmd.Dir = workingpath
|
2024-01-10 18:18:01 -06:00
|
|
|
|
|
|
|
// Execute the command
|
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
log.Warn("output was", output)
|
|
|
|
log.Warn("cmd exited with error", err)
|
2024-01-11 15:56:50 -06:00
|
|
|
return err, false, string(output)
|
2024-01-10 18:18:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
tmp := string(output)
|
|
|
|
tmp = strings.TrimSpace(tmp)
|
|
|
|
|
|
|
|
// Print the output
|
2024-01-11 15:56:50 -06:00
|
|
|
return nil, true, tmp
|
2024-01-10 18:18:01 -06:00
|
|
|
}
|
2024-01-13 00:43:26 -06:00
|
|
|
|
|
|
|
// Set the path to the package
|
|
|
|
// Replace this with the actual path to the github.com/coredns/coredns directory
|
|
|
|
func getfiles(pathToPackage string) {
|
|
|
|
// List files in the directory
|
|
|
|
err := filepath.Walk(pathToPackage, nil) // compiles but crashes
|
|
|
|
if err == nil {
|
|
|
|
log.Warn("directory ok", pathToPackage)
|
|
|
|
} else {
|
|
|
|
log.Warn("directory wrong", pathToPackage)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsDirectory(path string) bool {
|
|
|
|
info, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return info.IsDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
func VerifyLocalGoRepo(gorepo string) bool {
|
|
|
|
// Get current user
|
|
|
|
usr, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err, "VerifyLocalGoRepo() thinks you should switch to Ultrix")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Form the path to the home Git directory
|
|
|
|
gitDir := filepath.Join(usr.HomeDir, "go/src/", gorepo, ".git")
|
|
|
|
|
|
|
|
log.Warn("go directory:", gitDir)
|
|
|
|
return IsDirectory(gitDir)
|
|
|
|
}
|