MCPcopy Create free account
hub / github.com/belak/gitdir / NewEnvConfig

Function NewEnvConfig

cmd/gitdir/config.go:46–111  ·  view source on GitHub ↗

NewEnvConfig returns a new Config based on environment variables.

()

Source from the content-addressed store, hash-verified

44
45// NewEnvConfig returns a new Config based on environment variables.
46func NewEnvConfig() (Config, error) { //nolint:cyclop,funlen
47 var err error
48
49 c := DefaultConfig
50
51 if rawDebug, ok := os.LookupEnv("GITDIR_DEBUG"); ok {
52 c.LogDebug, err = strconv.ParseBool(rawDebug)
53 if err != nil {
54 return c, fmt.Errorf("GITDIR_DEBUG: %w", err)
55 }
56 }
57
58 if logFormat, ok := os.LookupEnv("GITDIR_LOG_FORMAT"); ok {
59 if logFormat != "console" && logFormat != "json" {
60 return c, errors.New("GITDIR_LOG_FORMAT: must be console or json")
61 }
62 }
63
64 // Set up the logger - anything other than console defaults to json.
65 if c.LogFormat == "console" {
66 log.Logger = zerolog.New(zerolog.NewConsoleWriter()).With().Timestamp().Logger()
67 }
68
69 if c.LogDebug {
70 zerolog.SetGlobalLevel(zerolog.DebugLevel)
71 }
72
73 if bindAddr, ok := os.LookupEnv("GITDIR_BIND_ADDR"); ok {
74 c.BindAddr = bindAddr
75 }
76
77 var ok bool
78
79 if c.BasePath, ok = os.LookupEnv("GITDIR_BASE_DIR"); !ok {
80 return c, fmt.Errorf("GITDIR_BASE_DIR: not set")
81 }
82
83 if c.BasePath, err = filepath.Abs(c.BasePath); err != nil {
84 return c, fmt.Errorf("GITDIR_BASE_DIR: %w", err)
85 }
86
87 info, err := os.Stat(c.BasePath)
88 if err != nil {
89 return c, fmt.Errorf("GITDIR_BASE_DIR: %w", err)
90 }
91
92 if !info.IsDir() {
93 return c, errors.New("GITDIR_BASE_DIR: not a directory")
94 }
95
96 // AdminUser and AdminPublicKey are allowed to not be set.
97 if adminUser, ok := os.LookupEnv("GITDIR_ADMIN_USER"); ok {
98 c.AdminUser = adminUser
99 }
100
101 if adminPublicKeyRaw, ok := os.LookupEnv("GITDIR_ADMIN_PUBLIC_KEY"); ok {
102 adminPublicKey, err := models.ParsePublicKey([]byte(adminPublicKeyRaw))
103 if err != nil {

Callers 1

mainFunction · 0.85

Calls 1

ParsePublicKeyFunction · 0.92

Tested by

no test coverage detected