From 9739eb52d8f94d32f61068d7209958e8d2582818 Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Wed, 26 Jul 2023 15:20:32 +0200 Subject: [PATCH] [CLI] implement forgejo-cli actions (squash) restore --version Refs: https://codeberg.org/forgejo/forgejo/issues/1134 --- cmd/forgejo/actions.go | 28 ++++++++++++++++++++++++---- cmd/main.go | 24 ++---------------------- cmd/main_test.go | 4 ++++ modules/private/actions.go | 1 - 4 files changed, 30 insertions(+), 27 deletions(-) diff --git a/cmd/forgejo/actions.go b/cmd/forgejo/actions.go index 42e1825cd9..4949dfcba5 100644 --- a/cmd/forgejo/actions.go +++ b/cmd/forgejo/actions.go @@ -35,7 +35,7 @@ func SubcmdActionsGenerateRunnerToken(ctx context.Context) *cli.Command { return &cli.Command{ Name: "generate-runner-token", 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{ &cli.StringFlag{ Name: "scope", @@ -59,7 +59,7 @@ func SubcmdActionsRegister(ctx context.Context) *cli.Command { return &cli.Command{ Name: "register", 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{ &cli.StringFlag{ Name: "secret", @@ -189,8 +189,6 @@ func RunRegister(ctx context.Context, cliCtx *cli.Context) error { } func RunGenerateSecret(ctx context.Context, cliCtx *cli.Context) error { - setting.MustInstalled() - runner := actions_model.ActionRunner{} if err := runner.GenerateToken(); err != nil { return err @@ -221,3 +219,25 @@ func RunGenerateActionsRunnerToken(ctx context.Context, cliCtx *cli.Context) err } 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) + } +} diff --git a/cmd/main.go b/cmd/main.go index 09c7d8b3ec..2a49cafe3a 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -61,8 +61,7 @@ func appGlobalFlags() []cli.Flag { return []cli.Flag{ // make the builtin flags at the top 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 // 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) { command.Flags = append(append([]cli.Flag{}, globalFlags...), command.Flags...) command.Action = prepareWorkPathAndCustomConf(command.Action) @@ -231,7 +211,6 @@ func newMainApp(subCmds ...*cli.Command) *cli.App { cmdConvert := util.ToPointer(*cmdDoctorConvert) 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, subCmds...) // these sub-commands do not need the config file, and they do not depend on any path or environment variable. 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, subCmdStandalone...) + app.Commands = append(app.Commands, subCmds...) if !checkCommandFlags(app) { panic("some flags are incorrect") // this is a runtime check to help developers diff --git a/cmd/main_test.go b/cmd/main_test.go index 0cff4f18f8..e9204d09cc 100644 --- a/cmd/main_test.go +++ b/cmd/main_test.go @@ -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 { app := NewMainApp() testCmd := &cli.Command{ diff --git a/modules/private/actions.go b/modules/private/actions.go index d71b726bcd..c924dac2cd 100644 --- a/modules/private/actions.go +++ b/modules/private/actions.go @@ -22,6 +22,5 @@ func GenerateActionsRunnerToken(ctx context.Context, scope string) (string, Resp }) resp, extra := requestJSONResp(req, &responseText{}) - // fmt.Printf("resp %v, extra %+v\n", resp, extra) return resp.Text, extra }