From aa9ed0590359c29181811b9f503921e414e465b2 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Thu, 21 Jul 2022 09:36:17 +0800 Subject: [PATCH] chore: load config from env file --- cmd/config/config.go | 25 ++++++++++++++++++++++ cmd/damon.go | 3 +-- cmd/root.go | 50 ++++++++++++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ 5 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 cmd/config/config.go diff --git a/cmd/config/config.go b/cmd/config/config.go new file mode 100644 index 0000000..6625bea --- /dev/null +++ b/cmd/config/config.go @@ -0,0 +1,25 @@ +package config + +import "github.com/kelseyhightower/envconfig" + +type ( + // Config provides the system configuration. + Config struct { + Logging Logging + } +) + +type Logging struct { + Debug bool `envconfig:"APP_LOGS_DEBUG"` + Level string `envconfig:"APP_LOGS_LEVEL" default:"info"` + NoColor bool `envconfig:"APP_LOGS_COLOR"` + Pretty bool `envconfig:"APP_LOGS_PRETTY"` + Text bool `envconfig:"APP_LOGS_TEXT"` +} + +// Environ returns the settings from the environment. +func Environ() (Config, error) { + cfg := Config{} + err := envconfig.Process("", &cfg) + return cfg, err +} diff --git a/cmd/damon.go b/cmd/damon.go index 331299b..d7380b1 100644 --- a/cmd/damon.go +++ b/cmd/damon.go @@ -127,9 +127,8 @@ func handleMessage(ctx context.Context, conn *websocket.Conn, message []byte) er } func runDaemon(ctx context.Context, input *Input) func(cmd *cobra.Command, args []string) error { - log.Info().Msgf("Starting runner daemon") - return func(cmd *cobra.Command, args []string) error { + log.Info().Msg("Starting runner daemon") var conn *websocket.Conn var err error ticker := time.NewTicker(time.Second) diff --git a/cmd/root.go b/cmd/root.go index a15b5c5..e079c1c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -5,10 +5,16 @@ import ( "fmt" "os" "path/filepath" + "strings" + "gitea.com/gitea/act_runner/cmd/config" + + "github.com/joho/godotenv" + "github.com/mattn/go-isatty" "github.com/nektos/act/pkg/artifacts" "github.com/nektos/act/pkg/model" "github.com/nektos/act/pkg/runner" + "github.com/rs/zerolog" "github.com/rs/zerolog/log" "github.com/spf13/cobra" ) @@ -59,7 +65,40 @@ func (i *Input) newPlatforms() map[string]string { } } +// helper function cfgures the logging. +func initLogging(cfg config.Config) { + isTerm := isatty.IsTerminal(os.Stdout.Fd()) + + switch strings.ToLower(cfg.Logging.Level) { + case "panic": + zerolog.SetGlobalLevel(zerolog.PanicLevel) + case "fatal": + zerolog.SetGlobalLevel(zerolog.FatalLevel) + case "error": + zerolog.SetGlobalLevel(zerolog.ErrorLevel) + case "warn": + zerolog.SetGlobalLevel(zerolog.WarnLevel) + case "info": + zerolog.SetGlobalLevel(zerolog.InfoLevel) + case "debug": + zerolog.SetGlobalLevel(zerolog.DebugLevel) + default: + zerolog.SetGlobalLevel(zerolog.InfoLevel) + } + + if cfg.Logging.Pretty || isTerm { + log.Logger = log.Output( + zerolog.ConsoleWriter{ + Out: os.Stderr, + NoColor: cfg.Logging.NoColor || !isTerm, + }, + ) + } +} + func Execute(ctx context.Context) { + var envfile string + input := Input{ reuseContainers: true, forgeInstance: "gitea.com", @@ -83,6 +122,17 @@ func Execute(ctx context.Context) { rootCmd.Flags().BoolP("run", "r", false, "run workflows") rootCmd.Flags().StringP("job", "j", "", "run job") rootCmd.PersistentFlags().StringVarP(&input.forgeInstance, "forge-instance", "", "github.com", "Forge instance to use.") + rootCmd.PersistentFlags().StringVarP(&envfile, "env-file", "", ".env", "Read in a file of environment variables.") + + _ = godotenv.Load(envfile) + cfg, err := config.Environ() + if err != nil { + log.Fatal(). + Err(err). + Msg("invalid cfguration") + } + + initLogging(cfg) if err := rootCmd.Execute(); err != nil { os.Exit(1) diff --git a/go.mod b/go.mod index 61c56c3..7d39b10 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.18 require ( github.com/gorilla/websocket v1.4.2 + github.com/kelseyhightower/envconfig v1.4.0 github.com/nektos/act v0.2.26 github.com/rs/zerolog v1.26.1 github.com/spf13/cobra v1.4.0 diff --git a/go.sum b/go.sum index 5da7b86..2ca9797 100644 --- a/go.sum +++ b/go.sum @@ -847,6 +847,8 @@ github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4d github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= +github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8= +github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg= github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= github.com/kevinburke/ssh_config v1.1.0 h1:pH/t1WS9NzT8go394IqZeJTMHVm6Cr6ZJ6AQ+mdNo/o= github.com/kevinburke/ssh_config v1.1.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=