2019-10-29 20:54:47 +00:00
|
|
|
//usr/bin/env go run "$0" "$@"; exit
|
2020-12-29 20:23:24 +00:00
|
|
|
//nolint:gocritic
|
2019-10-29 20:54:47 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-11-02 14:32:58 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2019-10-29 20:54:47 +00:00
|
|
|
"github.com/spf13/cobra"
|
2020-04-05 12:37:21 +00:00
|
|
|
|
|
|
|
"github.com/authelia/authelia/internal/commands"
|
|
|
|
"github.com/authelia/authelia/internal/utils"
|
2019-10-29 20:54:47 +00:00
|
|
|
)
|
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
var logLevel string
|
|
|
|
|
2019-10-29 20:54:47 +00:00
|
|
|
// AutheliaCommandDefinition is the definition of one authelia-scripts command.
|
|
|
|
type AutheliaCommandDefinition struct {
|
|
|
|
Name string
|
|
|
|
Short string
|
|
|
|
Long string
|
|
|
|
CommandLine string
|
|
|
|
Args cobra.PositionalArgs
|
|
|
|
Func func(cmd *cobra.Command, args []string)
|
|
|
|
SubCommands []*cobra.Command
|
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// CobraCommands list of cobra commands.
|
2019-10-29 20:54:47 +00:00
|
|
|
type CobraCommands = []*cobra.Command
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// Commands is the list of commands of authelia-scripts.
|
2019-10-29 20:54:47 +00:00
|
|
|
var Commands = []AutheliaCommandDefinition{
|
2020-04-09 01:05:17 +00:00
|
|
|
{
|
2019-10-29 20:54:47 +00:00
|
|
|
Name: "bootstrap",
|
|
|
|
Short: "Prepare environment for development and testing.",
|
|
|
|
Long: `Prepare environment for development and testing. This command prepares docker
|
|
|
|
images and download tools like Kind for Kubernetes testing.`,
|
|
|
|
Func: Bootstrap,
|
|
|
|
},
|
2020-04-09 01:05:17 +00:00
|
|
|
{
|
2019-10-29 20:54:47 +00:00
|
|
|
Name: "build",
|
|
|
|
Short: "Build Authelia binary and static assets",
|
|
|
|
Func: Build,
|
|
|
|
},
|
2020-04-09 01:05:17 +00:00
|
|
|
{
|
2019-10-29 20:54:47 +00:00
|
|
|
Name: "clean",
|
|
|
|
Short: "Clean build artifacts",
|
|
|
|
Func: Clean,
|
|
|
|
},
|
2020-04-09 01:05:17 +00:00
|
|
|
{
|
2019-10-29 20:54:47 +00:00
|
|
|
Name: "docker",
|
|
|
|
Short: "Commands related to building and publishing docker image",
|
2019-11-07 00:59:24 +00:00
|
|
|
SubCommands: CobraCommands{DockerBuildCmd, DockerPushCmd, DockerManifestCmd},
|
2019-10-29 20:54:47 +00:00
|
|
|
},
|
2020-04-09 01:05:17 +00:00
|
|
|
{
|
2019-10-29 20:54:47 +00:00
|
|
|
Name: "serve [config]",
|
|
|
|
Short: "Serve compiled version of Authelia",
|
|
|
|
Func: ServeCmd,
|
|
|
|
Args: cobra.MinimumNArgs(1),
|
|
|
|
},
|
2020-04-09 01:05:17 +00:00
|
|
|
{
|
2019-11-02 14:32:58 +00:00
|
|
|
Name: "suites",
|
2021-05-04 22:06:05 +00:00
|
|
|
Short: "Commands related to suites management",
|
2019-11-02 14:32:58 +00:00
|
|
|
SubCommands: CobraCommands{
|
|
|
|
SuitesTestCmd,
|
|
|
|
SuitesListCmd,
|
|
|
|
SuitesSetupCmd,
|
|
|
|
SuitesTeardownCmd,
|
|
|
|
},
|
2019-10-29 20:54:47 +00:00
|
|
|
},
|
2020-04-09 01:05:17 +00:00
|
|
|
{
|
2019-10-29 20:54:47 +00:00
|
|
|
Name: "ci",
|
|
|
|
Short: "Run continuous integration script",
|
|
|
|
Func: RunCI,
|
|
|
|
},
|
2020-04-09 01:05:17 +00:00
|
|
|
{
|
2019-10-29 20:54:47 +00:00
|
|
|
Name: "unittest",
|
|
|
|
Short: "Run unit tests",
|
|
|
|
Func: RunUnitTest,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
func levelStringToLevel(level string) log.Level {
|
|
|
|
if level == "debug" {
|
|
|
|
return log.DebugLevel
|
|
|
|
} else if level == "warning" {
|
|
|
|
return log.WarnLevel
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
return log.InfoLevel
|
|
|
|
}
|
|
|
|
|
2019-10-29 20:54:47 +00:00
|
|
|
func main() {
|
|
|
|
var rootCmd = &cobra.Command{Use: "authelia-scripts"}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-10-29 20:54:47 +00:00
|
|
|
cobraCommands := make([]*cobra.Command, 0)
|
|
|
|
|
|
|
|
for _, autheliaCommand := range Commands {
|
|
|
|
var fn func(cobraCmd *cobra.Command, args []string)
|
|
|
|
|
|
|
|
if autheliaCommand.CommandLine != "" {
|
|
|
|
cmdline := autheliaCommand.CommandLine
|
|
|
|
fn = func(cobraCmd *cobra.Command, args []string) {
|
2019-11-02 14:32:58 +00:00
|
|
|
cmd := utils.CommandWithStdout(cmdline, args...)
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-10-29 20:54:47 +00:00
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if autheliaCommand.Func != nil {
|
|
|
|
fn = autheliaCommand.Func
|
|
|
|
}
|
|
|
|
|
|
|
|
command := &cobra.Command{
|
|
|
|
Use: autheliaCommand.Name,
|
|
|
|
Short: autheliaCommand.Short,
|
|
|
|
}
|
|
|
|
|
|
|
|
if autheliaCommand.Long != "" {
|
|
|
|
command.Long = autheliaCommand.Long
|
|
|
|
}
|
|
|
|
|
|
|
|
if fn != nil {
|
|
|
|
command.Run = fn
|
|
|
|
}
|
|
|
|
|
|
|
|
if autheliaCommand.Args != nil {
|
|
|
|
command.Args = autheliaCommand.Args
|
|
|
|
}
|
|
|
|
|
|
|
|
if autheliaCommand.SubCommands != nil {
|
|
|
|
command.AddCommand(autheliaCommand.SubCommands...)
|
|
|
|
}
|
|
|
|
|
|
|
|
cobraCommands = append(cobraCommands, command)
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2021-06-18 04:35:43 +00:00
|
|
|
cobraCommands = append(cobraCommands, commands.HashPasswordCmd, commands.CertificatesCmd, commands.RSACmd, xflagsCmd)
|
2019-11-02 14:32:58 +00:00
|
|
|
|
|
|
|
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "Set the log level for the command")
|
2019-10-29 20:54:47 +00:00
|
|
|
rootCmd.AddCommand(cobraCommands...)
|
2019-11-02 14:32:58 +00:00
|
|
|
cobra.OnInitialize(initConfig)
|
|
|
|
|
2019-11-07 07:21:21 +00:00
|
|
|
err := rootCmd.Execute()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|
2019-11-02 14:32:58 +00:00
|
|
|
|
|
|
|
func initConfig() {
|
|
|
|
log.SetLevel(levelStringToLevel(logLevel))
|
|
|
|
}
|