2018-02-18 11:14:37 -07:00
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
|
|
|
// Copyright 2016 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2018-02-18 11:14:37 -07:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-05-20 10:16:06 -06:00
|
|
|
"os"
|
2018-02-18 11:14:37 -07:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/generate"
|
|
|
|
|
2020-05-20 10:16:06 -06:00
|
|
|
"github.com/mattn/go-isatty"
|
2023-07-21 03:28:19 -06:00
|
|
|
"github.com/urfave/cli/v2"
|
2018-02-18 11:14:37 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// CmdGenerate represents the available generate sub-command.
|
2023-07-21 03:28:19 -06:00
|
|
|
CmdGenerate = &cli.Command{
|
2018-02-18 11:14:37 -07:00
|
|
|
Name: "generate",
|
|
|
|
Usage: "Command line interface for running generators",
|
2023-07-21 03:28:19 -06:00
|
|
|
Subcommands: []*cli.Command{
|
2018-02-18 11:14:37 -07:00
|
|
|
subcmdSecret,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-07-21 03:28:19 -06:00
|
|
|
subcmdSecret = &cli.Command{
|
2018-02-18 11:14:37 -07:00
|
|
|
Name: "secret",
|
|
|
|
Usage: "Generate a secret token",
|
2023-07-21 03:28:19 -06:00
|
|
|
Subcommands: []*cli.Command{
|
2018-02-18 11:14:37 -07:00
|
|
|
microcmdGenerateInternalToken,
|
|
|
|
microcmdGenerateLfsJwtSecret,
|
|
|
|
microcmdGenerateSecretKey,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-07-21 03:28:19 -06:00
|
|
|
microcmdGenerateInternalToken = &cli.Command{
|
2018-02-18 11:14:37 -07:00
|
|
|
Name: "INTERNAL_TOKEN",
|
|
|
|
Usage: "Generate a new INTERNAL_TOKEN",
|
|
|
|
Action: runGenerateInternalToken,
|
|
|
|
}
|
|
|
|
|
2023-07-21 03:28:19 -06:00
|
|
|
microcmdGenerateLfsJwtSecret = &cli.Command{
|
2019-05-01 20:32:06 -06:00
|
|
|
Name: "JWT_SECRET",
|
|
|
|
Aliases: []string{"LFS_JWT_SECRET"},
|
|
|
|
Usage: "Generate a new JWT_SECRET",
|
|
|
|
Action: runGenerateLfsJwtSecret,
|
2018-02-18 11:14:37 -07:00
|
|
|
}
|
|
|
|
|
2023-07-21 03:28:19 -06:00
|
|
|
microcmdGenerateSecretKey = &cli.Command{
|
2018-02-18 11:14:37 -07:00
|
|
|
Name: "SECRET_KEY",
|
|
|
|
Usage: "Generate a new SECRET_KEY",
|
|
|
|
Action: runGenerateSecretKey,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func runGenerateInternalToken(c *cli.Context) error {
|
|
|
|
internalToken, err := generate.NewInternalToken()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-20 10:16:06 -06:00
|
|
|
fmt.Printf("%s", internalToken)
|
|
|
|
|
|
|
|
if isatty.IsTerminal(os.Stdout.Fd()) {
|
|
|
|
fmt.Printf("\n")
|
|
|
|
}
|
|
|
|
|
2018-02-18 11:14:37 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runGenerateLfsJwtSecret(c *cli.Context) error {
|
2024-02-22 10:07:41 -07:00
|
|
|
_, jwtSecretBase64, err := generate.NewJwtSecretWithBase64()
|
2018-02-18 11:14:37 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-14 04:30:16 -06:00
|
|
|
fmt.Printf("%s", jwtSecretBase64)
|
2020-05-20 10:16:06 -06:00
|
|
|
|
|
|
|
if isatty.IsTerminal(os.Stdout.Fd()) {
|
|
|
|
fmt.Printf("\n")
|
|
|
|
}
|
|
|
|
|
2018-02-18 11:14:37 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runGenerateSecretKey(c *cli.Context) error {
|
|
|
|
secretKey, err := generate.NewSecretKey()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-20 10:16:06 -06:00
|
|
|
fmt.Printf("%s", secretKey)
|
|
|
|
|
|
|
|
if isatty.IsTerminal(os.Stdout.Fd()) {
|
|
|
|
fmt.Printf("\n")
|
|
|
|
}
|
|
|
|
|
2018-02-18 11:14:37 -07:00
|
|
|
return nil
|
|
|
|
}
|