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

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