package client

import (
	"context"

	"github.com/sethvargo/go-envconfig"
)

type Env struct {
	LoginAuthURL       string `env:"TELEPRESENCE_LOGIN_AUTH_URL,default=https://auth.datawire.io/auth"`
	LoginTokenURL      string `env:"TELEPRESENCE_LOGIN_TOKEN_URL,default=https://auth.datawire.io/token"`
	LoginCompletionURL string `env:"TELEPRESENCE_LOGIN_COMPLETION_URL,default=https://auth.datawire.io/completion"`
	LoginClientID      string `env:"TELEPRESENCE_LOGIN_CLIENT_ID,default=telepresence-cli"`

	Registry string `env:"TELEPRESENCE_REGISTRY,default=docker.io/datawire"`

	SystemAHost string `env:"SYSTEMA_HOST,default="`
	SystemAPort string `env:"SYSTEMA_PORT,default="`
}

func LoadEnv(ctx context.Context) (Env, error) {
	var env Env
	err := envconfig.Process(ctx, &env)
	return env, err
}

package client

import (
	"context"
	"os"

	"github.com/sethvargo/go-envconfig"
)

type Env struct {
	LoginDomain        string `env:"TELEPRESENCE_LOGIN_DOMAIN,required"`
	LoginAuthURL       string `env:"TELEPRESENCE_LOGIN_AUTH_URL,default=https://${TELEPRESENCE_LOGIN_DOMAIN}/auth"`
	LoginTokenURL      string `env:"TELEPRESENCE_LOGIN_TOKEN_URL,default=https://${TELEPRESENCE_LOGIN_DOMAIN}/token"`
	LoginCompletionURL string `env:"TELEPRESENCE_LOGIN_COMPLETION_URL,default=https://${TELEPRESENCE_LOGIN_DOMAIN}/completion"`
	LoginClientID      string `env:"TELEPRESENCE_LOGIN_CLIENT_ID,default=telepresence-cli"`

	Registry string `env:"TELEPRESENCE_REGISTRY,default=docker.io/datawire"`

	SystemAHost string `env:"SYSTEMA_HOST,default="`
	SystemAPort string `env:"SYSTEMA_PORT,default="`
}

func maybeSetEnv(key, val string) {
	if os.Getenv(key) == "" {
		os.Setenv(key, val)
	}
}

func LoadEnv(ctx context.Context) (Env, error) {
	switch os.Getenv("SYSTEMA_ENV") {
	case "staging":
		maybeSetEnv("TELEPRESENCE_LOGIN_DOMAIN", "beta-auth.datawire.io")
		maybeSetEnv("SYSTEMA_HOST", "beta-app.datawire.io")
	default:
		maybeSetEnv("TELEPRESENCE_LOGIN_DOMAIN", "auth.datawire.io")
	}

	var env Env
	err := envconfig.Process(ctx, &env)
	return env, err
}

Add a SYSTEMA_ENV variable to easily adjust staging/prod
CLI: Add a SYSTEMA_ENV variable to easily adjust staging/prod
