grim/hgkeeper

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

Reviewed at https://reviews.imfreedom.org/r/2949/
package commands
import (
"fmt"
"os"
"github.com/gliderlabs/ssh"
"go.uber.org/zap"
"keep.imfreedom.org/grim/hgkeeper/access"
)
type Remove struct {
repoPath string
repoName string
}
func NewRemove(repoPath, repoName string) Command {
return &Remove{
repoPath: repoPath,
repoName: repoName,
}
}
func (i *Remove) Run(session ssh.Session, username string) error {
if !access.CanRemove(username, "/"+i.repoName) {
return fmt.Errorf("access denied")
}
err := error(nil)
if access.IsExistingRepo(i.repoName) {
err = i.removePath(session)
} else {
err = fmt.Errorf("repository not found: %s", i.repoName)
}
if err != nil {
sendResponse(err.Error(), session)
}
return err
}
func (i *Remove) removePath(session ssh.Session) error {
if i.repoName == access.AdminRepo() {
return fmt.Errorf("repository protected: %s", i.repoName)
}
err := os.RemoveAll(i.repoPath)
if err == nil {
sendResponse("repository removed: "+i.repoName, session)
zap.S().Info("refreshing access control for removed repo")
return access.Refresh()
}
return err
}
func sendResponse(content string, session ssh.Session) {
session.Write([]byte(content))
session.Write([]byte("\n"))
}