summaryrefslogtreecommitdiff
path: root/cmd/authelia-scripts/main.go
blob: d745d859ef3d992c0dec1182a3e4e6c71760a066 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//usr/bin/env go run "$0" "$@"; exit
//nolint:gocritic

package main

import (
	log "github.com/sirupsen/logrus"
	"github.com/spf13/cobra"

	"github.com/authelia/authelia/internal/commands"
	"github.com/authelia/authelia/internal/utils"
)

var buildkite bool
var logLevel string

// 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
}

// CobraCommands list of cobra commands.
type CobraCommands = []*cobra.Command

// Commands is the list of commands of authelia-scripts.
var Commands = []AutheliaCommandDefinition{
	{
		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,
	},
	{
		Name:  "build",
		Short: "Build Authelia binary and static assets",
		Func:  Build,
	},
	{
		Name:  "clean",
		Short: "Clean build artifacts",
		Func:  Clean,
	},
	{
		Name:        "docker",
		Short:       "Commands related to building and publishing docker image",
		SubCommands: CobraCommands{DockerBuildCmd, DockerPushCmd, DockerManifestCmd},
	},
	{
		Name:  "serve [config]",
		Short: "Serve compiled version of Authelia",
		Func:  ServeCmd,
		Args:  cobra.MinimumNArgs(1),
	},
	{
		Name:  "suites",
		Short: "Commands related to suites management",
		SubCommands: CobraCommands{
			SuitesTestCmd,
			SuitesListCmd,
			SuitesSetupCmd,
			SuitesTeardownCmd,
		},
	},
	{
		Name:  "ci",
		Short: "Run continuous integration script",
		Func:  RunCI,
	},
	{
		Name:  "unittest",
		Short: "Run unit tests",
		Func:  RunUnitTest,
	},
}

func levelStringToLevel(level string) log.Level {
	if level == "debug" {
		return log.DebugLevel
	} else if level == "warning" {
		return log.WarnLevel
	}

	return log.InfoLevel
}

func main() {
	var rootCmd = &cobra.Command{Use: "authelia-scripts"}

	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) {
				cmd := utils.CommandWithStdout(cmdline, args...)

				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)
	}

	cobraCommands = append(cobraCommands, commands.NewHashPasswordCmd(), commands.NewCertificatesCmd(), commands.NewRSACmd(), xflagsCmd)

	rootCmd.PersistentFlags().BoolVar(&buildkite, "buildkite", false, "Set CI flag for Buildkite")
	rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "Set the log level for the command")
	rootCmd.AddCommand(cobraCommands...)
	cobra.OnInitialize(initConfig)

	err := rootCmd.Execute()

	if err != nil {
		log.Fatal(err)
	}
}

func initConfig() {
	log.SetLevel(levelStringToLevel(logLevel))
}