summaryrefslogtreecommitdiff
path: root/unix.go
blob: 69cc02deed89354eea3caf346c8519d372ecd6fa (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package repostatus

import (
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"time"

	"github.com/go-cmd/cmd"
	"go.wit.com/lib/gui/shell"
	"go.wit.com/log"
)

func (rs *RepoStatus) Run(cmd []string) cmd.Status {
	path := rs.realPath.String()
	r := shell.PathRun(path, cmd)
	output := strings.Join(r.Stdout, "\n")
	if r.Error != nil {
		log.Log(WARN, "cmd:", cmd)
		log.Log(WARN, "ouptput:", output)
		log.Log(WARN, "failed with error:", r.Error)
	}
	return r
}

// Set the path to the package
func getfiles(pathToPackage string) {
	// List files in the directory
	err := filepath.Walk(pathToPackage, nil) // compiles but crashes
	if err == nil {
		log.Log(INFO, "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 (rs *RepoStatus) Exists(filename string) bool {
	if rs == nil {
		log.Warn("rs == nil for Exists()")
		panic(-1)
	}
	testf := filepath.Join(rs.Path(), filename)
	if Exists(testf) {
		return true
	}
	return false
}

func (rs *RepoStatus) mtime(filename string) (time.Time, error) {
	pathf := filepath.Join(rs.Path(), filename)
	statf, err := os.Stat(pathf)
	if err == nil {
		return statf.ModTime(), nil
	}
	log.Log(REPOWARN, "mtime() error", pathf, err)
	return time.Now(), err
}

// returns true if the file exists
func Exists(file string) bool {
	_, err := os.Stat(file)
	if err != nil {
		return false
	}
	return true
}

/*
func getDurationStamp(t time.Time) string {

	// Get the current time
	currentTime := time.Now()

	// Calculate the duration between t current time
	duration := currentTime.Sub(t)

	return formatDuration(duration)
}

func formatDuration(d time.Duration) string {
	seconds := int(d.Seconds()) % 60
	minutes := int(d.Minutes()) % 60
	hours := int(d.Hours()) % 24
	days := int(d.Hours()) / 24
	years := int(d.Hours()) / (24 * 365)

	result := ""
	if years > 0 {
		result += fmt.Sprintf("%dy ", years)
		return result
	}
	if days > 0 {
		result += fmt.Sprintf("%dd ", days)
		return result
	}
	if hours > 0 {
		result += fmt.Sprintf("%dh ", hours)
		return result
	}
	if minutes > 0 {
		result += fmt.Sprintf("%dm ", minutes)
		return result
	}
	if seconds > 0 {
		result += fmt.Sprintf("%ds", seconds)
	}
	return result
}
*/

func (rs *RepoStatus) XtermNohup(cmdline string) {
	shell.XtermCmd(rs.Path(), []string{cmdline})
}
func (rs *RepoStatus) Xterm(cmdline string) {
	shell.XtermCmd(rs.Path(), []string{cmdline})
}
func (rs *RepoStatus) XtermWait(cmdline string) {
	shell.XtermCmdWait(rs.Path(), []string{cmdline})
}

func (rs *RepoStatus) XtermBash(args []string) {
	var argsX = []string{"-geometry", "120x40"}
	tmp := strings.Join(args, " ") + ";bash"
	argsX = append(argsX, "-e", "bash", "-c", tmp)
	argsX = append(argsX, args...)
	log.Info("xterm cmd=", argsX)
	// set less to not exit on small diff's
	os.Setenv("LESS", "-+F -+X -R")
	cmd := exec.Command("xterm", argsX...)
	path := rs.realPath.String()
	cmd.Dir = path
	if err := cmd.Run(); err != nil {
		log.Log(WARN, "xterm.Run() failed")
		log.Log(WARN, "path =", path)
		log.Log(WARN, "cmd = xterm", argsX)
	} else {
		log.Log(WARN, "xterm.Run() worked")
		log.Log(WARN, "path =", path)
		log.Log(WARN, "cmd = xterm", argsX)
	}
}

func (rs *RepoStatus) DoAll(all [][]string) bool {
	for _, cmd := range all {
		log.Log(WARN, "doAll() RUNNING: cmd =", cmd)
		r := rs.Run(cmd)
		if r.Error != nil {
			log.Log(WARN, "doAll() err =", r.Error)
			log.Log(WARN, "doAll() out =", r.Stdout)
			return false
		}
	}
	return true
}