gitpb/shell.go

122 lines
2.6 KiB
Go

package gitpb
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"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.GoPath, 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) IsValid() 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) mtime(filename string) (time.Time, error) {
pathf := filepath.Join(repo.FullPath, filename)
statf, err := os.Stat(pathf)
if err == nil {
return statf.ModTime(), nil
}
log.Log(GITPB, "mtime() os.Stat() error", pathf, err)
return time.Now(), err
}