grim/hgkeeper

closing merged branch
termdumb
2019-04-18, Gary Kramlich
50c6ec130656
closing merged branch
package setup
import (
"io/ioutil"
"os"
"path/filepath"
"bitbucket.org/rw_grim/hgkeeper/hg"
)
type SetupCommand struct {
AdminRepo string `flag name:"admin-repo" default:"hgkeeper" help:"the name of the admin repo to create"`
adminRepoPath string
}
type setupFunc func(string) error
var (
hgrc = `# this file was created by hgkeeper, do not modify
[extensions]
hgext.purge =
[hooks]
changegroup.aaab = hg update -C default > /dev/null
changegroup.aaac = hg purge --all > /dev/null
changegroup.aaad = hgkeeper refresh-auth
`
)
func (c *SetupCommand) Run(reposPath string) error {
c.adminRepoPath = filepath.Join(reposPath, c.AdminRepo)
funcs := []setupFunc{
c.createReposDir,
c.createAdminRepo,
}
for _, f := range funcs {
err := f(reposPath)
if err != nil {
return err
}
}
return nil
}
func (c *SetupCommand) createReposDir(reposPath string) error {
if _, err := os.Stat(reposPath); os.IsNotExist(err) {
err := os.MkdirAll(reposPath, 0755)
if err != nil {
return err
}
}
return nil
}
func (c *SetupCommand) createAdminRepo(reposPath string) error {
repo, err := hg.NewRepository(c.adminRepoPath)
if err != nil {
return err
}
err = repo.Init(0700)
if err != nil {
return err
}
hgrc_path := filepath.Join(c.adminRepoPath, ".hg", "hgrc")
return ioutil.WriteFile(hgrc_path, []byte(hgrc), 0644)
}