2019-12-04 17:19:06 -05: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"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"opendev.org/airship/airshipctl/pkg/config"
|
|
|
|
)
|
|
|
|
|
2020-04-15 16:59:49 -05:00
|
|
|
const (
|
|
|
|
getContextLong = `
|
2021-05-06 14:46:42 +05:30
|
|
|
Displays information about contexts such as associated manifests, users, and clusters. It would display a specific
|
|
|
|
context information, or all defined context information if no name is provided.
|
2020-04-15 16:59:49 -05:00
|
|
|
`
|
2019-12-04 17:19:06 -05:00
|
|
|
|
2020-04-15 16:59:49 -05:00
|
|
|
getContextExample = `
|
2021-05-06 14:46:42 +05:30
|
|
|
List all contexts
|
|
|
|
# airshipctl config get-contexts
|
2019-12-04 17:19:06 -05:00
|
|
|
|
2021-05-06 14:46:42 +05:30
|
|
|
Display the current context
|
|
|
|
# airshipctl config get-context --current
|
2019-12-04 17:19:06 -05:00
|
|
|
|
2021-05-06 14:46:42 +05:30
|
|
|
Display a specific context
|
|
|
|
# airshipctl config get-context exampleContext
|
2020-04-15 16:59:49 -05:00
|
|
|
`
|
2019-12-04 17:19:06 -05:00
|
|
|
)
|
|
|
|
|
2020-04-15 16:59:49 -05:00
|
|
|
// NewGetContextCommand creates a command for viewing cluster information
|
|
|
|
// defined in the airshipctl config file.
|
2020-08-28 18:32:33 -05:00
|
|
|
func NewGetContextCommand(cfgFactory config.Factory) *cobra.Command {
|
2020-02-07 16:27:05 -06:00
|
|
|
o := &config.ContextOptions{}
|
|
|
|
cmd := &cobra.Command{
|
2021-04-16 19:03:54 +05:30
|
|
|
Use: "get-context CONTEXT_NAME",
|
2021-05-06 14:46:42 +05:30
|
|
|
Short: "Airshipctl command to get context(s) information from the airshipctl config",
|
2020-04-15 16:59:49 -05:00
|
|
|
Long: getContextLong[1:],
|
2019-12-04 17:19:06 -05:00
|
|
|
Example: getContextExample,
|
2021-04-16 19:03:54 +05:30
|
|
|
Args: cobra.MaximumNArgs(1),
|
2020-05-06 14:35:29 -05:00
|
|
|
Aliases: []string{"get-contexts"},
|
2019-12-04 17:19:06 -05:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2020-08-28 18:32:33 -05:00
|
|
|
airconfig, err := cfgFactory()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-04 17:19:06 -05:00
|
|
|
if len(args) == 1 {
|
2020-02-07 16:27:05 -06:00
|
|
|
o.Name = args[0]
|
2019-12-04 17:19:06 -05:00
|
|
|
}
|
2020-03-18 18:16:56 -05:00
|
|
|
|
2021-03-17 22:24:27 +05:30
|
|
|
if len(airconfig.GetContexts()) == 0 {
|
|
|
|
fmt.Fprintln(cmd.OutOrStdout(), "No Contexts found in the configuration.")
|
|
|
|
} else {
|
|
|
|
return o.Print(airconfig, cmd.OutOrStdout())
|
2020-03-18 18:16:56 -05:00
|
|
|
}
|
|
|
|
return nil
|
2019-12-04 17:19:06 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-02-07 16:27:05 -06:00
|
|
|
addGetContextFlags(o, cmd)
|
|
|
|
return cmd
|
2019-12-04 17:19:06 -05:00
|
|
|
}
|
|
|
|
|
2020-02-07 16:27:05 -06:00
|
|
|
func addGetContextFlags(o *config.ContextOptions, cmd *cobra.Command) {
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
2021-05-06 14:46:42 +05:30
|
|
|
flags.BoolVar(&o.CurrentContext, "current", false, "get the current context")
|
|
|
|
flags.StringVar(&o.Format, "format", "yaml",
|
|
|
|
"supported output format `yaml` or `table`, default is `yaml`")
|
2019-12-04 17:19:06 -05:00
|
|
|
}
|