code.gitea.io/gitea@v1.21.7/cmd/generate.go (about)

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