cmd

package
v0.1.16 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 29, 2022 License: MIT Imports: 28 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Root = &cobra.Command{
		Use:           "honey",
		SilenceUsage:  true,
		SilenceErrors: true,
		Short:         "DevOps tool to help find an instance in sea of clouds",
		Version:       version,
		PersistentPreRunE: func(cmd *cobra.Command, args []string) error {

			if err := cmd.RegisterFlagCompletionFunc("k8s-namespace", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
				getter := genericclioptions.NewConfigFlags(true)
				flag := cmd.Flag("k8s-context").Value.String()
				if flag != "" {
					getter.Context = &flag
				}

				factory := cmdutil.NewFactory(getter)
				if client, err := factory.KubernetesClientSet(); err == nil {

					to := int64(3)
					cobra.CompDebugln(fmt.Sprintf("About to call kube client for namespaces with timeout of: %d", to), true)

					nsNames := []string{}
					if namespaces, err := client.CoreV1().Namespaces().List(context.Background(), metav1.ListOptions{TimeoutSeconds: &to}); err == nil {
						for _, ns := range namespaces.Items {
							if strings.HasPrefix(ns.Name, toComplete) {
								nsNames = append(nsNames, ns.Name)
							}
						}
						return nsNames, cobra.ShellCompDirectiveNoFileComp
					}
				}
				return nil, cobra.ShellCompDirectiveDefault
			}); err != nil {
				return err
			}

			return cmd.RegisterFlagCompletionFunc("k8s-context", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
				cobra.CompDebugln("About to get the different kube-contexts", true)

				loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()

				if config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
					loadingRules,
					&clientcmd.ConfigOverrides{}).RawConfig(); err == nil {
					ctxs := []string{}
					for name := range config.Contexts {
						if strings.HasPrefix(name, toComplete) {
							ctxs = append(ctxs, name)
						}
					}
					return ctxs, cobra.ShellCompDirectiveNoFileComp
				}
				return nil, cobra.ShellCompDirectiveNoFileComp
			})
		},
		RunE: func(cmd *cobra.Command, args []string) error {
			if len(args) > 0 {
				re, err := regexp.Compile(args[0])
				if err != nil {
					return errors.Wrap(err, "Failed to compile flags regexp")
				}

				flagsRe = re
				filter = args[0]
			}

			ctx := context.TODO()
			ci := place.GetConfig(ctx)

			backends, err := ci.Backends()
			if err != nil {
				return err
			}

			if len(backends) == 0 {
				return errors.New("oops you must specify at least one backend")
			}

			defer operations.CacheDB.Close()

			instances, err := operations.Find(context.TODO(), backends, filter)
			if err != nil {
				return err
			}

			return printers.Print(&printers.PrintInput{
				Data:    instances,
				Format:  ci.OutFormat,
				NoColor: ci.NoColor,
			})
		},
	}
)

Functions

func CheckArgs added in v0.1.9

func CheckArgs(minArgs, maxArgs int, cmd *cobra.Command, args []string)

CheckArgs checks there are enough arguments and prints a message if not

func Execute

func Execute()

Execute adds all child commands to the root command and sets flags appropriately. This is called by main.main(). It only needs to happen once to the Root.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL