grim/hgkeeper

Use Go 1.22 and update dependencies
default tip
2 months ago, aklitzing
f33f223bc8fe
Use Go 1.22 and update dependencies

Reviewed at https://reviews.imfreedom.org/r/2949/
package once
import (
"fmt"
"os"
"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(repoPath, repoName string) error {
if !access.CanRead(c.User, "/"+repoName) {
return fmt.Errorf("repository %q not found", repoName)
}
writeable := access.CanWrite(c.User, "/"+repoName)
hgcmd := hg.Serve(repoPath, writeable)
return hgcmd.ExecPiped(repoName, c.User, os.Stdin, os.Stdout, os.Stderr)
}
func (c *Command) init(repoPath, repoName string) error {
if !access.CanInit(c.User, "/"+repoName) {
return fmt.Errorf("access denied")
}
if access.IsInExistingRepo(repoName) {
return fmt.Errorf("repository in repository is forbidden")
}
hgcmd := hg.Init(repoPath)
return hgcmd.ExecPiped(repoName, c.User, os.Stdin, os.Stdout, os.Stderr)
}
func (c *Command) Run(g *globals.Globals) error {
if err := access.Setup(g.ReposPath, g.AdminRepo, g.LdapConfig); 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(access.NormalizeRepo(g.ReposPath, args.Hg.Repo))
case "hg init <repo>":
return c.init(access.NormalizeRepo(g.ReposPath, args.Hg.Init.Repo))
default:
return fmt.Errorf("unsupported command %q", cmd)
}
}