-
Notifications
You must be signed in to change notification settings - Fork 838
/
checkout.go
68 lines (53 loc) · 1.74 KB
/
checkout.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
// Copyright 2024 Daytona Platforms Inc.
// SPDX-License-Identifier: Apache-2.0
package selection
import (
"fmt"
"os"
"github.com/daytonaio/daytona/pkg/views"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
)
type CheckoutOption struct {
Title string
Id string
}
var (
CheckoutDefault = CheckoutOption{Title: "Clone the default branch", Id: "default"}
CheckoutBranch = CheckoutOption{Title: "Branches", Id: "branch"}
CheckoutPR = CheckoutOption{Title: "Pull/Merge requests", Id: "pullrequest"}
)
func selectCheckoutPrompt(checkoutOptions []CheckoutOption, secondaryProjectOrder int, choiceChan chan<- string) {
items := []list.Item{}
for _, checkoutOption := range checkoutOptions {
newItem := item[string]{id: checkoutOption.Id, title: checkoutOption.Title, choiceProperty: checkoutOption.Id}
items = append(items, newItem)
}
l := views.GetStyledSelectList(items)
m := model[string]{list: l}
m.list.Title = "CLONING OPTIONS"
if secondaryProjectOrder > 0 {
m.list.Title += fmt.Sprintf(" (Secondary Project #%d)", secondaryProjectOrder)
}
p, err := tea.NewProgram(m, tea.WithAltScreen()).Run()
if err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
if m, ok := p.(model[string]); ok && m.choice != nil {
choiceChan <- *m.choice
} else {
choiceChan <- ""
}
}
func GetCheckoutOptionFromPrompt(secondaryProjectOrder int, checkoutOptions []CheckoutOption) CheckoutOption {
choiceChan := make(chan string)
go selectCheckoutPrompt(checkoutOptions, secondaryProjectOrder, choiceChan)
checkoutOptionId := <-choiceChan
for _, checkoutOption := range checkoutOptions {
if checkoutOption.Id == checkoutOptionId {
return checkoutOption
}
}
return CheckoutDefault
}