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.

101 lines
2.0 KiB

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