[#27] Move execution logic from cmd to pkg
Moves all execution logic from cmd/config to pkg/config. Adds tests for moved execution logic. Change-Id: I6381c9fe9eeba938e855bf7d7ea52cd22a5c08b6
This commit is contained in:
@@ -17,9 +17,6 @@ limitations under the License.
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"opendev.org/airship/airshipctl/pkg/config"
|
||||
@@ -49,41 +46,9 @@ func NewCmdConfigGetAuthInfo(rootSettings *environment.AirshipCTLSettings) *cobr
|
||||
if len(args) == 1 {
|
||||
theAuthInfo.Name = args[0]
|
||||
}
|
||||
return runGetAuthInfo(theAuthInfo, cmd.OutOrStdout(), rootSettings.Config())
|
||||
return config.RunGetAuthInfo(theAuthInfo, cmd.OutOrStdout(), rootSettings.Config())
|
||||
},
|
||||
}
|
||||
|
||||
return getauthinfocmd
|
||||
}
|
||||
|
||||
// runGetAuthInfo performs the execution of 'config get-credentials' sub command
|
||||
func runGetAuthInfo(o *config.AuthInfoOptions, out io.Writer, airconfig *config.Config) error {
|
||||
if o.Name == "" {
|
||||
return getAuthInfos(out, airconfig)
|
||||
}
|
||||
return getAuthInfo(o, out, airconfig)
|
||||
}
|
||||
|
||||
func getAuthInfo(o *config.AuthInfoOptions, out io.Writer, airconfig *config.Config) error {
|
||||
cName := o.Name
|
||||
authinfo, err := airconfig.GetAuthInfo(cName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintln(out, authinfo)
|
||||
return nil
|
||||
}
|
||||
|
||||
func getAuthInfos(out io.Writer, airconfig *config.Config) error {
|
||||
authinfos, err := airconfig.GetAuthInfos()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(authinfos) == 0 {
|
||||
fmt.Fprintln(out, "No User credentials found in the configuration.")
|
||||
}
|
||||
for _, authinfo := range authinfos {
|
||||
fmt.Fprintln(out, authinfo)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
@@ -48,7 +47,13 @@ func NewCmdConfigGetCluster(rootSettings *environment.AirshipCTLSettings) *cobra
|
||||
if len(args) == 1 {
|
||||
theCluster.Name = args[0]
|
||||
}
|
||||
return runGetCluster(theCluster, cmd.OutOrStdout(), rootSettings)
|
||||
|
||||
err := validate(theCluster)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return config.RunGetCluster(theCluster, cmd.OutOrStdout(), rootSettings.Config())
|
||||
},
|
||||
}
|
||||
|
||||
@@ -62,47 +67,6 @@ func gcInitFlags(o *config.ClusterOptions, getclustercmd *cobra.Command) {
|
||||
config.FlagClusterType+" for the cluster entry in airshipctl config")
|
||||
}
|
||||
|
||||
// runGetCluster performs the execution of 'config get-cluster' sub command
|
||||
func runGetCluster(o *config.ClusterOptions, out io.Writer, rootSettings *environment.AirshipCTLSettings) error {
|
||||
err := validate(o)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if o.Name == "" {
|
||||
return getClusters(out, rootSettings)
|
||||
}
|
||||
return getCluster(o.Name, o.ClusterType, out, rootSettings)
|
||||
}
|
||||
|
||||
func getCluster(cName, cType string,
|
||||
out io.Writer, rootSettings *environment.AirshipCTLSettings) error {
|
||||
airconfig := rootSettings.Config()
|
||||
cluster, err := airconfig.GetCluster(cName, cType)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(out, "%s", cluster.PrettyString())
|
||||
return nil
|
||||
}
|
||||
|
||||
func getClusters(out io.Writer, rootSettings *environment.AirshipCTLSettings) error {
|
||||
airconfig := rootSettings.Config()
|
||||
clusters, err := airconfig.GetClusters()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(clusters) == 0 {
|
||||
fmt.Fprintln(out, "No clusters found in the configuration.")
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, cluster := range clusters {
|
||||
fmt.Fprintf(out, "%s\n", cluster.PrettyString())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validate(o *config.ClusterOptions) error {
|
||||
// Only an error if asking for a specific cluster
|
||||
if len(o.Name) == 0 {
|
||||
|
||||
@@ -18,7 +18,6 @@ package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
@@ -55,7 +54,7 @@ func NewCmdConfigGetContext(rootSettings *environment.AirshipCTLSettings) *cobra
|
||||
if len(args) == 1 {
|
||||
theContext.Name = args[0]
|
||||
}
|
||||
return runGetContext(theContext, cmd.OutOrStdout(), rootSettings.Config())
|
||||
return config.RunGetContext(theContext, cmd.OutOrStdout(), rootSettings.Config())
|
||||
},
|
||||
}
|
||||
|
||||
@@ -68,38 +67,3 @@ func gctxInitFlags(o *config.ContextOptions, getcontextcmd *cobra.Command) {
|
||||
getcontextcmd.Flags().BoolVar(&o.CurrentContext, config.FlagCurrentContext, false,
|
||||
config.FlagCurrentContext+" to retrieve the current context entry in airshipctl config")
|
||||
}
|
||||
|
||||
// runGetContext performs the execution of 'config get-Context' sub command
|
||||
func runGetContext(o *config.ContextOptions, out io.Writer, airconfig *config.Config) error {
|
||||
if o.Name == "" && !o.CurrentContext {
|
||||
return getContexts(out, airconfig)
|
||||
}
|
||||
return getContext(o, out, airconfig)
|
||||
}
|
||||
|
||||
func getContext(o *config.ContextOptions, out io.Writer, airconfig *config.Config) error {
|
||||
cName := o.Name
|
||||
if o.CurrentContext {
|
||||
cName = airconfig.CurrentContext
|
||||
}
|
||||
context, err := airconfig.GetContext(cName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(out, "%s", context.PrettyString())
|
||||
return nil
|
||||
}
|
||||
|
||||
func getContexts(out io.Writer, airconfig *config.Config) error {
|
||||
contexts, err := airconfig.GetContexts()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(contexts) == 0 {
|
||||
fmt.Fprintln(out, "No Contexts found in the configuration.")
|
||||
}
|
||||
for _, context := range contexts {
|
||||
fmt.Fprintf(out, "%s", context.PrettyString())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@ limitations under the License.
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
@@ -78,7 +77,7 @@ func NewCmdConfigSetAuthInfo(rootSettings *environment.AirshipCTLSettings) *cobr
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
theAuthInfo.Name = args[0]
|
||||
modified, err := runSetAuthInfo(theAuthInfo, rootSettings.Config())
|
||||
modified, err := config.RunSetAuthInfo(theAuthInfo, rootSettings.Config(), true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -127,36 +126,3 @@ func suInitFlags(o *config.AuthInfoOptions, setauthinfo *cobra.Command) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func runSetAuthInfo(o *config.AuthInfoOptions, airconfig *config.Config) (bool, error) {
|
||||
authinfoWasModified := false
|
||||
err := o.Validate()
|
||||
if err != nil {
|
||||
return authinfoWasModified, err
|
||||
}
|
||||
|
||||
authinfoIWant := o.Name
|
||||
authinfo, err := airconfig.GetAuthInfo(authinfoIWant)
|
||||
if err != nil {
|
||||
var cerr config.ErrMissingConfig
|
||||
if !errors.As(err, &cerr) {
|
||||
// An error occurred, but it wasn't a "missing" config error.
|
||||
return authinfoWasModified, err
|
||||
}
|
||||
|
||||
// authinfo didn't exist, create it
|
||||
// ignoring the returned added authinfo
|
||||
airconfig.AddAuthInfo(o)
|
||||
} else {
|
||||
// AuthInfo exists, lets update
|
||||
airconfig.ModifyAuthInfo(authinfo, o)
|
||||
authinfoWasModified = true
|
||||
}
|
||||
// Update configuration file just in time persistence approach
|
||||
if err := airconfig.PersistConfig(); err != nil {
|
||||
// Error that it didnt persist the changes
|
||||
return authinfoWasModified, config.ErrConfigFailed{}
|
||||
}
|
||||
|
||||
return authinfoWasModified, nil
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@ limitations under the License.
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
@@ -68,7 +67,7 @@ func NewCmdConfigSetCluster(rootSettings *environment.AirshipCTLSettings) *cobra
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
theCluster.Name = cmd.Flags().Args()[0]
|
||||
modified, err := runSetCluster(theCluster, rootSettings)
|
||||
modified, err := config.RunSetCluster(theCluster, rootSettings.Config(), true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -107,46 +106,3 @@ func scInitFlags(o *config.ClusterOptions, setclustercmd *cobra.Command) {
|
||||
setclustercmd.Flags().BoolVar(&o.EmbedCAData, config.FlagEmbedCerts, false,
|
||||
config.FlagEmbedCerts+" for the cluster entry in airshipctl config")
|
||||
}
|
||||
|
||||
func runSetCluster(o *config.ClusterOptions, rootSettings *environment.AirshipCTLSettings) (bool, error) {
|
||||
clusterWasModified := false
|
||||
err := o.Validate()
|
||||
if err != nil {
|
||||
return clusterWasModified, err
|
||||
}
|
||||
|
||||
airconfig := rootSettings.Config()
|
||||
cluster, err := airconfig.GetCluster(o.Name, o.ClusterType)
|
||||
if err != nil {
|
||||
var cerr config.ErrMissingConfig
|
||||
if !errors.As(err, &cerr) {
|
||||
// An error occurred, but it wasn't a "missing" config error.
|
||||
return clusterWasModified, err
|
||||
}
|
||||
|
||||
// Cluster didn't exist, create it
|
||||
_, err := airconfig.AddCluster(o)
|
||||
if err != nil {
|
||||
return clusterWasModified, err
|
||||
}
|
||||
clusterWasModified = false
|
||||
} else {
|
||||
// Cluster exists, lets update
|
||||
_, err := airconfig.ModifyCluster(cluster, o)
|
||||
if err != nil {
|
||||
return clusterWasModified, err
|
||||
}
|
||||
clusterWasModified = true
|
||||
}
|
||||
|
||||
// Update configuration file
|
||||
// Just in time persistence approach
|
||||
if err := airconfig.PersistConfig(); err != nil {
|
||||
// Some warning here , that it didnt persist the changes because of this
|
||||
// Or should we float this up
|
||||
// What would it mean? No value.
|
||||
return clusterWasModified, err
|
||||
}
|
||||
|
||||
return clusterWasModified, nil
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@ limitations under the License.
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
@@ -58,7 +57,7 @@ func NewCmdConfigSetContext(rootSettings *environment.AirshipCTLSettings) *cobra
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
theContext.Name = cmd.Flags().Args()[0]
|
||||
modified, err := runSetContext(theContext, rootSettings.Config())
|
||||
modified, err := config.RunSetContext(theContext, rootSettings.Config(), true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -94,45 +93,3 @@ func sctxInitFlags(o *config.ContextOptions, setcontextcmd *cobra.Command) {
|
||||
setcontextcmd.Flags().StringVar(&o.ClusterType, config.FlagClusterType, "",
|
||||
config.FlagClusterType+" for the context entry in airshipctl config")
|
||||
}
|
||||
|
||||
func runSetContext(o *config.ContextOptions, airconfig *config.Config) (bool, error) {
|
||||
contextWasModified := false
|
||||
err := o.Validate()
|
||||
if err != nil {
|
||||
return contextWasModified, err
|
||||
}
|
||||
|
||||
contextIWant := o.Name
|
||||
context, err := airconfig.GetContext(contextIWant)
|
||||
if err != nil {
|
||||
var cerr config.ErrMissingConfig
|
||||
if !errors.As(err, &cerr) {
|
||||
// An error occurred, but it wasn't a "missing" config error.
|
||||
return contextWasModified, err
|
||||
}
|
||||
|
||||
if o.CurrentContext {
|
||||
return contextWasModified, config.ErrMissingConfig{}
|
||||
}
|
||||
// context didn't exist, create it
|
||||
// ignoring the returned added context
|
||||
airconfig.AddContext(o)
|
||||
} else {
|
||||
// Found the desired Current Context
|
||||
// Lets update it and be done.
|
||||
if o.CurrentContext {
|
||||
airconfig.CurrentContext = o.Name
|
||||
} else {
|
||||
// Context exists, lets update
|
||||
airconfig.ModifyContext(context, o)
|
||||
}
|
||||
contextWasModified = true
|
||||
}
|
||||
// Update configuration file just in time persistence approach
|
||||
if err := airconfig.PersistConfig(); err != nil {
|
||||
// Error that it didnt persist the changes
|
||||
return contextWasModified, config.ErrConfigFailed{}
|
||||
}
|
||||
|
||||
return contextWasModified, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user