...

Source file src/github.com/redhat-developer/odo/pkg/binding/asker/survey_asker.go

Documentation: github.com/redhat-developer/odo/pkg/binding/asker

     1  package asker
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  
     7  	"github.com/AlecAivazis/survey/v2"
     8  )
     9  
    10  const (
    11  	bindAsFiles  = "Bind As Files"
    12  	bindAsEnvVar = "Bind As Environment Variables"
    13  )
    14  
    15  const (
    16  	namingStrategyDefault   = "DEFAULT"
    17  	namingStrategyNone      = "none"
    18  	namingStrategylowercase = "lowercase"
    19  	namingStrategyUpperCase = "uppercase"
    20  	NamingStrategyCustom    = "CUSTOM"
    21  )
    22  
    23  type Survey struct{}
    24  
    25  var _ Asker = (*Survey)(nil)
    26  
    27  func NewSurveyAsker() *Survey {
    28  	return &Survey{}
    29  }
    30  
    31  func (s *Survey) SelectNamespaceListOption() (ServiceInstancesNamespaceListOption, error) {
    32  	const (
    33  		currentNsOption       = "current namespace"
    34  		allAccessibleNsOption = "all accessible namespaces"
    35  	)
    36  	question := &survey.Select{
    37  		Message: "Do you want to list services from:",
    38  		Options: []string{currentNsOption, allAccessibleNsOption},
    39  	}
    40  	var answer string
    41  	err := survey.AskOne(question, &answer)
    42  	if err != nil {
    43  		return 0, err
    44  	}
    45  
    46  	switch answer {
    47  	case currentNsOption:
    48  		return CurrentNamespace, nil
    49  	case allAccessibleNsOption:
    50  		return AllAccessibleNamespaces, nil
    51  	default:
    52  		return 0, fmt.Errorf("unknown namespace list option: %s", answer)
    53  	}
    54  }
    55  
    56  func (s *Survey) AskNamespace() (string, error) {
    57  	question := &survey.Input{
    58  		Message: "Enter the namespace containing the service instances or press Enter to use the current namespace:",
    59  		Default: "",
    60  	}
    61  	var answer string
    62  	err := survey.AskOne(question, &answer)
    63  	if err != nil {
    64  		return "", err
    65  	}
    66  	return answer, nil
    67  }
    68  
    69  func (s *Survey) SelectNamespace(options []string) (string, error) {
    70  	question := &survey.Select{
    71  		Message: "Select the namespace containing the service instances:",
    72  		Options: options,
    73  	}
    74  	var answer string
    75  	err := survey.AskOne(question, &answer)
    76  	if err != nil {
    77  		return "", err
    78  	}
    79  	return answer, nil
    80  }
    81  
    82  func (s *Survey) SelectWorkloadResource(options []string) (int, error) {
    83  	question := &survey.Select{
    84  		Message: "Select workload resource you want to bind:",
    85  		Options: options,
    86  	}
    87  	var answer int
    88  	err := survey.AskOne(question, &answer)
    89  	if err != nil {
    90  		return 0, err
    91  	}
    92  	return answer, nil
    93  }
    94  
    95  func (s *Survey) SelectWorkloadResourceName(names []string) (bool, string, error) {
    96  	sort.Strings(names)
    97  	notFoundOption := "DOES NOT EXIST YET"
    98  	goBackOption := "** GO BACK **"
    99  	names = append(names, notFoundOption, goBackOption)
   100  	question := &survey.Select{
   101  		Message: "Select workload resource name you want to bind:",
   102  		Options: names,
   103  	}
   104  	var answer string
   105  	err := survey.AskOne(question, &answer)
   106  	if err != nil {
   107  		return false, "", err
   108  	}
   109  	if answer == notFoundOption {
   110  		return false, "", nil
   111  	}
   112  	if answer == goBackOption {
   113  		return true, "", nil
   114  	}
   115  	return false, answer, nil
   116  }
   117  
   118  func (s *Survey) SelectNamingStrategy() (string, error) {
   119  	question := &survey.Select{
   120  		Message: "Select naming strategy for binding names:",
   121  		Options: []string{namingStrategyDefault, namingStrategyNone, namingStrategylowercase, namingStrategyUpperCase, NamingStrategyCustom},
   122  	}
   123  	var answer string
   124  	err := survey.AskOne(question, &answer)
   125  	if err != nil {
   126  		return "", err
   127  	}
   128  	if answer == namingStrategyDefault {
   129  		return "", nil
   130  	}
   131  	return answer, nil
   132  }
   133  
   134  func (s *Survey) AskWorkloadResourceName() (string, error) {
   135  	question := &survey.Input{
   136  		Message: "Enter the Workload's name:",
   137  		Default: "",
   138  	}
   139  	var answer string
   140  	err := survey.AskOne(question, &answer)
   141  	if err != nil {
   142  		return "", err
   143  	}
   144  	return answer, nil
   145  }
   146  
   147  func (s *Survey) AskServiceInstance(serviceInstances []string) (string, error) {
   148  	sort.Strings(serviceInstances)
   149  	question := &survey.Select{
   150  		Message: "Select service instance you want to bind to:",
   151  		Options: serviceInstances,
   152  	}
   153  	var answer string
   154  	err := survey.AskOne(question, &answer)
   155  	if err != nil {
   156  		return "", err
   157  	}
   158  	return answer, nil
   159  }
   160  
   161  func (s *Survey) AskServiceBindingName(defaultName string) (string, error) {
   162  	question := &survey.Input{
   163  		Message: "Enter the Binding's name:",
   164  		Default: defaultName,
   165  	}
   166  	var answer string
   167  	err := survey.AskOne(question, &answer)
   168  	if err != nil {
   169  		return "", err
   170  	}
   171  	return answer, nil
   172  }
   173  
   174  func (o *Survey) AskBindAsFiles() (bool, error) {
   175  	question := &survey.Select{
   176  		Message: "How do you want to bind the service?",
   177  		Options: []string{bindAsFiles, bindAsEnvVar},
   178  	}
   179  	var answer string
   180  	err := survey.AskOne(question, &answer)
   181  	if err != nil {
   182  		return true, err
   183  	}
   184  
   185  	var bindAsFile bool
   186  	if answer == bindAsFiles {
   187  		bindAsFile = true
   188  	}
   189  	return bindAsFile, nil
   190  }
   191  
   192  func (o *Survey) AskNamingStrategy() (string, error) {
   193  	question := &survey.Input{
   194  		Message: "Enter the naming strategy:",
   195  		Default: "",
   196  	}
   197  	var answer string
   198  	err := survey.AskOne(question, &answer)
   199  	if err != nil {
   200  		return "", err
   201  	}
   202  	return answer, nil
   203  }
   204  
   205  func (o *Survey) SelectCreationOptions() ([]CreationOption, error) {
   206  	options := []int{}
   207  	prompt := &survey.MultiSelect{
   208  		Message: "Check(with Space Bar) one or more operations to perform with the ServiceBinding:",
   209  		Options: []string{"create it on cluster", "display it", "save it to file"}, // respect order of CreationOption constants
   210  		Help:    "Use the Space Bar to select one or more operations to perform with the ServiceBinding",
   211  	}
   212  	err := survey.AskOne(prompt, &options)
   213  	if err != nil {
   214  		return nil, err
   215  	}
   216  	result := make([]CreationOption, 0, len(options))
   217  	for _, option := range options {
   218  		result = append(result, CreationOption(option))
   219  	}
   220  	return result, nil
   221  }
   222  
   223  func (o *Survey) AskOutputFilePath(defaultValue string) (string, error) {
   224  	question := &survey.Input{
   225  		Message: "Save the ServiceBinding to file:",
   226  		Default: defaultValue,
   227  	}
   228  	var answer string
   229  	err := survey.AskOne(question, &answer)
   230  	if err != nil {
   231  		return "", err
   232  	}
   233  	return answer, nil
   234  }
   235  

View as plain text