prow/cmd/tackle/main.go
func selectContext(co contextOptions) (string, error) {
	fmt.Println("Existing kubernetes contexts:")
	// get cluster context
	_, cfg, err := contextConfig()
	if err != nil {
		logrus.WithError(err).Fatal("Failed to load ~/.kube/config from any obvious location")
	}
	// list contexts and ask to user to choose a context
	options := map[int]string{}

	var ctxs []string
	for ctx := range cfg.Contexts {
		ctxs = append(ctxs, ctx)
	}
	sort.Strings(ctxs)
	for idx, ctx := range ctxs {
		options[idx] = ctx
		if ctx == cfg.CurrentContext {
			fmt.Printf("* %d: %s (current)", idx, ctx)
		} else {
			fmt.Printf("  %d: %s", idx, ctx)
		}
		fmt.Println()
	}
	fmt.Println()
	choice := co.context
	switch {
	case choice != "":
		fmt.Println("Reuse " + choice + " context...")
	case co.create != "" || co.reuse != "":
		choice = "create"
		fmt.Println("Create new context...")
	default:
		fmt.Print("Choose context or [create new]: ")
		fmt.Scanln(&choice)
	}

	if choice == "create" || choice == "" || choice == "create new" || choice == "new" {
		ctx, err := createContext(co)
		if err != nil {
			return "", fmt.Errorf("create context: %v", err)
		}
		return ctx, nil
	}

	if _, ok := cfg.Contexts[choice]; ok {
		return choice, nil
	}

	idx, err := strconv.Atoi(choice)
	if err != nil {
		return "", fmt.Errorf("invalid context: %q", choice)
	}

	if ctx, ok := options[idx]; ok {
		return ctx, nil
	}

	return "", fmt.Errorf("invalid index: %d", idx)
}
func selectContext(co contextOptions) (string, error) {
	fmt.Println("Existing kubernetes contexts:")
	// get cluster context
	_, cfg, err := contextConfig()
	if err != nil {
		logrus.WithError(err).Fatal("Failed to load ~/.kube/config from any obvious location")
	}
	// list contexts and ask to user to choose a context
	options := map[int]string{}

	var ctxs []string
	for ctx := range cfg.Contexts {
		ctxs = append(ctxs, ctx)
	}
	sort.Strings(ctxs)
	for idx, ctx := range ctxs {
		options[idx] = ctx
		if ctx == cfg.CurrentContext {
			fmt.Printf("* %d: %s (current)", idx, ctx)
		} else {
			fmt.Printf("  %d: %s", idx, ctx)
		}
		fmt.Println()
	}
	fmt.Println()
	choice := co.context
	switch {
	case choice != "":
		fmt.Println("Reuse " + choice + " context...")
	case co.create != "" || co.reuse != "":
		choice = "create"
		fmt.Println("Create new context...")
	default:
		fmt.Print("Choose context or [create new]: ")
		fmt.Scanln(&choice)
	}

	if choice == "create" || choice == "" || choice == "create new" || choice == "new" {
		ctx, err := createContext(co)
		if err != nil {
			return "", fmt.Errorf("create context: %v", err)
		}
		return ctx, nil
	}

	if _, ok := cfg.Contexts[choice]; ok {
		return choice, nil
	}

	idx, err := strconv.Atoi(choice)
	if err != nil {
		return "", fmt.Errorf("invalid context: %q", choice)
	}

	if ctx, ok := options[idx]; ok {
		return ctx, nil
	}

	return "", fmt.Errorf("invalid index: %d", idx)
}
func selectContext ( co contextOptions ) ( string , error ) { fmt . Println ( " " ) // get cluster context _ , cfg , err := contextConfig ( ) if err != nil { logrus . WithError ( err ) . Fatal ( " " ) } // list contexts and ask to user to choose a context options := map [ int ] string { } var ctxs [ ] string for ctx := range cfg . Contexts { ctxs = append ( ctxs , ctx ) } sort . Strings ( ctxs ) for idx , ctx := range ctxs { options [ idx ] = ctx if ctx == cfg . CurrentContext { fmt . Printf ( " " , idx , ctx ) } else { fmt . Printf ( " " , idx , ctx ) } fmt . Println ( ) } fmt . Println ( ) choice := co . context switch { case choice != " " : fmt . Println ( " " + choice + " " ) case co . create != " " || co . reuse != " " : choice = " " fmt . Println ( " " ) default : fmt . Print ( " " ) fmt . Scanln ( & choice ) } if choice == " " || choice == " " || choice == " " || choice == " " { ctx , err := createContext ( co ) if err != nil { return " " , fmt . Errorf ( " " , err ) } return ctx , nil } if _ , ok := cfg . Contexts [ choice ] ; ok { return choice , nil } idx , err := strconv . Atoi ( choice ) if err != nil { return " " , fmt . Errorf ( " " , choice ) } if ctx , ok := options [ idx ] ; ok { return ctx , nil } return " " , fmt . Errorf ( " " , idx ) }
// selectContext allows the user to choose a context
// This may involve creating a cluster
selectContext allows the user to choose a context This may involve creating a cluster