Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions env/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Package env defines our application environments (local, test, dev, staging, prod)
// with utilities for comparison, string conversion, and text marshaling.
package env

import (
"fmt"
"slices"
"strings"
)

type Env uint8

const (
EnvLocal Env = iota
EnvTest
EnvDev
EnvDev2
EnvNext
EnvStg
EnvProd
)

var environments = []string{
"local", // 0
"test", // 1
"dev", // 2
"dev2", // 3
"next", // 4
"stg", // 5
"prod", // 6
}

func (e Env) Is(envs ...Env) bool {
return slices.Contains(envs, e)
}

func (e Env) MarshalText() ([]byte, error) {
return []byte(e.String()), nil
}

func (e *Env) UnmarshalText(text []byte) error {
enum := string(text)

// on empty string fallback to "local"
if enum == "" {
*e = EnvLocal
return nil
}

for i, name := range environments {
if enum == name {
*e = Env(i)
return nil
}
}

return fmt.Errorf("unknown env=(%s), supported=(%s)", text, strings.Join(environments, ","))
}

func (e Env) String() string {
if int(e) >= len(environments) {
return fmt.Sprintf("Env(%d)", e)
}

return environments[e]
}