Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

admin_auth.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package cmd
  4. import (
  5. "errors"
  6. "fmt"
  7. "os"
  8. "text/tabwriter"
  9. auth_model "code.gitea.io/gitea/models/auth"
  10. "code.gitea.io/gitea/models/db"
  11. auth_service "code.gitea.io/gitea/services/auth"
  12. "github.com/urfave/cli/v2"
  13. )
  14. var (
  15. microcmdAuthDelete = &cli.Command{
  16. Name: "delete",
  17. Usage: "Delete specific auth source",
  18. Flags: []cli.Flag{idFlag},
  19. Action: runDeleteAuth,
  20. }
  21. microcmdAuthList = &cli.Command{
  22. Name: "list",
  23. Usage: "List auth sources",
  24. Action: runListAuth,
  25. Flags: []cli.Flag{
  26. &cli.IntFlag{
  27. Name: "min-width",
  28. Usage: "Minimal cell width including any padding for the formatted table",
  29. Value: 0,
  30. },
  31. &cli.IntFlag{
  32. Name: "tab-width",
  33. Usage: "width of tab characters in formatted table (equivalent number of spaces)",
  34. Value: 8,
  35. },
  36. &cli.IntFlag{
  37. Name: "padding",
  38. Usage: "padding added to a cell before computing its width",
  39. Value: 1,
  40. },
  41. &cli.StringFlag{
  42. Name: "pad-char",
  43. 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)`,
  44. Value: "\t",
  45. },
  46. &cli.BoolFlag{
  47. Name: "vertical-bars",
  48. Usage: "Set to true to print vertical bars between columns",
  49. },
  50. },
  51. }
  52. )
  53. func runListAuth(c *cli.Context) error {
  54. ctx, cancel := installSignals()
  55. defer cancel()
  56. if err := initDB(ctx); err != nil {
  57. return err
  58. }
  59. authSources, err := db.Find[auth_model.Source](ctx, auth_model.FindSourcesOptions{})
  60. if err != nil {
  61. return err
  62. }
  63. flags := tabwriter.AlignRight
  64. if c.Bool("vertical-bars") {
  65. flags |= tabwriter.Debug
  66. }
  67. padChar := byte('\t')
  68. if len(c.String("pad-char")) > 0 {
  69. padChar = c.String("pad-char")[0]
  70. }
  71. // loop through each source and print
  72. w := tabwriter.NewWriter(os.Stdout, c.Int("min-width"), c.Int("tab-width"), c.Int("padding"), padChar, flags)
  73. fmt.Fprintf(w, "ID\tName\tType\tEnabled\n")
  74. for _, source := range authSources {
  75. fmt.Fprintf(w, "%d\t%s\t%s\t%t\n", source.ID, source.Name, source.Type.String(), source.IsActive)
  76. }
  77. w.Flush()
  78. return nil
  79. }
  80. func runDeleteAuth(c *cli.Context) error {
  81. if !c.IsSet("id") {
  82. return errors.New("--id flag is missing")
  83. }
  84. ctx, cancel := installSignals()
  85. defer cancel()
  86. if err := initDB(ctx); err != nil {
  87. return err
  88. }
  89. source, err := auth_model.GetSourceByID(ctx, c.Int64("id"))
  90. if err != nil {
  91. return err
  92. }
  93. return auth_service.DeleteSource(ctx, source)
  94. }