Skip to content

Commit

Permalink
feat: Filter param daytona create tui (#862)
Browse files Browse the repository at this point in the history
Signed-off-by: abhishek818 <[email protected]>
  • Loading branch information
abhishek818 authored Sep 12, 2024
1 parent 77a6393 commit 03e9d34
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 28 deletions.
11 changes: 7 additions & 4 deletions pkg/cmd/workspace/util/branch_wizard.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package util
import (
"context"
"errors"
"fmt"
"net/url"

apiclient_util "github.com/daytonaio/daytona/internal/util/apiclient"
Expand All @@ -18,6 +19,7 @@ type BranchWizardConfig struct {
ApiClient *apiclient.APIClient
ProviderId string
NamespaceId string
Namespace string
ChosenRepo *apiclient.GitRepository
ProjectOrder int
}
Expand Down Expand Up @@ -58,8 +60,9 @@ func SetBranchFromWizard(config BranchWizardConfig) (*apiclient.GitRepository, e
}

var branch *apiclient.GitBranch
parentIdentifier := fmt.Sprintf("%s/%s/%s", config.ProviderId, config.Namespace, config.ChosenRepo.Name)
if len(prList) == 0 {
branch = selection.GetBranchFromPrompt(branchList, config.ProjectOrder)
branch = selection.GetBranchFromPrompt(branchList, config.ProjectOrder, parentIdentifier)
if branch == nil {
return nil, errors.New("must select a branch")
}
Expand All @@ -74,7 +77,7 @@ func SetBranchFromWizard(config BranchWizardConfig) (*apiclient.GitRepository, e
checkoutOptions = append(checkoutOptions, selection.CheckoutBranch)
checkoutOptions = append(checkoutOptions, selection.CheckoutPR)

chosenCheckoutOption := selection.GetCheckoutOptionFromPrompt(config.ProjectOrder, checkoutOptions)
chosenCheckoutOption := selection.GetCheckoutOptionFromPrompt(config.ProjectOrder, checkoutOptions, parentIdentifier)
if chosenCheckoutOption == selection.CheckoutDefault {
// Get the default branch from context
repo, res, err := config.ApiClient.GitProviderAPI.GetGitContext(ctx).Repository(apiclient.GetRepositoryContext{
Expand All @@ -90,14 +93,14 @@ func SetBranchFromWizard(config BranchWizardConfig) (*apiclient.GitRepository, e
}

if chosenCheckoutOption == selection.CheckoutBranch {
branch = selection.GetBranchFromPrompt(branchList, config.ProjectOrder)
branch = selection.GetBranchFromPrompt(branchList, config.ProjectOrder, parentIdentifier)
if branch == nil {
return nil, errors.New("must select a branch")
}
config.ChosenRepo.Branch = branch.Name
config.ChosenRepo.Sha = branch.Sha
} else if chosenCheckoutOption == selection.CheckoutPR {
chosenPullRequest := selection.GetPullRequestFromPrompt(prList, config.ProjectOrder)
chosenPullRequest := selection.GetPullRequestFromPrompt(prList, config.ProjectOrder, parentIdentifier)
if chosenPullRequest == nil {
return nil, errors.New("must select a pull request")
}
Expand Down
14 changes: 12 additions & 2 deletions pkg/cmd/workspace/util/repository_wizard.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package util

import (
"context"
"fmt"

config_const "github.com/daytonaio/daytona/cmd/daytona/config"
apiclient_util "github.com/daytonaio/daytona/internal/util/apiclient"
Expand Down Expand Up @@ -96,13 +97,20 @@ func getRepositoryFromWizard(config RepositoryWizardConfig) (*apiclient.GitRepos
return nil, err
}

namespace := ""
if len(namespaceList) == 1 {
namespaceId = namespaceList[0].Id
namespace = namespaceList[0].Name
} else {
namespaceId = selection.GetNamespaceIdFromPrompt(namespaceList, config.ProjectOrder)
namespaceId = selection.GetNamespaceIdFromPrompt(namespaceList, config.ProjectOrder, providerId)
if namespaceId == "" {
return nil, common.ErrCtrlCAbort
}
for _, namespaceItem := range namespaceList {
if namespaceItem.Id == namespaceId {
namespace = namespaceItem.Name
}
}
}

var providerRepos []apiclient.GitRepository
Expand All @@ -115,7 +123,8 @@ func getRepositoryFromWizard(config RepositoryWizardConfig) (*apiclient.GitRepos
return nil, err
}

chosenRepo := selection.GetRepositoryFromPrompt(providerRepos, config.ProjectOrder, config.SelectedRepos)
parentIdentifier := fmt.Sprintf("%s/%s", providerId, namespace)
chosenRepo := selection.GetRepositoryFromPrompt(providerRepos, config.ProjectOrder, config.SelectedRepos, parentIdentifier)
if chosenRepo == nil {
return nil, common.ErrCtrlCAbort
}
Expand All @@ -128,6 +137,7 @@ func getRepositoryFromWizard(config RepositoryWizardConfig) (*apiclient.GitRepos
ApiClient: config.ApiClient,
ProviderId: providerId,
NamespaceId: namespaceId,
Namespace: namespace,
ChosenRepo: chosenRepo,
ProjectOrder: config.ProjectOrder,
})
Expand Down
13 changes: 11 additions & 2 deletions pkg/views/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package views

import (
"fmt"
"os"

"github.com/charmbracelet/bubbles/list"
Expand Down Expand Up @@ -45,7 +46,7 @@ var LogPrefixColors = []lipgloss.AdaptiveColor{
Blue, Orange, Cyan, Yellow,
}

func GetStyledSelectList(items []list.Item) list.Model {
func GetStyledSelectList(items []list.Item, parentIdentifier ...string) list.Model {

d := list.NewDefaultDelegate()

Expand All @@ -68,7 +69,15 @@ func GetStyledSelectList(items []list.Item) list.Model {
l.FilterInput.PromptStyle = lipgloss.NewStyle().Foreground(Green)
l.FilterInput.TextStyle = lipgloss.NewStyle().Foreground(Green)

l.SetStatusBarItemName("item\n\n"+lipgloss.NewStyle().Foreground(LightGray).Render("==="), "items\n\n"+lipgloss.NewStyle().Foreground(LightGray).Render("==="))
singularItemName := "item " + SeparatorString
var pluralItemName string
if len(parentIdentifier) == 0 {
pluralItemName = fmt.Sprintf("items\n\n%s", SeparatorString)
} else {
pluralItemName = fmt.Sprintf("items (%s)\n\n%s", parentIdentifier[0], SeparatorString)
}

l.SetStatusBarItemName(singularItemName, pluralItemName)

return l
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/views/workspace/selection/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
tea "github.com/charmbracelet/bubbletea"
)

func selectBranchPrompt(branches []apiclient.GitBranch, projectOrder int, choiceChan chan<- string) {
func selectBranchPrompt(branches []apiclient.GitBranch, projectOrder int, parentIdentifier string, choiceChan chan<- string) {
items := []list.Item{}

// Populate items with titles and descriptions from workspaces.
Expand All @@ -26,7 +26,7 @@ func selectBranchPrompt(branches []apiclient.GitBranch, projectOrder int, choice
items = append(items, newItem)
}

l := views.GetStyledSelectList(items)
l := views.GetStyledSelectList(items, parentIdentifier)

title := "Choose a Branch"
if projectOrder > 1 {
Expand All @@ -49,10 +49,10 @@ func selectBranchPrompt(branches []apiclient.GitBranch, projectOrder int, choice
}
}

func GetBranchFromPrompt(branches []apiclient.GitBranch, projectOrder int) *apiclient.GitBranch {
func GetBranchFromPrompt(branches []apiclient.GitBranch, projectOrder int, parentIdentifier string) *apiclient.GitBranch {
choiceChan := make(chan string)

go selectBranchPrompt(branches, projectOrder, choiceChan)
go selectBranchPrompt(branches, projectOrder, parentIdentifier, choiceChan)

branchName := <-choiceChan

Expand Down
8 changes: 4 additions & 4 deletions pkg/views/workspace/selection/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ var (
CheckoutPR = CheckoutOption{Title: "Pull/Merge requests", Id: "pullrequest"}
)

func selectCheckoutPrompt(checkoutOptions []CheckoutOption, projectOrder int, choiceChan chan<- string) {
func selectCheckoutPrompt(checkoutOptions []CheckoutOption, projectOrder int, parentIdentifier string, 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)
l := views.GetStyledSelectList(items, parentIdentifier)

title := "Cloning Options"
if projectOrder > 1 {
Expand All @@ -55,10 +55,10 @@ func selectCheckoutPrompt(checkoutOptions []CheckoutOption, projectOrder int, ch
}
}

func GetCheckoutOptionFromPrompt(projectOrder int, checkoutOptions []CheckoutOption) CheckoutOption {
func GetCheckoutOptionFromPrompt(projectOrder int, checkoutOptions []CheckoutOption, parentIdentifier string) CheckoutOption {
choiceChan := make(chan string)

go selectCheckoutPrompt(checkoutOptions, projectOrder, choiceChan)
go selectCheckoutPrompt(checkoutOptions, projectOrder, parentIdentifier, choiceChan)

checkoutOptionId := <-choiceChan

Expand Down
8 changes: 4 additions & 4 deletions pkg/views/workspace/selection/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/daytonaio/daytona/pkg/views"
)

func selectNamespacePrompt(namespaces []apiclient.GitNamespace, projectOrder int, choiceChan chan<- string) {
func selectNamespacePrompt(namespaces []apiclient.GitNamespace, projectOrder int, parentIdentifier string, choiceChan chan<- string) {
items := []list.Item{}
var desc string

Expand All @@ -28,7 +28,7 @@ func selectNamespacePrompt(namespaces []apiclient.GitNamespace, projectOrder int
items = append(items, newItem)
}

l := views.GetStyledSelectList(items)
l := views.GetStyledSelectList(items, parentIdentifier)

title := "Choose a Namespace"
if projectOrder > 1 {
Expand All @@ -51,10 +51,10 @@ func selectNamespacePrompt(namespaces []apiclient.GitNamespace, projectOrder int
}
}

func GetNamespaceIdFromPrompt(namespaces []apiclient.GitNamespace, projectOrder int) string {
func GetNamespaceIdFromPrompt(namespaces []apiclient.GitNamespace, projectOrder int, parentIdentifier string) string {
choiceChan := make(chan string)

go selectNamespacePrompt(namespaces, projectOrder, choiceChan)
go selectNamespacePrompt(namespaces, projectOrder, parentIdentifier, choiceChan)

return <-choiceChan
}
8 changes: 4 additions & 4 deletions pkg/views/workspace/selection/pullrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
tea "github.com/charmbracelet/bubbletea"
)

func selectPullRequestPrompt(pullRequests []apiclient.GitPullRequest, projectOrder int, choiceChan chan<- string) {
func selectPullRequestPrompt(pullRequests []apiclient.GitPullRequest, projectOrder int, parentIdentifier string, choiceChan chan<- string) {
items := []list.Item{}

// Populate items with titles and descriptions from workspaces.
Expand All @@ -28,7 +28,7 @@ func selectPullRequestPrompt(pullRequests []apiclient.GitPullRequest, projectOrd
items = append(items, newItem)
}

l := views.GetStyledSelectList(items)
l := views.GetStyledSelectList(items, parentIdentifier)

title := "Choose a Pull/Merge Request"
if projectOrder > 1 {
Expand All @@ -51,10 +51,10 @@ func selectPullRequestPrompt(pullRequests []apiclient.GitPullRequest, projectOrd
}
}

func GetPullRequestFromPrompt(pullRequests []apiclient.GitPullRequest, projectOrder int) *apiclient.GitPullRequest {
func GetPullRequestFromPrompt(pullRequests []apiclient.GitPullRequest, projectOrder int, parentIdentifier string) *apiclient.GitPullRequest {
choiceChan := make(chan string)

go selectPullRequestPrompt(pullRequests, projectOrder, choiceChan)
go selectPullRequestPrompt(pullRequests, projectOrder, parentIdentifier, choiceChan)

pullRequestName := <-choiceChan

Expand Down
8 changes: 4 additions & 4 deletions pkg/views/workspace/selection/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
tea "github.com/charmbracelet/bubbletea"
)

func selectRepositoryPrompt(repositories []apiclient.GitRepository, projectOrder int, choiceChan chan<- string, selectedRepos map[string]int) {
func selectRepositoryPrompt(repositories []apiclient.GitRepository, projectOrder int, choiceChan chan<- string, selectedRepos map[string]int, parentIdentifier string) {
items := []list.Item{}

// Populate items with titles and descriptions from workspaces.
Expand All @@ -28,7 +28,7 @@ func selectRepositoryPrompt(repositories []apiclient.GitRepository, projectOrder
items = append(items, newItem)
}

l := views.GetStyledSelectList(items)
l := views.GetStyledSelectList(items, parentIdentifier)

title := "Choose a Repository"
if projectOrder > 1 {
Expand All @@ -54,10 +54,10 @@ func selectRepositoryPrompt(repositories []apiclient.GitRepository, projectOrder
}
}

func GetRepositoryFromPrompt(repositories []apiclient.GitRepository, projectOrder int, selectedRepos map[string]int) *apiclient.GitRepository {
func GetRepositoryFromPrompt(repositories []apiclient.GitRepository, projectOrder int, selectedRepos map[string]int, parentIdentifier string) *apiclient.GitRepository {
choiceChan := make(chan string)

go selectRepositoryPrompt(repositories, projectOrder, choiceChan, selectedRepos)
go selectRepositoryPrompt(repositories, projectOrder, choiceChan, selectedRepos, parentIdentifier)

choice := <-choiceChan

Expand Down

0 comments on commit 03e9d34

Please sign in to comment.