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.

generate.go 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Copyright 2016 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package cmd
  5. import (
  6. "fmt"
  7. "os"
  8. "code.gitea.io/gitea/modules/generate"
  9. "github.com/mattn/go-isatty"
  10. "github.com/urfave/cli"
  11. )
  12. var (
  13. // CmdGenerate represents the available generate sub-command.
  14. CmdGenerate = cli.Command{
  15. Name: "generate",
  16. Usage: "Command line interface for running generators",
  17. Subcommands: []cli.Command{
  18. subcmdSecret,
  19. },
  20. }
  21. subcmdSecret = cli.Command{
  22. Name: "secret",
  23. Usage: "Generate a secret token",
  24. Subcommands: []cli.Command{
  25. microcmdGenerateInternalToken,
  26. microcmdGenerateLfsJwtSecret,
  27. microcmdGenerateSecretKey,
  28. },
  29. }
  30. microcmdGenerateInternalToken = cli.Command{
  31. Name: "INTERNAL_TOKEN",
  32. Usage: "Generate a new INTERNAL_TOKEN",
  33. Action: runGenerateInternalToken,
  34. }
  35. microcmdGenerateLfsJwtSecret = cli.Command{
  36. Name: "JWT_SECRET",
  37. Aliases: []string{"LFS_JWT_SECRET"},
  38. Usage: "Generate a new JWT_SECRET",
  39. Action: runGenerateLfsJwtSecret,
  40. }
  41. microcmdGenerateSecretKey = cli.Command{
  42. Name: "SECRET_KEY",
  43. Usage: "Generate a new SECRET_KEY",
  44. Action: runGenerateSecretKey,
  45. }
  46. )
  47. func runGenerateInternalToken(c *cli.Context) error {
  48. internalToken, err := generate.NewInternalToken()
  49. if err != nil {
  50. return err
  51. }
  52. fmt.Printf("%s", internalToken)
  53. if isatty.IsTerminal(os.Stdout.Fd()) {
  54. fmt.Printf("\n")
  55. }
  56. return nil
  57. }
  58. func runGenerateLfsJwtSecret(c *cli.Context) error {
  59. JWTSecretBase64, err := generate.NewJwtSecretBase64()
  60. if err != nil {
  61. return err
  62. }
  63. fmt.Printf("%s", JWTSecretBase64)
  64. if isatty.IsTerminal(os.Stdout.Fd()) {
  65. fmt.Printf("\n")
  66. }
  67. return nil
  68. }
  69. func runGenerateSecretKey(c *cli.Context) error {
  70. secretKey, err := generate.NewSecretKey()
  71. if err != nil {
  72. return err
  73. }
  74. fmt.Printf("%s", secretKey)
  75. if isatty.IsTerminal(os.Stdout.Fd()) {
  76. fmt.Printf("\n")
  77. }
  78. return nil
  79. }