Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dinamically map and bind env vars #54

Merged
merged 3 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ coverage:
install:
mv $(MAIN_PATH)/$(CMD_NAME) $(GOBIN)/$(CMD_NAME)

build: $(CMD_FILES)
CGO_ENABLED=0 go build -ldflags="$(LDFLAGS)" -v -o $(CMD_NAME) $(MAIN_PATH)
# The build flag -tags=viper_bind_struct has been added to avoid the need to bind each of the environment variables
build: $(CMD_FILES)
CGO_ENABLED=0 go build -tags=viper_bind_struct -ldflags="$(LDFLAGS)" -v -o $(CMD_NAME) $(MAIN_PATH)
22 changes: 1 addition & 21 deletions cmd/greenmask/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ func init() {
),
)

if err := registerEnvVars(); err != nil {
log.Fatal().Err(err).Msg("unable to register env vars")
}

RootCmd.AddCommand(dump.Cmd)
RootCmd.AddCommand(list_dumps.Cmd)
RootCmd.AddCommand(restore.Cmd)
Expand Down Expand Up @@ -127,21 +123,6 @@ func init() {

}

func registerEnvVars() error {
varMap := map[string]string{
"storage.type": "STORAGE_TYPE",
"storage.directory.path": "STORAGE_DIRECTORY_PATH",
"common.pg_bin_path": "COMMON_PG_BIN_PATH",
}

for configPath, envVarName := range varMap {
if err := viper.BindEnv(configPath, envVarName); err != nil {
return err
}
}
return nil
}

func initConfig() {
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
Expand All @@ -164,5 +145,4 @@ func initConfig() {
if err := viper.Unmarshal(Config, decoderCfg); err != nil {
log.Fatal().Err(err).Msg("")
}

}
}
38 changes: 19 additions & 19 deletions internal/storages/s3/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@ const (
)

type Config struct {
Endpoint string `mapstructure:"endpoint,omitempty"`
Bucket string `mapstructure:"bucket,omitempty"`
Prefix string `mapstructure:"prefix,omitempty"`
Region string `mapstructure:"region,omitempty"`
StorageClass string `mapstructure:"storage_class,omitempty"`
DisableSSL bool `mapstructure:"disable_ssl,omitempty"`
AccessKeyId string `mapstructure:"access_key_id,omitempty"`
SecretAccessKey string `mapstructure:"secret_access_key,omitempty"`
SessionToken string `mapstructure:"session_token,omitempty"`
RoleArn string `mapstructure:"role_arn,omitempty"`
SessionName string `mapstructure:"session_name,omitempty"`
MaxRetries int `mapstructure:"max_retries,omitempty"`
CertFile string `mapstructure:"cert_file,omitempty"`
MaxPartSize int64 `mapstructure:"max_part_size,omitempty"`
Concurrency int `mapstructure:"concurrency,omitempty"`
UseListObjectsV1 bool `mapstructure:"use_list_objects_v1,omitempty"`
ForcePathStyle bool `mapstructure:"force_path_style,omitempty"`
UseAccelerate bool `mapstructure:"use_accelerate,omitempty"`
NoVerifySsl bool `mapstructure:"no_verify_ssl,omitempty"`
Endpoint string `mapstructure:"endpoint"`
Bucket string `mapstructure:"bucket"`
Prefix string `mapstructure:"prefix"`
Region string `mapstructure:"region"`
StorageClass string `mapstructure:"storage_class"`
DisableSSL bool `mapstructure:"disable_ssl"`
AccessKeyId string `mapstructure:"access_key_id"`
SecretAccessKey string `mapstructure:"secret_access_key"`
SessionToken string `mapstructure:"session_token"`
RoleArn string `mapstructure:"role_arn"`
SessionName string `mapstructure:"session_name"`
MaxRetries int `mapstructure:"max_retries"`
CertFile string `mapstructure:"cert_file"`
MaxPartSize int64 `mapstructure:"max_part_size"`
Concurrency int `mapstructure:"concurrency"`
UseListObjectsV1 bool `mapstructure:"use_list_objects_v1"`
ForcePathStyle bool `mapstructure:"force_path_style"`
UseAccelerate bool `mapstructure:"use_accelerate"`
NoVerifySsl bool `mapstructure:"no_verify_ssl"`
}

func NewConfig() *Config {
Expand Down