Skip to content

Commit

Permalink
deprecation warning for client env var use (#721)
Browse files Browse the repository at this point in the history
  • Loading branch information
theganyo authored Sep 2, 2022
1 parent f1e9153 commit 8e82f10
Show file tree
Hide file tree
Showing 18 changed files with 47 additions and 32 deletions.
2 changes: 1 addition & 1 deletion cmd/registry/cmd/annotate/annotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func Command() *cobra.Command {
}
args[0] = c.FQName(args[0])

client, err := connection.NewRegistryClient(ctx)
client, err := connection.NewRegistryClientWithSettings(ctx, c)
if err != nil {
log.FromContext(ctx).WithError(err).Fatal("Failed to get client")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/registry/cmd/compute/complexity.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func complexityCommand() *cobra.Command {
log.FromContext(ctx).WithError(err).Fatal("Failed to get dry-run from flags")
}

client, err := connection.NewRegistryClient(ctx)
client, err := connection.NewRegistryClientWithSettings(ctx, c)
if err != nil {
log.FromContext(ctx).WithError(err).Fatal("Failed to get client")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/registry/cmd/compute/conformance.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func conformanceCommand() *cobra.Command {
log.FromContext(ctx).WithError(err).Fatal("Failed to get dry-run from flags")
}

client, err := connection.NewRegistryClient(ctx)
client, err := connection.NewRegistryClientWithSettings(ctx, c)
if err != nil {
log.FromContext(ctx).WithError(err).Fatal("Failed to get client")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/registry/cmd/compute/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func lintCommand() *cobra.Command {
log.FromContext(ctx).WithError(err).Fatal("Failed to get fry-run from flags")
}

client, err := connection.NewRegistryClient(ctx)
client, err := connection.NewRegistryClientWithSettings(ctx, c)
if err != nil {
log.FromContext(ctx).WithError(err).Fatal("Failed to get client")
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/registry/cmd/compute/lintstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ func lintStatsCommand() *cobra.Command {
log.FromContext(ctx).WithError(err).Fatal("Failed to get dry-run from flags")
}

client, err := connection.NewRegistryClient(ctx)
client, err := connection.NewRegistryClientWithSettings(ctx, c)
if err != nil {
log.FromContext(ctx).WithError(err).Fatal("Failed to get client")
}

adminClient, err := connection.NewAdminClient(ctx)
adminClient, err := connection.NewAdminClientWithSettings(ctx, c)
if err != nil {
log.FromContext(ctx).WithError(err).Fatal("Failed to get client")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/registry/cmd/compute/references.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func referencesCommand() *cobra.Command {
log.FromContext(ctx).WithError(err).Fatal("Failed to get dry-run from flags")
}

client, err := connection.NewRegistryClient(ctx)
client, err := connection.NewRegistryClientWithSettings(ctx, c)
if err != nil {
log.FromContext(ctx).WithError(err).Fatal("Failed to get client")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/registry/cmd/compute/vocabulary.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func vocabularyCommand() *cobra.Command {
log.FromContext(ctx).WithError(err).Fatal("Failed to get dry-run from flags")
}

client, err := connection.NewRegistryClient(ctx)
client, err := connection.NewRegistryClientWithSettings(ctx, c)
if err != nil {
log.FromContext(ctx).WithError(err).Fatal("Failed to get client")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/registry/cmd/count/revisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func revisionsCommand() *cobra.Command {
}
args[0] = c.FQName(args[0])

client, err := connection.NewRegistryClient(ctx)
client, err := connection.NewRegistryClientWithSettings(ctx, c)
if err != nil {
log.FromContext(ctx).WithError(err).Fatal("Failed to get client")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/registry/cmd/count/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func versionsCommand() *cobra.Command {
}
args[0] = c.FQName(args[0])

client, err := connection.NewRegistryClient(ctx)
client, err := connection.NewRegistryClientWithSettings(ctx, c)
if err != nil {
log.FromContext(ctx).WithError(err).Fatal("Failed to get client")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/registry/cmd/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func Command() *cobra.Command {
}
args[0] = c.FQName(args[0])

client, err := connection.NewRegistryClient(ctx)
client, err := connection.NewRegistryClientWithSettings(ctx, c)
if err != nil {
log.FromContext(ctx).WithError(err).Fatal("Failed to get client")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/registry/cmd/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func Command() *cobra.Command {
args[i] = c.FQName(args[i])
}

client, err := connection.NewRegistryClient(ctx)
client, err := connection.NewRegistryClientWithSettings(ctx, c)
if err != nil {
log.FromContext(ctx).WithError(err).Fatal("Failed to get client")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/registry/cmd/export/sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func sheetCommand() *cobra.Command {
}

var path string
client, err := connection.NewRegistryClient(ctx)
client, err := connection.NewRegistryClientWithSettings(ctx, c)
if err != nil {
log.FromContext(ctx).WithError(err).Fatal("Failed to get client")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/registry/cmd/export/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func yamlCommand() *cobra.Command {
}
args[0] = c.FQName(args[0])

client, err := connection.NewRegistryClient(ctx)
client, err := connection.NewRegistryClientWithSettings(ctx, c)
if err != nil {
log.FromContext(ctx).WithError(err).Fatal("Failed to get client")
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/registry/cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ func Command() *cobra.Command {
}
args[0] = c.FQName(args[0])

client, err := connection.NewRegistryClient(ctx)
client, err := connection.NewRegistryClientWithSettings(ctx, c)
if err != nil {
log.FromContext(ctx).WithError(err).Fatal("Failed to get client")
}
adminClient, err := connection.NewAdminClient(ctx)
adminClient, err := connection.NewAdminClientWithSettings(ctx, c)
if err != nil {
log.FromContext(ctx).WithError(err).Fatal("Failed to get client")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/registry/cmd/label/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func Command() *cobra.Command {
}
args[0] = c.FQName(args[0])

client, err := connection.NewRegistryClient(ctx)
client, err := connection.NewRegistryClientWithSettings(ctx, c)
if err != nil {
log.FromContext(ctx).WithError(err).Fatal("Failed to get client")
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/registry/cmd/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ func Command() *cobra.Command {
}
args[0] = c.FQName(args[0])

client, err := connection.NewRegistryClient(ctx)
client, err := connection.NewRegistryClientWithSettings(ctx, c)
if err != nil {
log.FromContext(ctx).WithError(err).Fatal("Failed to get client")
}
adminClient, err := connection.NewAdminClient(ctx)
adminClient, err := connection.NewAdminClientWithSettings(ctx, c)
if err != nil {
log.FromContext(ctx).WithError(err).Fatal("Failed to get client")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/registry/cmd/resolve/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func Command() *cobra.Command {
log.FromContext(ctx).WithError(err).Fatal("Invalid manifest resource name")
}

registryClient, err := connection.NewRegistryClient(ctx)
registryClient, err := connection.NewRegistryClientWithSettings(ctx, c)
if err != nil {
log.FromContext(ctx).WithError(err).Fatal("Failed to get client")
}
Expand Down
39 changes: 27 additions & 12 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,22 @@ import (

const ActivePointerFilename = "active_config"

var NoActiveConfigurationError = fmt.Errorf("No active configuration.")
var (
NoActiveConfigurationError = fmt.Errorf("No active configuration.")

// Flags defines Flags that may be bound to a Configuration. Use like:
// `cmd.PersistentFlags().AddFlagSet(connection.Flags)`
var Flags *pflag.FlagSet = CreateFlagSet()
// Flags defines Flags that may be bound to a Configuration. Use like:
// `cmd.PersistentFlags().AddFlagSet(connection.Flags)`
Flags *pflag.FlagSet = CreateFlagSet()

// Directory is $HOME/config/registry
var Directory string
var CannotDeleteActiveError = fmt.Errorf("Cannot delete active configuration")
var ReservedConfigNameError = fmt.Errorf("%q is reserved", ActivePointerFilename)
// Directory is $HOME/config/registry
Directory string
CannotDeleteActiveError = fmt.Errorf("Cannot delete active configuration")
ReservedConfigNameError = fmt.Errorf("%q is reserved", ActivePointerFilename)

envBindings = []string{"registry.address", "registry.insecure", "registry.token"}
envPrefix = "APG"
envKeyReplacer = strings.NewReplacer(".", "_")
)

func init() {
home, err := os.UserHomeDir()
Expand Down Expand Up @@ -190,6 +196,16 @@ func ReadValid(name string) (c Configuration, err error) {
return
}

for _, env := range envBindings {
if v.IsSet(env) {
evar := strings.ToUpper(envKeyReplacer.Replace(envPrefix + "." + env))
eval, _ := os.LookupEnv(evar)
if eval == v.GetString(env) {
fmt.Printf("WARN: deprecated env: %s=%q\n", evar, eval)
}
}
}

return
}

Expand Down Expand Up @@ -259,10 +275,9 @@ func ActiveName() (string, error) {
// Binds environment vars to populate config
func bindEnvs(v *viper.Viper) error {
v.AutomaticEnv()
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
v.SetEnvPrefix("APG")
bindings := []string{"registry.address", "registry.insecure", "registry.token"}
for _, env := range bindings {
v.SetEnvKeyReplacer(envKeyReplacer)
v.SetEnvPrefix(envPrefix)
for _, env := range envBindings {
if err := v.BindEnv(env); err != nil {
return err
}
Expand Down

0 comments on commit 8e82f10

Please sign in to comment.