grim/hgkeeper

ebd2fc7bc10c
Parents 192d0f174bf4
Children 5335e07264c8
Overhaul the way the hgkeeper repo is created
--- a/hg/hg.go Thu Apr 18 23:15:44 2019 +0000
+++ b/hg/hg.go Sat Apr 27 02:03:00 2019 -0500
@@ -35,6 +35,12 @@
return r.exists
}
+func (r *Repository) run(args ...string) ([]byte, error) {
+ cmd := exec.Command("hg", args...)
+ cmd.Env = append(os.Environ(), "HGRCPATH=/dev/null")
+ return cmd.CombinedOutput()
+}
+
func (r *Repository) Init(mode os.FileMode) error {
if r.exists {
return fmt.Errorf("repo %s already exists", r.path)
@@ -45,9 +51,9 @@
return err
}
- cmd := exec.Command("hg", "init", r.path)
- cmd.Env = append(os.Environ(), "HGRCPATH=/dev/null")
- return cmd.Run()
+ _, err = r.run("init", r.path)
+
+ return err
}
func (r *Repository) Serve() error {
@@ -55,7 +61,20 @@
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()
+ _, err := r.run("-R", r.path, "serve", "--stdio")
+ return err
}
+
+func (r *Repository) Add(files ...string) error {
+ args := append([]string{"add", "--cwd", r.path}, files...)
+
+ _, err := r.run(args...)
+
+ return err
+}
+
+func (r *Repository) Commit(username, message string) error {
+ _, err := r.run("commit", "--cwd", r.path, "--config", "ui.username="+username, "-m", message)
+
+ return err
+}
--- a/setup/command.go Thu Apr 18 23:15:44 2019 +0000
+++ b/setup/command.go Sat Apr 27 02:03:00 2019 -0500
@@ -1,11 +1,10 @@
package setup
import (
- "io/ioutil"
"os"
"path/filepath"
- "bitbucket.org/rw_grim/hgkeeper/hg"
+ "bitbucket.org/rw_grim/hgkeeper/setup/repo"
)
type Command struct {
@@ -15,18 +14,6 @@
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 *Command) Run(reposPath string) error {
c.adminRepoPath = filepath.Join(reposPath, c.AdminRepo)
@@ -57,17 +44,5 @@
}
func (c *Command) 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")
-
- return ioutil.WriteFile(hgrcPath, []byte(hgrc), 0644)
+ return repo.Create(c.adminRepoPath)
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/setup/repo/access.go Sat Apr 27 02:03:00 2019 -0500
@@ -0,0 +1,37 @@
+package repo
+
+import (
+ "io/ioutil"
+ "path/filepath"
+
+ "bitbucket.org/rw_grim/hgkeeper/hg"
+)
+
+var (
+ accessYaml = `---
+global:
+ init:
+ - admins
+ read:
+ - public
+groups:
+ admins:
+patterns:
+ hgkeeper:
+ read:
+ - admins
+ write:
+ - admins
+`
+)
+
+func addAccessYaml(r *hg.Repository) error {
+ filename := "access.yaml"
+ accessYamlPath := filepath.Join(r.Path(), filename)
+ err := ioutil.WriteFile(accessYamlPath, []byte(accessYaml), 0644)
+ if err != nil {
+ return err
+ }
+
+ return r.Add(filename)
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/setup/repo/commit.go Sat Apr 27 02:03:00 2019 -0500
@@ -0,0 +1,13 @@
+package repo
+
+import (
+ "bitbucket.org/rw_grim/hgkeeper/hg"
+)
+
+var (
+ hgUsername = "hgkeeper"
+)
+
+func commit(r *hg.Repository) error {
+ return r.Commit(hgUsername, "Inital revision")
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/setup/repo/hgrc.go Sat Apr 27 02:03:00 2019 -0500
@@ -0,0 +1,26 @@
+package repo
+
+import (
+ "io/ioutil"
+ "path/filepath"
+
+ "bitbucket.org/rw_grim/hgkeeper/hg"
+)
+
+var (
+ hgrc = `# 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 addHGRC(r *hg.Repository) error {
+ hgrcPath := filepath.Join(r.Path(), ".hg", "hgrc")
+
+ return ioutil.WriteFile(hgrcPath, []byte(hgrc), 0644)
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/setup/repo/readme.go Sat Apr 27 02:03:00 2019 -0500
@@ -0,0 +1,33 @@
+package repo
+
+import (
+ "io/ioutil"
+ "path/filepath"
+
+ "bitbucket.org/rw_grim/hgkeeper/hg"
+)
+
+var (
+ readmeMd = `# 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.
+`
+)
+
+func addReadmeMd(r *hg.Repository) error {
+ filename := "README.md"
+ readmePath := filepath.Join(r.Path(), filename)
+ err := ioutil.WriteFile(readmePath, []byte(readmeMd), 0644)
+ if err != nil {
+ return err
+ }
+
+ return r.Add(filename)
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/setup/repo/repo.go Sat Apr 27 02:03:00 2019 -0500
@@ -0,0 +1,35 @@
+package repo
+
+import (
+ "bitbucket.org/rw_grim/hgkeeper/hg"
+)
+
+type setupFunc func(*hg.Repository) error
+
+func Create(path string) error {
+ repo, err := hg.NewRepository(path)
+ if err != nil {
+ return err
+ }
+
+ err = repo.Init(0700)
+ if err != nil {
+ return err
+ }
+
+ setupFuncs := []setupFunc{
+ addHGRC,
+ addAccessYaml,
+ addReadmeMd,
+ commit,
+ }
+
+ for _, f := range setupFuncs {
+ err := f(repo)
+ if err != nil {
+ return err
+ }
+ }
+
+ return nil
+}
--- a/ssh/server.go Thu Apr 18 23:15:44 2019 +0000
+++ b/ssh/server.go Sat Apr 27 02:03:00 2019 -0500
@@ -54,6 +54,7 @@
}
func (s *Server) publicKeyCallback(meta ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
+ log.Debugf("key: %s\n", key.Marshal())
return nil, nil
}