Skip to content

Commit

Permalink
feat: display active profile in views (#301)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Dagelic <dagelic.ivan@gmail.com>
  • Loading branch information
idagelic authored Mar 29, 2024
1 parent 17f53d1 commit 9046a53
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 15 deletions.
13 changes: 12 additions & 1 deletion pkg/cmd/target/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package target
import (
"context"

"github.com/daytonaio/daytona/cmd/daytona/config"
"github.com/daytonaio/daytona/internal/util/apiclient"
"github.com/daytonaio/daytona/internal/util/apiclient/server"
"github.com/daytonaio/daytona/pkg/views/target"
Expand All @@ -21,12 +22,22 @@ var targetRemoveCmd = &cobra.Command{
Args: cobra.NoArgs,
Aliases: []string{"rm", "delete"},
Run: func(cmd *cobra.Command, args []string) {
c, err := config.GetConfig()
if err != nil {
log.Fatal(err)
}

activeProfile, err := c.GetActiveProfile()
if err != nil {
log.Fatal(err)
}

targets, err := server.GetTargetList()
if err != nil {
log.Fatal(err)
}

selectedTarget, err := target.GetTargetFromPrompt(targets, false)
selectedTarget, err := target.GetTargetFromPrompt(targets, activeProfile.Name, false)
if err != nil {
log.Fatal(err)
}
Expand Down
13 changes: 12 additions & 1 deletion pkg/cmd/target/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package target
import (
"context"

"github.com/daytonaio/daytona/cmd/daytona/config"
internal_util "github.com/daytonaio/daytona/internal/util"
"github.com/daytonaio/daytona/internal/util/apiclient"
"github.com/daytonaio/daytona/internal/util/apiclient/server"
Expand All @@ -24,6 +25,16 @@ var targetSetCmd = &cobra.Command{
Args: cobra.NoArgs,
Aliases: []string{"s"},
Run: func(cmd *cobra.Command, args []string) {
c, err := config.GetConfig()
if err != nil {
log.Fatal(err)
}

activeProfile, err := c.GetActiveProfile()
if err != nil {
log.Fatal(err)
}

pluginList, err := server.GetProviderList()
if err != nil {
log.Fatal(err)
Expand All @@ -47,7 +58,7 @@ var targetSetCmd = &cobra.Command{
}
}

selectedTarget, err := target.GetTargetFromPrompt(filteredTargets, true)
selectedTarget, err := target.GetTargetFromPrompt(filteredTargets, activeProfile.Name, true)
if err != nil {
log.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/workspace/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ var CreateCmd = &cobra.Command{
visited[repo.Url] = true
}

target, err := getTarget()
target, err := getTarget(activeProfile.Name)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -184,7 +184,7 @@ func init() {
CreateCmd.Flags().BoolP("code", "c", false, "Open the workspace in the IDE after workspace creation")
}

func getTarget() (*serverapiclient.ProviderTarget, error) {
func getTarget(activeProfileName string) (*serverapiclient.ProviderTarget, error) {
targets, err := server.GetTargetList()
if err != nil {
return nil, err
Expand All @@ -203,7 +203,7 @@ func getTarget() (*serverapiclient.ProviderTarget, error) {
return &targets[0], nil
}

return target.GetTargetFromPrompt(targets, false)
return target.GetTargetFromPrompt(targets, activeProfileName, false)
}

func processPrompting(cmd *cobra.Command, apiClient *serverapiclient.APIClient, workspaceName *string, repos *[]types.Repository, ctx context.Context) {
Expand Down
4 changes: 3 additions & 1 deletion pkg/views/target/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import (
"github.com/daytonaio/daytona/internal/util"
"github.com/daytonaio/daytona/pkg/serverapiclient"
"github.com/daytonaio/daytona/pkg/views"
view_util "github.com/daytonaio/daytona/pkg/views/util"
)

const NewTargetName = "+ New Target"

func GetTargetFromPrompt(targets []serverapiclient.ProviderTarget, withNewTarget bool) (*serverapiclient.ProviderTarget, error) {
func GetTargetFromPrompt(targets []serverapiclient.ProviderTarget, activeProfileName string, withNewTarget bool) (*serverapiclient.ProviderTarget, error) {
items := util.ArrayMap(targets, func(t serverapiclient.ProviderTarget) list.Item {
return item{
target: t,
Expand All @@ -38,6 +39,7 @@ func GetTargetFromPrompt(targets []serverapiclient.ProviderTarget, withNewTarget
l := views.GetStyledSelectList(items)
m := model{list: l}
m.list.Title = "CHOOSE A TARGET"
m.footer = view_util.GetListFooter(activeProfileName)

p, err := tea.NewProgram(m, tea.WithAltScreen()).Run()
if err != nil {
Expand Down
9 changes: 4 additions & 5 deletions pkg/views/target/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ package target
import (
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/daytonaio/daytona/pkg/serverapiclient"
view_util "github.com/daytonaio/daytona/pkg/views/util"
)

var docStyle = lipgloss.NewStyle().Margin(1, 2)

type item struct {
target serverapiclient.ProviderTarget
}
Expand All @@ -28,6 +26,7 @@ func (i item) FilterValue() string { return *i.target.Name }
type model struct {
list list.Model
choice *serverapiclient.ProviderTarget
footer string
}

func (m model) Init() tea.Cmd {
Expand All @@ -49,7 +48,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Quit
}
case tea.WindowSizeMsg:
h, v := docStyle.GetFrameSize()
h, v := view_util.DocStyle.GetFrameSize()
m.list.SetSize(msg.Width-h, msg.Height-v)
}

Expand All @@ -59,5 +58,5 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (m model) View() string {
return docStyle.Render(m.list.View())
return view_util.DocStyle.Render(m.list.View() + m.footer)
}
6 changes: 6 additions & 0 deletions pkg/views/util/globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/daytonaio/daytona/pkg/views"
)

var DocStyle = lipgloss.NewStyle().Margin(3, 2, 1, 2)

func RenderMainTitle(title string) {
fmt.Println(lipgloss.NewStyle().Foreground(views.Green).Bold(true).Padding(1, 0, 1, 0).Render(title))
}
Expand All @@ -30,6 +32,10 @@ func RenderInfoMessageBold(message string) {
fmt.Println(lipgloss.NewStyle().Bold(true).Padding(1, 0, 1, 1).Render(message))
}

func GetListFooter(profileName string) string {
return lipgloss.NewStyle().Bold(true).PaddingLeft(2).Render("\n\nActive profile: " + profileName)
}

func RenderBorderedMessage(message string) {
fmt.Println(lipgloss.
NewStyle().
Expand Down
23 changes: 19 additions & 4 deletions pkg/views/workspace/selection/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ package selection
import (
"fmt"
"io"
"log"
"strings"

"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/daytonaio/daytona/cmd/daytona/config"
"github.com/daytonaio/daytona/pkg/views"
view_util "github.com/daytonaio/daytona/pkg/views/util"
)

var CustomRepoIdentifier = "<CUSTOM_REPO>"

var docStyle = lipgloss.NewStyle().Margin(1, 2)

type item[T any] struct {
id, title, desc, createdTime, statusTime, target string
choiceProperty T
Expand All @@ -34,6 +35,7 @@ func (i item[T]) Target() string { return i.target }
type model[T any] struct {
list list.Model
choice *T
footer string
}

func (m model[T]) Init() tea.Cmd {
Expand All @@ -55,7 +57,7 @@ func (m model[T]) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Quit
}
case tea.WindowSizeMsg:
h, v := docStyle.GetFrameSize()
h, v := view_util.DocStyle.GetFrameSize()
m.list.SetSize(msg.Width-h, msg.Height-v)
}

Expand All @@ -65,7 +67,20 @@ func (m model[T]) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (m model[T]) View() string {
return docStyle.Render(m.list.View())
if m.footer == "" {
c, err := config.GetConfig()
if err != nil {
log.Fatal(err)
}

activeProfile, err := c.GetActiveProfile()
if err != nil {
log.Fatal(err)
}

m.footer = view_util.GetListFooter(activeProfile.Name)
}
return view_util.DocStyle.Render(m.list.View() + m.footer)
}

type ItemDelegate[T any] struct {
Expand Down

0 comments on commit 9046a53

Please sign in to comment.