summaryrefslogtreecommitdiff
path: root/shell.go
blob: c9ea1ed1542b8e7ec580cb4e2250511b4c26bcfb (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
package gitpb

import (
	"errors"
	"fmt"
	"os"
	"path/filepath"
	"strings"

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

// execute something with the working directory
// set to the FullPath
func (repo *Repo) Run(cmd []string) cmd.Status {
	result := shell.PathRun(repo.FullPath, cmd)
	output := strings.Join(result.Stdout, "\n")
	if result.Error != nil {
		log.Warn("cmd:", cmd)
		log.Warn("ouptput:", output)
		log.Warn("failed with error:", result.Error)
	}
	return result
}

func (repo *Repo) RunQuiet(cmd []string) cmd.Status {
	result := shell.PathRunQuiet(repo.FullPath, cmd)
	return result
}

func (repo *Repo) RunEcho(cmd []string) cmd.Status {
	result := shell.PathRunQuiet(repo.FullPath, cmd)
	log.Info("cmd:", repo.FullPath, cmd)
	log.Warn("cmd.Exit:", result.Exit, "cmd.Error:", result.Error)
	for _, line := range result.Stdout {
		log.Info("STDOUT:", line)
	}
	for _, line := range result.Stderr {
		log.Info("STDERR:", line)
	}
	return result
}

func (repo *Repo) RunRealtime(cmd []string) cmd.Status {
	return shell.PathRunRealtime(repo.GetFullPath(), cmd)
}

// error if result.Error or if result.Exit != 0
func (repo *Repo) RunStrict(cmd []string) error {
	return repo.StrictRun(cmd)
}

func (repo *Repo) StrictRun(cmd []string) error {
	result := repo.RunQuiet(cmd)
	if result.Error != nil {
		log.Warn(repo.GetGoPath(), cmd, "wow. golang is cool. an os.Error:", result.Error)
		return result.Error
	}
	if result.Exit != 0 {
		log.Warn(cmd, "failed with", result.Exit)
		return errors.New(fmt.Sprint(cmd, "failed with", result.Exit))
	}
	return nil
}

func (repo *Repo) Exists(filename string) bool {
	if repo == nil {
		return false
	}
	testf := filepath.Join(repo.FullPath, filename)
	_, err := os.Stat(testf)
	if err != nil {
		return false
	}
	return true
}

func (repo *Repo) IsValidDir() bool {
	if repo == nil {
		return false
	}
	if !repo.IsGitDirectory() {
		return false
	}
	return true
}

func (repo *Repo) ReadFile(fname string) ([]byte, error) {
	fullname := filepath.Join(repo.FullPath, fname)
	return os.ReadFile(fullname)
}

func (repo *Repo) IsGitDirectory() bool {
	gitdir := filepath.Join(repo.FullPath, ".git")
	info, err := os.Stat(gitdir)
	if err != nil {
		return false
	}
	return info.IsDir()
}

func (repo *Repo) IsDirectory() bool {
	info, err := os.Stat(repo.FullPath)
	if err != nil {
		return false
	}
	return info.IsDir()
}

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