mirror of
https://codeberg.org/forgejo/forgejo
synced 2025-10-18 22:50:36 +02:00
Fixes #9433. ``` $ ./gitea admin user create --username blah --must-change-password false Hint: boolean false must be specified as a single arg, eg. '--restricted=false', not '--restricted false' Command error: unexpected arguments: false ``` **Breaking**: CLI sub-commands that only have flags would previously ignore anything that might be considered an "extra" argument, and would proceed without any errors. I've manually tested this change on the single `admin user create` command with positive (ensuring cmd still works) and negative (ensuring errors are reported) test cases. I've attempted to ensure the change is applied only to commands which don't use the CLI `Args()` and avoided touching them, including: - `admin user must-change-password` takes a list of users - `doctor recreate-tables` takes a list of tables - `embedded [list/view/extract]` use a pattern of resources to operate upon - git repo hook subcommands, and the ssh serv command, use arguments and have been omitted from the change ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests - I added test coverage for Go changes... - [ ] in their respective `*_test.go` for unit tests. - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I added test coverage for JavaScript changes... - [ ] in `web_src/js/*.test.js` if it can be unit tested. - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)). ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [ ] I do not want this change to show in the release notes. - [ ] I want the title to show in the release notes with a link to this pull request. - [x] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9458 Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net> Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
136 lines
3.4 KiB
Go
136 lines
3.4 KiB
Go
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"text/tabwriter"
|
|
|
|
auth_model "forgejo.org/models/auth"
|
|
"forgejo.org/models/db"
|
|
auth_service "forgejo.org/services/auth"
|
|
|
|
"github.com/urfave/cli/v3"
|
|
)
|
|
|
|
type (
|
|
authService struct {
|
|
initDB func(ctx context.Context) error
|
|
createAuthSource func(context.Context, *auth_model.Source) error
|
|
updateAuthSource func(context.Context, *auth_model.Source) error
|
|
getAuthSourceByID func(ctx context.Context, id int64) (*auth_model.Source, error)
|
|
}
|
|
)
|
|
|
|
func microcmdAuthDelete() *cli.Command {
|
|
return &cli.Command{
|
|
Name: "delete",
|
|
Usage: "Delete specific auth source",
|
|
Flags: []cli.Flag{idFlag()},
|
|
Before: noDanglingArgs,
|
|
Action: runDeleteAuth,
|
|
}
|
|
}
|
|
|
|
func microcmdAuthList() *cli.Command {
|
|
return &cli.Command{
|
|
Name: "list",
|
|
Usage: "List auth sources",
|
|
Before: noDanglingArgs,
|
|
Action: runListAuth,
|
|
Flags: []cli.Flag{
|
|
&cli.IntFlag{
|
|
Name: "min-width",
|
|
Usage: "Minimal cell width including any padding for the formatted table",
|
|
Value: 0,
|
|
},
|
|
&cli.IntFlag{
|
|
Name: "tab-width",
|
|
Usage: "width of tab characters in formatted table (equivalent number of spaces)",
|
|
Value: 8,
|
|
},
|
|
&cli.IntFlag{
|
|
Name: "padding",
|
|
Usage: "padding added to a cell before computing its width",
|
|
Value: 1,
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "pad-char",
|
|
Usage: `ASCII char used for padding if padchar == '\\t', the Writer will assume that the width of a '\\t' in the formatted output is tabwidth, and cells are left-aligned independent of align_left (for correct-looking results, tabwidth must correspond to the tab width in the viewer displaying the result)`,
|
|
Value: "\t",
|
|
},
|
|
&cli.BoolFlag{
|
|
Name: "vertical-bars",
|
|
Usage: "Set to true to print vertical bars between columns",
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
// newAuthService creates a service with default functions.
|
|
func newAuthService() *authService {
|
|
return &authService{
|
|
initDB: initDB,
|
|
createAuthSource: auth_model.CreateSource,
|
|
updateAuthSource: auth_model.UpdateSource,
|
|
getAuthSourceByID: auth_model.GetSourceByID,
|
|
}
|
|
}
|
|
|
|
func runListAuth(ctx context.Context, c *cli.Command) error {
|
|
ctx, cancel := installSignals(ctx)
|
|
defer cancel()
|
|
|
|
if err := initDB(ctx); err != nil {
|
|
return err
|
|
}
|
|
|
|
authSources, err := db.Find[auth_model.Source](ctx, auth_model.FindSourcesOptions{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
flags := tabwriter.AlignRight
|
|
if c.Bool("vertical-bars") {
|
|
flags |= tabwriter.Debug
|
|
}
|
|
|
|
padChar := byte('\t')
|
|
if len(c.String("pad-char")) > 0 {
|
|
padChar = c.String("pad-char")[0]
|
|
}
|
|
|
|
// loop through each source and print
|
|
w := tabwriter.NewWriter(os.Stdout, c.Int("min-width"), c.Int("tab-width"), c.Int("padding"), padChar, flags)
|
|
fmt.Fprint(w, "ID\tName\tType\tEnabled\n")
|
|
for _, source := range authSources {
|
|
fmt.Fprintf(w, "%d\t%s\t%s\t%t\n", source.ID, source.Name, source.Type.String(), source.IsActive)
|
|
}
|
|
w.Flush()
|
|
|
|
return nil
|
|
}
|
|
|
|
func runDeleteAuth(ctx context.Context, c *cli.Command) error {
|
|
if !c.IsSet("id") {
|
|
return errors.New("--id flag is missing")
|
|
}
|
|
|
|
ctx, cancel := installSignals(ctx)
|
|
defer cancel()
|
|
|
|
if err := initDB(ctx); err != nil {
|
|
return err
|
|
}
|
|
|
|
source, err := auth_model.GetSourceByID(ctx, c.Int64("id"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return auth_service.DeleteSource(ctx, source)
|
|
}
|