You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

admin_auth.go 2.6KB

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