2022-01-06 06:40:23 -05:00
|
|
|
package env
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
|
|
"github.com/qdm12/gluetun/internal/configuration/settings"
|
2023-05-29 20:43:06 +00:00
|
|
|
"github.com/qdm12/gosettings/sources/env"
|
2022-01-06 06:40:23 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
ErrSystemPUIDNotValid = errors.New("PUID is not valid")
|
|
|
|
|
ErrSystemPGIDNotValid = errors.New("PGID is not valid")
|
|
|
|
|
ErrSystemTimezoneNotValid = errors.New("timezone is not valid")
|
|
|
|
|
)
|
|
|
|
|
|
2022-08-26 15:16:51 +00:00
|
|
|
func (s *Source) readSystem() (system settings.System, err error) {
|
|
|
|
|
system.PUID, err = s.readID("PUID", "UID")
|
2022-01-06 06:40:23 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return system, err
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 15:16:51 +00:00
|
|
|
system.PGID, err = s.readID("PGID", "GID")
|
2022-01-06 06:40:23 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return system, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-30 15:21:09 +00:00
|
|
|
system.Timezone = env.String("TZ")
|
2022-01-06 06:40:23 -05:00
|
|
|
|
|
|
|
|
return system, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var ErrSystemIDNotValid = errors.New("system ID is not valid")
|
|
|
|
|
|
2022-08-26 15:16:51 +00:00
|
|
|
func (s *Source) readID(key, retroKey string) (
|
2022-05-01 13:35:14 +00:00
|
|
|
id *uint32, err error) {
|
2023-05-30 15:21:09 +00:00
|
|
|
idEnvKey, idStringPtr := s.getEnvWithRetro(key, []string{retroKey})
|
|
|
|
|
if idStringPtr == nil {
|
2022-01-06 06:40:23 -05:00
|
|
|
return nil, nil //nolint:nilnil
|
|
|
|
|
}
|
2023-05-30 15:21:09 +00:00
|
|
|
idString := *idStringPtr
|
2022-01-06 06:40:23 -05:00
|
|
|
|
2022-05-01 13:35:14 +00:00
|
|
|
const base = 10
|
|
|
|
|
const bitSize = 64
|
|
|
|
|
const max = uint64(^uint32(0))
|
|
|
|
|
idUint64, err := strconv.ParseUint(idString, base, bitSize)
|
2022-01-06 06:40:23 -05:00
|
|
|
if err != nil {
|
2022-05-01 13:35:14 +00:00
|
|
|
return nil, fmt.Errorf("environment variable %s: %w: %s",
|
|
|
|
|
idEnvKey, ErrSystemIDNotValid, err)
|
|
|
|
|
} else if idUint64 > max {
|
|
|
|
|
return nil, fmt.Errorf("environment variable %s: %w: %d: must be between 0 and %d",
|
|
|
|
|
idEnvKey, ErrSystemIDNotValid, idUint64, max)
|
2022-01-06 06:40:23 -05:00
|
|
|
}
|
|
|
|
|
|
2023-05-30 13:02:10 +00:00
|
|
|
return ptrTo(uint32(idUint64)), nil
|
2022-01-06 06:40:23 -05:00
|
|
|
}
|