grim/hgkeeper
Clone
Summary
Browse
Changes
Graph
Replace ioutils as it is deprecated since go 1.17
12 months ago, aklitzing
4ec99415f32b
Replace ioutils as it is deprecated since go 1.17
Reviewed at https://reviews.imfreedom.org/r/2444/
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
);
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
)
}
}