grim/hgkeeper

closing merged branch
styling
2019-04-18, Gary Kramlich
110f88f716e7
closing merged branch
package hg
import (
"fmt"
"os"
"os/exec"
)
type Repository struct {
path string
exists bool
}
func NewRepository(path string) (*Repository, error) {
exists := true
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
exists = false
} else {
return nil, err
}
}
return &Repository{
path: path,
exists: exists,
}, nil
}
func (r *Repository) Path() string {
return r.path
}
func (r *Repository) Exists() bool {
return r.exists
}
func (r *Repository) Init(mode os.FileMode) error {
if r.exists {
return fmt.Errorf("repo %s already exists", r.path)
}
err := os.MkdirAll(r.path, mode)
if err != nil {
return err
}
cmd := exec.Command("hg", "init", r.path)
cmd.Env = append(os.Environ(), "HGRCPATH=/dev/null")
return cmd.Run()
}
func (r *Repository) Serve() error {
if !r.exists {
return fmt.Errorf("repo %s does not exist", r.path)
}
cmd := exec.Command("hg", "-R", r.path, "serve", "--stdio")
cmd.Env = append(os.Environ(), "HGRCPATH=/dev/null")
return cmd.Run()
}