grim/hgkeeper

dc46af0b583b
Spit out a warning if we find a duplicated key.

We're going to continue the existing behavior of overwriting the keys to not
break potential set ups for the time being.

Fixes HGKEEPER-22
package once
import (
"fmt"
"os"
"path/filepath"
"keep.imfreedom.org/grim/hgkeeper/access"
"keep.imfreedom.org/grim/hgkeeper/globals"
"keep.imfreedom.org/grim/hgkeeper/hg"
)
type Command struct {
User string `kong:"arg,help='The username who is trying to access the repositories'"`
}
func (c *Command) serve(reposPath, repoPath string) error {
if !access.CanRead(c.User, "/"+repoPath) {
return fmt.Errorf("repository %q not found", repoPath)
}
writeable := access.CanWrite(c.User, "/"+repoPath)
hgcmd := hg.Serve(filepath.Join(reposPath, repoPath), writeable)
return hgcmd.ExecPiped(repoPath, c.User, os.Stdin, os.Stdout, os.Stderr)
}
func (c *Command) init(reposPath, repoPath string) error {
if !access.CanInit(c.User, "/"+repoPath) {
return fmt.Errorf("access denied")
}
hgcmd := hg.Init(filepath.Join(reposPath, repoPath))
return hgcmd.ExecPiped(repoPath, c.User, os.Stdin, os.Stdout, os.Stderr)
}
func (c *Command) Run(g *globals.Globals) error {
if err := access.Setup(g.ReposPath, g.AdminRepo); err != nil {
return err
}
defer access.Teardown()
originalCommand, found := os.LookupEnv("SSH_ORIGINAL_COMMAND")
if !found {
return fmt.Errorf("client did not specify a command")
}
cmd, args, err := hg.ParseCommandArguments(originalCommand)
if err != nil {
return err
}
switch cmd {
case "hg serve":
return c.serve(g.ReposPath, args.Hg.Repo)
case "hg init <repo>":
return c.init(g.ReposPath, args.Hg.Init.Repo)
default:
return fmt.Errorf("unsupported command %q", cmd)
}
}