summaryrefslogtreecommitdiff
path: root/unix.go
blob: 45a1cbccb923b9f6f8168772578cf4aeeee42705 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// This is a simple example
package repostatus

import 	(
	"os"
	"os/exec"
	"strings"
	"regexp"
	"errors"

	"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
}

// goes in one directory so it gets remote branch names
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 {
		if file.IsDir() {
			dirname := file.Name()
			newdir, _ := os.ReadDir(directory + "/" + dirname)
			for _, file := range newdir {
				if ! file.IsDir() {
					files = append(files, dirname + "/" + file.Name())
				}
			}
		} else {
			files = append(files, file.Name())
		}
	}

	return files
}

/*
// 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, "-")
	if len(parts) == 0 { return "" }
	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
}

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]
	}
}

func runCmd(path string, parts []string) (error, bool, string) {
	if len(parts) == 0 {
		log.Warn("command line was empty")
		return errors.New("empty"), false, ""
	}
	if parts[0] == "" {
		log.Warn("command line was empty")
		return errors.New("empty"), false, ""
	}
	thing := parts[0]
	parts = parts[1:]

	log.Warn("path =", path, "thing =", thing, "cmdline =", parts)
	// 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)
		log.Warn("output was", output)
		log.Warn("cmd exited with error", err)
		return err, false, string(output)
	}

	tmp := string(output)
	tmp = strings.TrimSpace(tmp)

	// Print the output
	return nil, true, tmp
}