Repository: apigee/registry (go)

Issue:
Remove cmd/registry/cmd/util.go
Move contents and delete. See: https://github.com/apigee/registry/pull/683#discussion_r948148193

Pull request:
Remove cmd/registry/cmd/util.go
Fixes #686

Patch:
diff --git a/cmd/registry/cmd/auth/print_token.go b/cmd/registry/cmd/auth/print_token.go
index 539208b..b378e0a 100644
--- a/cmd/registry/cmd/auth/print_token.go
+++ b/cmd/registry/cmd/auth/print_token.go
@@ -17,7 +17,7 @@ package auth
 import (
 	"fmt"
 
-	"github.com/apigee/registry/cmd/registry/cmd/util"
+	"github.com/apigee/registry/pkg/config"
 	"github.com/spf13/cobra"
 )
 
@@ -30,20 +30,23 @@ func printTokenCommand() *cobra.Command {
 			cmd.SilenceUsage = true
 			cmd.SilenceErrors = true
 
-			_, config, err := util.TargetConfiguration()
+			_, c, err := config.ActiveRaw()
 			if err != nil {
+				if err == config.NoActiveConfigurationError {
+					return fmt.Errorf(`No active configuration. Use 'registry config configurations' to manage.`)
+				}
 				return err
 			}
-			err = config.Resolve()
+			err = c.Resolve()
 			if err != nil {
 				return err
 			}
 
-			if config.TokenSource == "" {
+			if c.TokenSource == "" {
 				return fmt.Errorf("No token source found. Use `registry config set token-source` to define.")
 			}
 
-			token, err := genToken(config.TokenSource)
+			token, err := genToken(c.TokenSource)
 			if err != nil {
 				return err
 			}
diff --git a/cmd/registry/cmd/config/get.go b/cmd/registry/cmd/config/get.go
index 467231c..125c112 100644
--- a/cmd/registry/cmd/config/get.go
+++ b/cmd/registry/cmd/config/get.go
@@ -17,7 +17,7 @@ package config
 import (
 	"fmt"
 
-	"github.com/apigee/registry/cmd/registry/cmd/util"
+	"github.com/apigee/registry/pkg/config"
 	"github.com/spf13/cobra"
 )
 
@@ -29,22 +29,24 @@ func getCommand() *cobra.Command {
 		RunE: func(cmd *cobra.Command, args []string) error {
 			cmd.SilenceUsage = true
 			cmd.SilenceErrors = true
-			_, config, err := util.TargetConfiguration()
+			_, c, err := config.ActiveRaw()
 			if err != nil {
+				if err == config.NoActiveConfigurationError {
+					return fmt.Errorf(`No active configuration. Use 'registry config configurations' to manage.`)
+				}
 				return fmt.Errorf("Cannot read config: %v", err)
 			}
 
-			name := args[0]
-			if !contains(config.Properties(), name) {
-				return UnknownPropertyError{name}
+			if !contains(c.Properties(), args[0]) {
+				return UnknownPropertyError{args[0]}
 			}
 
-			m, err := config.FlatMap()
+			m, err := c.FlatMap()
 			if err != nil {
 				return fmt.Errorf("Cannot decode config: %v", err)
 			}
 
-			cmd.Println(m[name])
+			cmd.Println(m[args[0]])
 			return nil
 		},
 	}
diff --git a/cmd/registry/cmd/config/list.go b/cmd/registry/cmd/config/list.go
index af807e4..aa062f0 100644
--- a/cmd/registry/cmd/config/list.go
+++ b/cmd/registry/cmd/config/list.go
@@ -17,7 +17,7 @@ package config
 import (
 	"fmt"
 
-	"github.com/apigee/registry/cmd/registry/cmd/util"
+	"github.com/apigee/registry/pkg/config"
 	"github.com/spf13/cobra"
 )
 
@@ -29,16 +29,19 @@ func listCommand() *cobra.Command {
 		RunE: func(cmd *cobra.Command, _ []string) error {
 			cmd.SilenceUsage = true
 			cmd.SilenceErrors = true
-			target, config, err := util.TargetConfiguration()
+			target, c, err := config.ActiveRaw()
 			if err != nil {
+				if err == config.NoActiveConfigurationError {
+					return fmt.Errorf(`No active configuration. Use 'registry config configurations' to manage.`)
+				}
 				return fmt.Errorf("Cannot read config: %v", err)
 			}
 
-			m, err := config.FlatMap()
+			m, err := c.FlatMap()
 			if err != nil {
 				return fmt.Errorf("Cannot decode config: %v", err)
 			}
-			for _, p := range config.Properties() {
+			for _, p := range c.Properties() {
 				if m[p] != nil {
 					if sv := fmt.Sprintf("%v", m[p]); sv != "" {
 						cmd.Println(p, "=", sv)
diff --git a/cmd/registry/cmd/config/set.go b/cmd/registry/cmd/config/set.go
index 0db6af3..7b43c56 100644
--- a/cmd/registry/cmd/config/set.go
+++ b/cmd/registry/cmd/config/set.go
@@ -17,7 +17,7 @@ package config
 import (
 	"fmt"
 
-	"github.com/apigee/registry/cmd/registry/cmd/util"
+	"github.com/apigee/registry/pkg/config"
 	"github.com/spf13/cobra"
 )
 
@@ -29,21 +29,24 @@ func setCommand() *cobra.Command {
 		RunE: func(cmd *cobra.Command, args []string) error {
 			cmd.SilenceUsage = true
 			cmd.SilenceErrors = true
-			target, config, err := util.TargetConfiguration()
+			target, c, err := config.ActiveRaw()
 			if err != nil {
+				if err == config.NoActiveConfigurationError {
+					return fmt.Errorf(`No active configuration. Use 'registry config configurations' to manage.`)
+				}
 				return fmt.Errorf("Cannot read config: %v", err)
 			}
 
 			name, value := args[0], args[1]
-			if !contains(config.Properties(), name) {
+			if !contains(c.Properties(), name) {
 				return UnknownPropertyError{name}
 			}
 
-			if err = config.Set(name, value); err != nil {
+			if err = c.Set(name, value); err != nil {
 				return fmt.Errorf("Cannot set value: %v", err)
 			}
 
-			if err = config.Write(target); err != nil {
+			if err = c.Write(target); err != nil {
 				return fmt.Errorf("Cannot write config: %v", err)
 			}
 
diff --git a/cmd/registry/cmd/config/unset.go b/cmd/registry/cmd/config/unset.go
index 769a4c8..5f5db4a 100644
--- a/cmd/registry/cmd/config/unset.go
+++ b/cmd/registry/cmd/config/unset.go
@@ -17,7 +17,7 @@ package config
 import (
 	"fmt"
 
-	"github.com/apigee/registry/cmd/registry/cmd/util"
+	"github.com/apigee/registry/pkg/config"
 	"github.com/spf13/cobra"
 )
 
@@ -29,16 +29,19 @@ func unsetCommand() *cobra.Command {
 		RunE: func(cmd *cobra.Command, args []string) error {
 			cmd.SilenceUsage = true
 			cmd.SilenceErrors = true
-			target, config, err := util.TargetConfiguration()
+			target, c, err := config.ActiveRaw()
 			if err != nil {
+				if err == config.NoActiveConfigurationError {
+					return fmt.Errorf(`No active configuration. Use 'registry config configurations' to manage.`)
+				}
 				return fmt.Errorf("Cannot read config: %v", err)
 			}
 
-			if err = config.Unset(args[0]); err != nil {
+			if err = c.Unset(args[0]); err != nil {
 				return fmt.Errorf("Cannot unset property: %v", err)
 			}
 
-			if err = config.Write(target); err != nil {
+			if err = c.Write(target); err != nil {
 				return fmt.Errorf("Cannot write config: %v", err)
 			}
 
diff --git a/cmd/registry/cmd/util/util.go b/cmd/registry/cmd/util/util.go
deleted file mode 100644
index 955caa7..0000000
--- a/cmd/registry/cmd/util/util.go
+++ /dev/null
@@ -1,32 +0,0 @@
-// Copyright 2022 Google LLC. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//    http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package util
-
-import (
-	"fmt"
-
-	"github.com/apigee/registry/pkg/config"
-)
-
-var NoActiveConfigurationError = fmt.Errorf(`No active configuration. Use 'registry config configurations' to manage.`)
-
-func TargetConfiguration() (name string, c config.Configuration, err error) {
-	name, c, err = config.ActiveRaw()
-	if name == "" {
-		return name, config.Configuration{}, NoActiveConfigurationError
-	}
-
-	return
-}
diff --git a/pkg/config/config.go b/pkg/config/config.go
index bbee70a..3e5fe9c 100644
--- a/pkg/config/config.go
+++ b/pkg/config/config.go
@@ -31,6 +31,8 @@ import (
 
 const ActivePointerFilename = "active_config"
 
+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()
@@ -125,6 +127,9 @@ func ActiveRaw() (name string, c Configuration, err error) {
 			return name, Configuration{}, err
 		}
 	}
+	if name == "" {
+		return name, Configuration{}, NoActiveConfigurationError
+	}
 
 	c, err = Read(name)
 	return


Test patch:
diff --git a/cmd/registry/cmd/config/config_test.go b/cmd/registry/cmd/config/config_test.go
index db5615c..1f6babc 100644
--- a/cmd/registry/cmd/config/config_test.go
+++ b/cmd/registry/cmd/config/config_test.go
@@ -36,8 +36,9 @@ func TestCommand(t *testing.T) {
 func TestNoActiveConfig(t *testing.T) {
 	t.Cleanup(test.CleanConfigDir(t))
 
-	checkErr := func(err error) {
-		want := fmt.Errorf(`Cannot read config: No active configuration. Use 'registry config configurations' to manage.`)
+	checkErr := func(t *testing.T, err error) {
+		t.Helper()
+		want := fmt.Errorf(`No active configuration. Use 'registry config configurations' to manage.`)
 		if err == nil {
 			t.Errorf("expected error: %s", want)
 		} else if diff := cmp.Diff(want.Error(), err.Error()); diff != "" {
@@ -60,18 +61,18 @@ func TestNoActiveConfig(t *testing.T) {
 
 			// missing directory
 			config.Directory = filepath.Join(config.Directory, "test")
-			checkErr(e.cmd.Execute())
+			checkErr(t, e.cmd.Execute())
 
 			// empty list
 			config.Directory = t.TempDir()
-			checkErr(e.cmd.Execute())
+			checkErr(t, e.cmd.Execute())
 
 			// no active
 			c := config.Configuration{}
 			if err := c.Write("test"); err != nil {
 				t.Fatal(err)
 			}
-			checkErr(e.cmd.Execute())
+			checkErr(t, e.cmd.Execute())
 		})
 	}
 }