grim/hgkeeper

closing merged branch
styling
2019-04-18, Gary Kramlich
110f88f716e7
closing merged branch
package setup
import (
"io/ioutil"
"os"
"path/filepath"
"bitbucket.org/rw_grim/hgkeeper/hg"
)
type SetupCommand struct {
AdminRepo string `kong:"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
`
accessYaml = `---
global:
init:
- admins
read:
- public
groups:
admins:
patterns:
hgkeeper:
read:
- admins
`
)
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
}
hgrcPath := filepath.Join(c.adminRepoPath, ".hg", "hgrc")
err = ioutil.WriteFile(hgrcPath, []byte(hgrc), 0644)
if err != nil {
return err
}
accessYamlPath := filepath.Join(c.adminRepoPath, "access.yaml")
return ioutil.WriteFile(accessYamlPath, []byte(accessYaml), 0644)
}