0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-12-22 15:23:14 -05:00

[CLI] implement forgejo-cli actions (squash) restore --version

Refs: https://codeberg.org/forgejo/forgejo/issues/1134
This commit is contained in:
Earl Warren 2023-07-26 15:20:32 +02:00
parent 7e097b945c
commit e2000f5bfc
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
4 changed files with 30 additions and 27 deletions

View file

@ -35,7 +35,7 @@ func SubcmdActionsGenerateRunnerToken(ctx context.Context) *cli.Command {
return &cli.Command{ return &cli.Command{
Name: "generate-runner-token", Name: "generate-runner-token",
Usage: "Generate a new token for a runner to use to register with the server", Usage: "Generate a new token for a runner to use to register with the server",
Action: func(cliCtx *cli.Context) error { return RunGenerateActionsRunnerToken(ctx, cliCtx) }, Action: prepareWorkPathAndCustomConf(ctx, func(cliCtx *cli.Context) error { return RunGenerateActionsRunnerToken(ctx, cliCtx) }),
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.StringFlag{ &cli.StringFlag{
Name: "scope", Name: "scope",
@ -59,7 +59,7 @@ func SubcmdActionsRegister(ctx context.Context) *cli.Command {
return &cli.Command{ return &cli.Command{
Name: "register", Name: "register",
Usage: "Idempotent registration of a runner using a shared secret", Usage: "Idempotent registration of a runner using a shared secret",
Action: func(cliCtx *cli.Context) error { return RunRegister(ctx, cliCtx) }, Action: prepareWorkPathAndCustomConf(ctx, func(cliCtx *cli.Context) error { return RunRegister(ctx, cliCtx) }),
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.StringFlag{ &cli.StringFlag{
Name: "secret", Name: "secret",
@ -189,8 +189,6 @@ func RunRegister(ctx context.Context, cliCtx *cli.Context) error {
} }
func RunGenerateSecret(ctx context.Context, cliCtx *cli.Context) error { func RunGenerateSecret(ctx context.Context, cliCtx *cli.Context) error {
setting.MustInstalled()
runner := actions_model.ActionRunner{} runner := actions_model.ActionRunner{}
if err := runner.GenerateToken(); err != nil { if err := runner.GenerateToken(); err != nil {
return err return err
@ -221,3 +219,25 @@ func RunGenerateActionsRunnerToken(ctx context.Context, cliCtx *cli.Context) err
} }
return nil return nil
} }
func prepareWorkPathAndCustomConf(ctx context.Context, action cli.ActionFunc) func(cliCtx *cli.Context) error {
return func(cliCtx *cli.Context) error {
if !ContextGetNoInit(ctx) {
var args setting.ArgWorkPathAndCustomConf
// from children to parent, check the global flags
for _, curCtx := range cliCtx.Lineage() {
if curCtx.IsSet("work-path") && args.WorkPath == "" {
args.WorkPath = curCtx.String("work-path")
}
if curCtx.IsSet("custom-path") && args.CustomPath == "" {
args.CustomPath = curCtx.String("custom-path")
}
if curCtx.IsSet("config") && args.CustomConf == "" {
args.CustomConf = curCtx.String("config")
}
}
setting.InitWorkPathAndCommonConfig(os.Getenv, args)
}
return action(cliCtx)
}
}

View file

@ -61,8 +61,7 @@ func appGlobalFlags() []cli.Flag {
return []cli.Flag{ return []cli.Flag{
// make the builtin flags at the top // make the builtin flags at the top
helpFlag, helpFlag,
// Forgejo: commented out because it would conflict at runtime with the --version cli.VersionFlag,
// cli.VersionFlag,
// shared configuration flags, they are for global and for each sub-command at the same time // shared configuration flags, they are for global and for each sub-command at the same time
// eg: such command is valid: "./gitea --config /tmp/app.ini web --config /tmp/app.ini", while it's discouraged indeed // eg: such command is valid: "./gitea --config /tmp/app.ini web --config /tmp/app.ini", while it's discouraged indeed
@ -86,25 +85,6 @@ func appGlobalFlags() []cli.Flag {
} }
} }
func makePathOutput(workPath, customPath, customConf string) string {
return fmt.Sprintf("WorkPath=%s\nCustomPath=%s\nCustomConf=%s", workPath, customPath, customConf)
}
func NewTestApp() *cli.App {
app := NewMainApp()
testCmd := &cli.Command{
Name: "test-cmd",
Action: func(ctx *cli.Context) error {
_, _ = fmt.Fprint(app.Writer, makePathOutput(setting.AppWorkPath, setting.CustomPath, setting.CustomConf))
return nil
},
}
prepareSubcommandWithConfig(testCmd, appGlobalFlags())
app.Commands = append(app.Commands, testCmd)
app.DefaultCommand = testCmd.Name
return app
}
func prepareSubcommandWithConfig(command *cli.Command, globalFlags []cli.Flag) { func prepareSubcommandWithConfig(command *cli.Command, globalFlags []cli.Flag) {
command.Flags = append(append([]cli.Flag{}, globalFlags...), command.Flags...) command.Flags = append(append([]cli.Flag{}, globalFlags...), command.Flags...)
command.Action = prepareWorkPathAndCustomConf(command.Action) command.Action = prepareWorkPathAndCustomConf(command.Action)
@ -231,7 +211,6 @@ func newMainApp(subCmds ...*cli.Command) *cli.App {
cmdConvert := util.ToPointer(*cmdDoctorConvert) cmdConvert := util.ToPointer(*cmdDoctorConvert)
cmdConvert.Hidden = true // still support the legacy "./gitea doctor" by the hidden sub-command, remove it in next release cmdConvert.Hidden = true // still support the legacy "./gitea doctor" by the hidden sub-command, remove it in next release
subCmdWithConfig = append(subCmdWithConfig, cmdConvert) subCmdWithConfig = append(subCmdWithConfig, cmdConvert)
subCmdWithConfig = append(subCmdWithConfig, subCmds...)
// these sub-commands do not need the config file, and they do not depend on any path or environment variable. // these sub-commands do not need the config file, and they do not depend on any path or environment variable.
subCmdStandalone := []*cli.Command{ subCmdStandalone := []*cli.Command{
@ -251,6 +230,7 @@ func newMainApp(subCmds ...*cli.Command) *cli.App {
} }
app.Commands = append(app.Commands, subCmdWithConfig...) app.Commands = append(app.Commands, subCmdWithConfig...)
app.Commands = append(app.Commands, subCmdStandalone...) app.Commands = append(app.Commands, subCmdStandalone...)
app.Commands = append(app.Commands, subCmds...)
if !checkCommandFlags(app) { if !checkCommandFlags(app) {
panic("some flags are incorrect") // this is a runtime check to help developers panic("some flags are incorrect") // this is a runtime check to help developers

View file

@ -23,6 +23,10 @@ func TestMain(m *testing.M) {
}) })
} }
func makePathOutput(workPath, customPath, customConf string) string {
return fmt.Sprintf("WorkPath=%s\nCustomPath=%s\nCustomConf=%s", workPath, customPath, customConf)
}
func newTestApp() *cli.App { func newTestApp() *cli.App {
app := NewMainApp() app := NewMainApp()
testCmd := &cli.Command{ testCmd := &cli.Command{

View file

@ -22,6 +22,5 @@ func GenerateActionsRunnerToken(ctx context.Context, scope string) (string, Resp
}) })
resp, extra := requestJSONResp(req, &responseText{}) resp, extra := requestJSONResp(req, &responseText{})
// fmt.Printf("resp %v, extra %+v\n", resp, extra)
return resp.Text, extra return resp.Text, extra
} }