-
Notifications
You must be signed in to change notification settings - Fork 11
/
input.go
74 lines (62 loc) · 2.15 KB
/
input.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package shared
import (
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/OctopusDeploy/cli/pkg/question"
"github.com/OctopusDeploy/cli/pkg/util"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/variables"
)
func PromptScopes(asker question.Asker, projectVariables *variables.VariableSet, flags *ScopeFlags, isPrompted bool) error {
var err error
if util.Empty(flags.EnvironmentsScopes.Value) {
flags.EnvironmentsScopes.Value, err = PromptScope(asker, "Environment", projectVariables.ScopeValues.Environments, nil)
if err != nil {
return err
}
}
flags.ProcessScopes.Value, err = PromptScope(asker, "Process", ConvertProcessScopesToReference(projectVariables.ScopeValues.Processes), nil)
if err != nil {
return err
}
if !isPrompted {
flags.ChannelScopes.Value, err = PromptScope(asker, "Channel", projectVariables.ScopeValues.Channels, nil)
if err != nil {
return err
}
flags.TargetScopes.Value, err = PromptScope(asker, "Target", projectVariables.ScopeValues.Machines, nil)
if err != nil {
return err
}
flags.RoleScopes.Value, err = PromptScope(asker, "Role", projectVariables.ScopeValues.Roles, nil)
if err != nil {
return err
}
flags.TagScopes.Value, err = PromptScope(asker, "Tag", projectVariables.ScopeValues.TenantTags, func(i *resources.ReferenceDataItem) string { return i.ID })
if err != nil {
return err
}
flags.StepScopes.Value, err = PromptScope(asker, "Step", projectVariables.ScopeValues.Actions, nil)
if err != nil {
return err
}
}
return nil
}
func PromptScope(ask question.Asker, scopeDescription string, items []*resources.ReferenceDataItem, displaySelector func(i *resources.ReferenceDataItem) string) ([]string, error) {
if displaySelector == nil {
displaySelector = func(i *resources.ReferenceDataItem) string { return i.Name }
}
if util.Empty(items) {
return nil, nil
}
var selectedItems []string
err := ask(&survey.MultiSelect{
Message: fmt.Sprintf("%s scope", scopeDescription),
Options: util.SliceTransform(items, displaySelector),
}, &selectedItems)
if err != nil {
return nil, err
}
return selectedItems, nil
}