forked from keboola/keboola-as-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_instance.go
55 lines (47 loc) · 1.8 KB
/
template_instance.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
package dialog
import (
"fmt"
"github.com/keboola/keboola-as-code/internal/pkg/model"
"github.com/keboola/keboola-as-code/internal/pkg/project"
"github.com/keboola/keboola-as-code/internal/pkg/service/cli/prompt"
"github.com/keboola/keboola-as-code/internal/pkg/service/common/configmap"
"github.com/keboola/keboola-as-code/internal/pkg/utils/errors"
)
// AskTemplateInstance - dialog to select template instance.
func (p *Dialogs) AskTemplateInstance(projectState *project.State, branchName configmap.Value[string], instanceID configmap.Value[string]) (branchKey model.BranchKey, instance *model.TemplateInstance, err error) {
// Branch
branch, err := p.SelectBranch(projectState.LocalObjects().Branches(), `Select branch`, branchName)
if err != nil {
return branchKey, instance, err
}
// Template instance
instance, err = p.selectTemplateInstance(branch, `Select template instance`, instanceID)
return branch.BranchKey, instance, err
}
func (p *Dialogs) selectTemplateInstance(branch *model.Branch, label string, instanceID configmap.Value[string]) (*model.TemplateInstance, error) {
if instanceID.IsSet() {
usage, found, err := branch.Metadata.TemplateInstance(instanceID.Value)
if err != nil {
return nil, err
}
if found {
return usage, nil
}
return nil, errors.Errorf(`template instance "%s" was not found in branch "%s"`, instanceID.Value, branch.Name)
}
all, err := branch.Metadata.TemplatesInstances()
if err != nil {
return nil, err
}
selectOpts := make([]string, 0)
for _, u := range all {
selectOpts = append(selectOpts, fmt.Sprintf(`%s %s (%s)`, u.TemplateID, u.Version, u.InstanceID))
}
if index, ok := p.SelectIndex(&prompt.SelectIndex{
Label: label,
Options: selectOpts,
}); ok {
return &all[index], nil
}
return nil, errors.New(`please specify template instance`)
}