grim/hgkeeper
Clone
Summary
Browse
Changes
Graph
Tell go mod we support golang 1.17 now
2021-12-05, Gary Kramlich
6d4da6cbaf26
Tell go mod we support golang 1.17 now
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
)
}
}