chore: load config from env file

pull/2/head
Bo-Yi Wu 2022-07-21 09:36:17 +08:00 committed by Jason Song
parent 5213612033
commit aa9ed05903
5 changed files with 79 additions and 2 deletions

25
cmd/config/config.go Normal file
View File

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

View File

@ -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 { 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 { return func(cmd *cobra.Command, args []string) error {
log.Info().Msg("Starting runner daemon")
var conn *websocket.Conn var conn *websocket.Conn
var err error var err error
ticker := time.NewTicker(time.Second) ticker := time.NewTicker(time.Second)

View File

@ -5,10 +5,16 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "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/artifacts"
"github.com/nektos/act/pkg/model" "github.com/nektos/act/pkg/model"
"github.com/nektos/act/pkg/runner" "github.com/nektos/act/pkg/runner"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/cobra" "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) { func Execute(ctx context.Context) {
var envfile string
input := Input{ input := Input{
reuseContainers: true, reuseContainers: true,
forgeInstance: "gitea.com", forgeInstance: "gitea.com",
@ -83,6 +122,17 @@ func Execute(ctx context.Context) {
rootCmd.Flags().BoolP("run", "r", false, "run workflows") rootCmd.Flags().BoolP("run", "r", false, "run workflows")
rootCmd.Flags().StringP("job", "j", "", "run job") rootCmd.Flags().StringP("job", "j", "", "run job")
rootCmd.PersistentFlags().StringVarP(&input.forgeInstance, "forge-instance", "", "github.com", "Forge instance to use.") 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 { if err := rootCmd.Execute(); err != nil {
os.Exit(1) os.Exit(1)

1
go.mod
View File

@ -4,6 +4,7 @@ go 1.18
require ( require (
github.com/gorilla/websocket v1.4.2 github.com/gorilla/websocket v1.4.2
github.com/kelseyhightower/envconfig v1.4.0
github.com/nektos/act v0.2.26 github.com/nektos/act v0.2.26
github.com/rs/zerolog v1.26.1 github.com/rs/zerolog v1.26.1
github.com/spf13/cobra v1.4.0 github.com/spf13/cobra v1.4.0

2
go.sum
View File

@ -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/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 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= 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 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 h1:pH/t1WS9NzT8go394IqZeJTMHVm6Cr6ZJ6AQ+mdNo/o=
github.com/kevinburke/ssh_config v1.1.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= github.com/kevinburke/ssh_config v1.1.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=