2020-05-06 20:32:27 +00:00
|
|
|
/*
|
|
|
|
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 config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-06-11 15:45:29 -05:00
|
|
|
"sort"
|
2020-05-06 20:32:27 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2020-08-28 18:32:33 -05:00
|
|
|
"opendev.org/airship/airshipctl/pkg/config"
|
2020-05-06 20:32:27 +00:00
|
|
|
)
|
|
|
|
|
2021-05-06 14:46:42 +05:30
|
|
|
const (
|
|
|
|
getManagementConfigLong = `
|
|
|
|
Displays a specific management config information, or all defined management configs if no name is provided.
|
|
|
|
The information relates to reboot-delays and retry in seconds along with management-type that has to be used.
|
|
|
|
`
|
|
|
|
|
|
|
|
getManagementConfigExample = `
|
|
|
|
View all management configurations
|
|
|
|
# airshipctl config get-management-configs
|
2020-05-06 20:32:27 +00:00
|
|
|
|
2021-05-06 14:46:42 +05:30
|
|
|
View a specific management configuration named "default"
|
|
|
|
# airshipctl config get-management-config default
|
2020-05-06 20:32:27 +00:00
|
|
|
`
|
2021-05-06 14:46:42 +05:30
|
|
|
)
|
2020-05-06 20:32:27 +00:00
|
|
|
|
|
|
|
// NewGetManagementConfigCommand creates a command that enables printing a management configuration to stdout.
|
2020-08-28 18:32:33 -05:00
|
|
|
func NewGetManagementConfigCommand(cfgFactory config.Factory) *cobra.Command {
|
2020-05-06 20:32:27 +00:00
|
|
|
cmd := &cobra.Command{
|
2021-05-06 14:46:42 +05:30
|
|
|
Use: "get-management-config MGMT_CONFIG_NAME",
|
|
|
|
Short: "Airshipctl command to view management config(s) defined in the airshipctl config",
|
2021-05-03 17:02:24 +05:30
|
|
|
Long: getManagementConfigLong[1:],
|
2020-05-06 20:32:27 +00:00
|
|
|
Example: getManagementConfigExample,
|
|
|
|
Args: cobra.MaximumNArgs(1),
|
|
|
|
Aliases: []string{"get-management-configs"},
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2020-08-28 18:32:33 -05:00
|
|
|
cfg, err := cfgFactory()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-06 20:32:27 +00:00
|
|
|
if len(args) == 1 {
|
|
|
|
name := args[0]
|
|
|
|
|
2020-08-28 18:32:33 -05:00
|
|
|
config, err := cfg.GetManagementConfiguration(name)
|
2020-05-06 20:32:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(cmd.OutOrStdout(), "name: %s\n%s\n", name, config.String())
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-28 18:32:33 -05:00
|
|
|
if len(cfg.ManagementConfiguration) == 0 {
|
2020-05-06 20:32:27 +00:00
|
|
|
fmt.Fprintln(cmd.OutOrStdout(), "No management configurations defined.")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-11 15:45:29 -05:00
|
|
|
// Print all of the management configurations in order by name
|
2020-08-28 18:32:33 -05:00
|
|
|
keys := make([]string, 0, len(cfg.ManagementConfiguration))
|
|
|
|
for key := range cfg.ManagementConfiguration {
|
2020-06-11 15:45:29 -05:00
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
for _, key := range keys {
|
2020-08-28 18:32:33 -05:00
|
|
|
config := cfg.ManagementConfiguration[key]
|
2020-05-06 20:32:27 +00:00
|
|
|
fmt.Fprintf(cmd.OutOrStdout(), "name: %s\n%s\n", key, config.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|