environment refactoring (#34, #369)

This commit is contained in:
Michael Quigley 2023-07-10 16:53:27 -04:00
parent ea71493f2f
commit e26ef1ba1a
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
5 changed files with 21 additions and 21 deletions

View File

@ -8,13 +8,13 @@ import (
httptransport "github.com/go-openapi/runtime/client" httptransport "github.com/go-openapi/runtime/client"
"github.com/openziti/sdk-golang/ziti" "github.com/openziti/sdk-golang/ziti"
"github.com/openziti/sdk-golang/ziti/edge" "github.com/openziti/sdk-golang/ziti/edge"
"github.com/openziti/zrok/environment"
"github.com/openziti/zrok/model" "github.com/openziti/zrok/model"
"github.com/openziti/zrok/rest_client_zrok" "github.com/openziti/zrok/rest_client_zrok"
"github.com/openziti/zrok/rest_client_zrok/share" "github.com/openziti/zrok/rest_client_zrok/share"
"github.com/openziti/zrok/rest_model_zrok" "github.com/openziti/zrok/rest_model_zrok"
"github.com/openziti/zrok/tui" "github.com/openziti/zrok/tui"
"github.com/openziti/zrok/util" "github.com/openziti/zrok/util"
"github.com/openziti/zrok/environment"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"io" "io"

View File

@ -12,11 +12,11 @@ import (
"regexp" "regexp"
) )
func (zrd *Root) Client() (*rest_client_zrok.Zrok, error) { func (r *Root) Client() (*rest_client_zrok.Zrok, error) {
apiEndpoint, _ := zrd.ApiEndpoint() apiEndpoint, _ := r.ApiEndpoint()
apiUrl, err := url.Parse(apiEndpoint) apiUrl, err := url.Parse(apiEndpoint)
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "error parsing api endpoint '%v'", zrd) return nil, errors.Wrapf(err, "error parsing api endpoint '%v'", r)
} }
transport := httptransport.New(apiUrl.Host, "/api/v1", []string{apiUrl.Scheme}) transport := httptransport.New(apiUrl.Host, "/api/v1", []string{apiUrl.Scheme})
transport.Producers["application/zrok.v1+json"] = runtime.JSONProducer() transport.Producers["application/zrok.v1+json"] = runtime.JSONProducer()
@ -37,12 +37,12 @@ func (zrd *Root) Client() (*rest_client_zrok.Zrok, error) {
return zrok, nil return zrok, nil
} }
func (zrd *Root) ApiEndpoint() (apiEndpoint string, from string) { func (r *Root) ApiEndpoint() (apiEndpoint string, from string) {
apiEndpoint = "https://api.zrok.io" apiEndpoint = "https://api.zrok.io"
from = "binary" from = "binary"
if zrd.Cfg != nil && zrd.Cfg.ApiEndpoint != "" { if r.Cfg != nil && r.Cfg.ApiEndpoint != "" {
apiEndpoint = zrd.Cfg.ApiEndpoint apiEndpoint = r.Cfg.ApiEndpoint
from = "config" from = "config"
} }
@ -52,8 +52,8 @@ func (zrd *Root) ApiEndpoint() (apiEndpoint string, from string) {
from = "ZROK_API_ENDPOINT" from = "ZROK_API_ENDPOINT"
} }
if zrd.Env != nil && zrd.Env.ApiEndpoint != "" { if r.Env != nil && r.Env.ApiEndpoint != "" {
apiEndpoint = zrd.Env.ApiEndpoint apiEndpoint = r.Env.ApiEndpoint
from = "env" from = "env"
} }

View File

@ -11,7 +11,7 @@ type Config struct {
ApiEndpoint string `json:"api_endpoint"` ApiEndpoint string `json:"api_endpoint"`
} }
func hasConfig() (bool, error) { func HasConfig() (bool, error) {
cf, err := configFile() cf, err := configFile()
if err != nil { if err != nil {
return false, errors.Wrap(err, "error getting config file path") return false, errors.Wrap(err, "error getting config file path")
@ -26,7 +26,7 @@ func hasConfig() (bool, error) {
return true, nil return true, nil
} }
func loadConfig() (*Config, error) { func LoadConfig() (*Config, error) {
cf, err := configFile() cf, err := configFile()
if err != nil { if err != nil {
return nil, errors.Wrap(err, "error getting config file path") return nil, errors.Wrap(err, "error getting config file path")
@ -42,7 +42,7 @@ func loadConfig() (*Config, error) {
return cfg, nil return cfg, nil
} }
func saveConfig(cfg *Config) error { func SaveConfig(cfg *Config) error {
data, err := json.MarshalIndent(cfg, "", " ") data, err := json.MarshalIndent(cfg, "", " ")
if err != nil { if err != nil {
return errors.Wrap(err, "error marshaling config") return errors.Wrap(err, "error marshaling config")

View File

@ -13,7 +13,7 @@ type Environment struct {
ApiEndpoint string `json:"api_endpoint"` ApiEndpoint string `json:"api_endpoint"`
} }
func hasEnvironment() (bool, error) { func IsEnabled() (bool, error) {
ef, err := environmentFile() ef, err := environmentFile()
if err != nil { if err != nil {
return false, errors.Wrap(err, "error getting environment file path") return false, errors.Wrap(err, "error getting environment file path")

View File

@ -48,19 +48,19 @@ func Load() (*Root, error) {
} }
zrd.identities = ids zrd.identities = ids
hasCfg, err := hasConfig() hasCfg, err := HasConfig()
if err != nil { if err != nil {
return nil, err return nil, err
} }
if hasCfg { if hasCfg {
cfg, err := loadConfig() cfg, err := LoadConfig()
if err != nil { if err != nil {
return nil, err return nil, err
} }
zrd.Cfg = cfg zrd.Cfg = cfg
} }
hasEnv, err := hasEnvironment() hasEnv, err := IsEnabled()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -75,17 +75,17 @@ func Load() (*Root, error) {
return zrd, nil return zrd, nil
} }
func (zrd *Root) Save() error { func (r *Root) Save() error {
if err := writeMetadata(); err != nil { if err := writeMetadata(); err != nil {
return errors.Wrap(err, "error saving metadata") return errors.Wrap(err, "error saving metadata")
} }
if zrd.Env != nil { if r.Env != nil {
if err := saveEnvironment(zrd.Env); err != nil { if err := saveEnvironment(r.Env); err != nil {
return errors.Wrap(err, "error saving environment") return errors.Wrap(err, "error saving environment")
} }
} }
if zrd.Cfg != nil { if r.Cfg != nil {
if err := saveConfig(zrd.Cfg); err != nil { if err := SaveConfig(r.Cfg); err != nil {
return errors.Wrap(err, "error saving config") return errors.Wrap(err, "error saving config")
} }
} }