act_runner/cmd/config.go
Bo-Yi Wu 4d7ef95d40 chore(proto): Add ping request.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2022-11-24 15:36:54 +08:00

65 lines
1.5 KiB
Go

package cmd
import (
"fmt"
"os"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
)
type (
// Config provides the system configuration.
Config struct {
Debug bool `envconfig:"GITEA_DEBUG"`
Trace bool `envconfig:"GITEA_TRACE"`
Client struct {
Address string `ignored:"true"`
Proto string `envconfig:"GITEA_RPC_PROTO" default:"http"`
Host string `envconfig:"GITEA_RPC_HOST" required:"true"`
Secret string `envconfig:"GITEA_RPC_SECRET" required:"true"`
SkipVerify bool `envconfig:"GITEA_RPC_SKIP_VERIFY"`
}
Runner struct {
Name string `envconfig:"GITEA_RUNNER_NAME"`
Capacity int `envconfig:"GITEA_RUNNER_CAPACITY" default:"2"`
Procs int64 `envconfig:"GITEA_RUNNER_MAX_PROCS"`
Environ map[string]string `envconfig:"GITEA_RUNNER_ENVIRON"`
EnvFile string `envconfig:"GITEA_RUNNER_ENV_FILE"`
}
}
)
// fromEnviron returns the settings from the environment.
func fromEnviron() (Config, error) {
cfg := Config{}
err := envconfig.Process("", &cfg)
// runner config
if cfg.Runner.Environ == nil {
cfg.Runner.Environ = map[string]string{}
}
if cfg.Runner.Name == "" {
cfg.Runner.Name, _ = os.Hostname()
}
cfg.Client.Address = fmt.Sprintf(
"%s://%s",
cfg.Client.Proto,
cfg.Client.Host,
)
if file := cfg.Runner.EnvFile; file != "" {
envs, err := godotenv.Read(file)
if err != nil {
return cfg, err
}
for k, v := range envs {
cfg.Runner.Environ[k] = v
}
}
return cfg, err
}