gitpb/shell.go

111 lines
2.3 KiB
Go
Raw Normal View History

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
}
2024-11-28 21:04:10 -06:00
func (repo *Repo) RunQuiet(cmd []string) cmd.Status {
result := shell.PathRunQuiet(repo.FullPath, cmd)
2024-12-10 01:48:49 -06:00
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)
2024-11-28 21:04:10 -06:00
}
return result
}
2024-12-01 22:23:38 -06:00
func (repo *Repo) RunRealtime(cmd []string) cmd.Status {
return shell.PathRunRealtime(repo.GetFullPath(), cmd)
}
2024-12-13 12:57:02 -06:00
// 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 {
2024-11-30 02:03:32 -06:00
result := repo.RunQuiet(cmd)
if result.Error != nil {
2024-12-17 06:37:14 -06:00
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 {
2024-12-01 22:37:11 -06:00
return false
}
testf := filepath.Join(repo.FullPath, filename)
_, err := os.Stat(testf)
if err != nil {
return false
}
return true
}
2024-12-01 11:44:20 -06:00
2024-12-17 01:15:31 -06:00
func (repo *Repo) IsValidDir() bool {
2024-12-13 16:18:25 -06:00
if repo == nil {
return false
}
if !repo.IsGitDirectory() {
2024-12-01 16:04:07 -06:00
return false
}
return true
}
2024-12-13 16:18:25 -06:00
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()
}
2024-12-01 11:44:20 -06:00
func (repo *Repo) IsDirectory() bool {
info, err := os.Stat(repo.FullPath)
if err != nil {
return false
}
return info.IsDir()
}