grim/hgkeeper

f82b7c397849
Move from our custom yaml access setup to casbin
package setup
import (
"fmt"
"io/ioutil"
"os/exec"
"path/filepath"
log "github.com/sirupsen/logrus"
"bitbucket.org/rw_grim/hgkeeper/globals"
"bitbucket.org/rw_grim/hgkeeper/hg"
)
type Command struct{}
var (
accessYmlFilename = "access.yml"
accessYmlData = `---
global:
init:
- admins
read:
- public
groups:
admins:
patterns:
hgkeeper:
read:
- admins
write:
- admins
`
readmeMdFilename = "README.md"
readmeMdData = `# hgkeeper
This repository is used to manage keys for hgkeeper. This is done by
organizing ssh public keys in the keys directory.
# keys/
Files in the keys directory should be named after the user the belong to and
contain the ssh public keys for that user. The name of the file is used in
access.yaml as the users/group name.
`
hgUsername = "hgkeeper"
commitMessage = "initial revision"
hgrcData = `# this file was created by hgkeeper, do not modify
[extensions]
hgext.purge =
[hooks]
changegroup.aaba = hg update -C default > /dev/null
changegroup.aaca = hg purge --all > /dev/null
changegroup.aada = hgkeeper refresh-auth
`
)
func runCmd(cmd *exec.Cmd) error {
output, err := cmd.CombinedOutput()
if len(output) > 0 {
fmt.Printf("%s\n", output)
}
return err
}
func (c *Command) createAdminRepo(reposPath, adminRepo string) error {
log.Errorf("reposPath: %q", reposPath)
log.Errorf("adminRepo: %q", adminRepo)
path := filepath.Join(reposPath, adminRepo)
log.Infof("creating %q", path)
// create the admin repo
if err := runCmd(hg.Init(path)); err != nil {
return err
}
// create our hgrc
hgrcPath := filepath.Join(path, ".hg", "hgrc")
if err := ioutil.WriteFile(hgrcPath, []byte(hgrcData), 0644); err != nil {
return err
}
// create our access.yml
accessYmlPath := filepath.Join(path, accessYmlFilename)
if err := ioutil.WriteFile(accessYmlPath, []byte(accessYmlData), 0644); err != nil {
return err
}
// create our readme.md
readmeMdPath := filepath.Join(path, readmeMdFilename)
if err := ioutil.WriteFile(readmeMdPath, []byte(readmeMdData), 0644); err != nil {
return err
}
// add our files
if err := runCmd(hg.Add(path, accessYmlFilename, readmeMdFilename)); err != nil {
return err
}
// commit our changes
if err := runCmd(hg.Commit(path, hgUsername, commitMessage)); err != nil {
return err
}
return nil
}
func (c *Command) Run(g *globals.Globals) error {
if err := c.createAdminRepo(g.ReposPath, g.AdminRepo); err != nil {
// do clean up
return err
}
return nil
}