Skip to content

Commit

Permalink
feat(command,proxy): allow config path to be configured by env (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Feb 4, 2024
1 parent 614face commit 6c0d91d
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 72 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"cmock",
"cmocks",
"cobrass",
"configurator",
"cubiest",
"deadcode",
"deepcopy",
Expand Down
56 changes: 16 additions & 40 deletions src/app/command/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"log/slog"
"os"
"path/filepath"

"github.com/cubiest/jibberjabber"
"github.com/natefinch/lumberjack"
Expand Down Expand Up @@ -33,10 +32,6 @@ const (
perm = 0o766
)

var (
pixaRelativePath = filepath.Join("snivilised", "pixa")
)

type LocaleDetector interface {
Scan() language.Tag
}
Expand Down Expand Up @@ -68,13 +63,6 @@ func validatePositionalArgs(cmd *cobra.Command, args []string) error {
return nil
}

type ConfigInfo struct {
Name string
ConfigType string
ConfigPath string
Viper configuration.ViperConfig
}

// Bootstrap represents construct that performs start up of the cli
// without resorting to the use of Go's init() mechanism and minimal
// use of package global variables.
Expand All @@ -86,25 +74,33 @@ type Bootstrap struct {
}

type ConfigureOptionsInfo struct {
Detector LocaleDetector
Config ConfigInfo
Detector LocaleDetector
Config common.ConfigInfo
Configurator common.ConfigRunner
}

type ConfigureOptionFn func(*ConfigureOptionsInfo)

// Root builds the command tree and returns the root command, ready
// to be executed.
func (b *Bootstrap) Root(options ...ConfigureOptionFn) *cobra.Command {
home, err := os.UserHomeDir()
cobra.CheckErr(err)
vc := &configuration.GlobalViperConfig{}
ci := common.ConfigInfo{
Name: ApplicationName,
ConfigType: "yaml",
Viper: vc,
}

b.OptionsInfo = ConfigureOptionsInfo{
Detector: &Jabber{},
Config: ConfigInfo{
Config: common.ConfigInfo{
Name: ApplicationName,
ConfigType: "yaml",
ConfigPath: filepath.Join(home, pixaRelativePath),
Viper: &configuration.GlobalViperConfig{},
Viper: vc,
},
Configurator: configRunner{
vc: vc,
ci: &ci,
},
}

Expand Down Expand Up @@ -163,27 +159,7 @@ func (b *Bootstrap) Root(options ...ConfigureOptionFn) *cobra.Command {
}

func (b *Bootstrap) configure() {
vc := b.OptionsInfo.Config.Viper
ci := b.OptionsInfo.Config

vc.SetConfigName(ci.Name)
vc.SetConfigType(ci.ConfigType)
vc.AddConfigPath(ci.ConfigPath)
vc.AutomaticEnv()

err := vc.ReadInConfig()

handleLangSetting(vc)

if err != nil {
msg := xi18n.Text(i18n.UsingConfigFileTemplData{
ConfigFileName: vc.ConfigFileUsed(),
})

fmt.Fprintln(os.Stderr, msg)

fmt.Printf("💥 error reading config path: '%v' \n", err)
}
_ = b.OptionsInfo.Configurator.Run()
}

func handleLangSetting(config configuration.ViperConfig) {
Expand Down
61 changes: 61 additions & 0 deletions src/app/command/config-runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package command

import (
"fmt"
"os"
"path/filepath"

"github.com/snivilised/cobrass/src/assistant/configuration"
xi18n "github.com/snivilised/extendio/i18n"
"github.com/snivilised/pixa/src/app/proxy/common"
"github.com/snivilised/pixa/src/i18n"
"github.com/spf13/cobra"
)

type configRunner struct {
vc configuration.ViperConfig
ci *common.ConfigInfo
}

func (c configRunner) Run() error {
c.vc.SetConfigName(c.ci.Name)
c.vc.SetConfigType(c.ci.ConfigType)
c.vc.AddConfigPath(c.path())
c.vc.AutomaticEnv()

err := c.vc.ReadInConfig()

handleLangSetting(c.vc)

if err != nil {
msg := xi18n.Text(i18n.UsingConfigFileTemplData{
ConfigFileName: c.vc.ConfigFileUsed(),
})

fmt.Fprintln(os.Stderr, msg)

fmt.Printf("💥 error reading config path: '%v' \n", err)
}

return nil
}

func (c configRunner) path() string {
configPath := c.ci.ConfigPath

if configPath == "" {
configPath, _ = c.vc.Get("PIXA-HOME").(string)

fmt.Printf("---> ✨ PIXA-HOME found in environment: '%v'\n", configPath)
}

if configPath == "" {
home, err := os.UserHomeDir()
cobra.CheckErr(err)

defaultPath := filepath.Join("snivilised", "pixa")
configPath = filepath.Join(home, defaultPath)
}

return configPath
}
32 changes: 0 additions & 32 deletions src/app/proxy/common/common-defs.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,50 @@
package common

import (
"fmt"
"time"

"github.com/snivilised/cobrass/src/assistant/configuration"
"github.com/snivilised/cobrass/src/clif"
)

type (
ProfilesFlagOptionAsAnyPair = map[string]any
ProfilesConfigMap map[string]clif.ChangedFlagsMap
)

func (pc ProfilesConfigMap) Validate(name string) error {
if name != "" {
if _, found := pc[name]; !found {
return fmt.Errorf("no such profile: '%v'", name)
}
}

return nil
}

type (
Configs struct {
Profiles ProfilesConfig
Schemes SchemesConfig
Sampler SamplerConfig
Advanced AdvancedConfig
Logging LoggingConfig
}

ConfigInfo struct {
Name string
ConfigType string
ConfigPath string
Viper configuration.ViperConfig
}
)

type (
ConfigRunner interface {
Run() error
}

ProfilesConfig interface {
Profile(name string) (clif.ChangedFlagsMap, bool)
}
Expand Down

0 comments on commit 6c0d91d

Please sign in to comment.