2024-01-23 19:24:01 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2024-01-29 23:07:57 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/thomiceli/opengist/internal/config"
|
|
|
|
"github.com/thomiceli/opengist/internal/db"
|
2024-01-23 19:24:01 +00:00
|
|
|
"github.com/thomiceli/opengist/internal/hooks"
|
|
|
|
"github.com/urfave/cli/v2"
|
2024-01-29 23:07:57 +00:00
|
|
|
"io"
|
2024-01-23 19:24:01 +00:00
|
|
|
"os"
|
2024-01-29 23:07:57 +00:00
|
|
|
"path/filepath"
|
2024-01-23 19:24:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var CmdHook = cli.Command{
|
|
|
|
Name: "hook",
|
|
|
|
Usage: "Run Git server hooks, used and should only be called by Opengist itself",
|
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
&CmdHookPreReceive,
|
|
|
|
&CmdHookPostReceive,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var CmdHookPreReceive = cli.Command{
|
|
|
|
Name: "pre-receive",
|
|
|
|
Usage: "Run Git server pre-receive hook for a repository",
|
|
|
|
Action: func(ctx *cli.Context) error {
|
2024-01-29 23:07:57 +00:00
|
|
|
initialize(ctx)
|
2024-01-23 19:24:01 +00:00
|
|
|
if err := hooks.PreReceive(os.Stdin, os.Stdout, os.Stderr); err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var CmdHookPostReceive = cli.Command{
|
|
|
|
Name: "post-receive",
|
|
|
|
Usage: "Run Git server post-receive hook for a repository",
|
|
|
|
Action: func(ctx *cli.Context) error {
|
2024-01-29 23:07:57 +00:00
|
|
|
initialize(ctx)
|
2024-01-23 19:24:01 +00:00
|
|
|
if err := hooks.PostReceive(os.Stdin, os.Stdout, os.Stderr); err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2024-01-29 23:07:57 +00:00
|
|
|
|
|
|
|
func initialize(ctx *cli.Context) {
|
|
|
|
if err := config.InitConfig(ctx.String("config"), io.Discard); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
config.InitLog()
|
|
|
|
|
|
|
|
if err := db.Setup(filepath.Join(config.GetHomeDir(), config.C.DBFilename), false); err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("Failed to initialize database in hooks")
|
|
|
|
}
|
|
|
|
}
|