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

import (
	"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)
	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) RunRealtime(cmd []string) cmd.Status {
	return shell.PathRunRealtime(repo.GetFullPath(), cmd)
}

// for now, even check cmd.Exit
func (repo *Repo) strictRun(cmd []string) (bool, error) {
	result := repo.RunQuiet(cmd)
	if result.Error != nil {
		log.Warn("go mod init failed err:", result.Error)
		return false, result.Error
	}
	if result.Exit != 0 {
		log.Warn("go mod init exit =", result.Exit)
		return false, result.Error
	}
	return true, nil
}

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

func (repo *Repo) IsValid() bool {
	if !repo.IsDirectory() {
		return false
	}
	return true
}

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