Skip to content

Commit

Permalink
feat: default values in advanced config views
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Dagelic <[email protected]>
  • Loading branch information
idagelic committed Apr 25, 2024
1 parent 37b07ec commit 8910c03
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 39 deletions.
8 changes: 7 additions & 1 deletion pkg/cmd/workspace/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,13 @@ func processPrompting(cmd *cobra.Command, apiClient *serverapiclient.APIClient,
for _, workspaceInfo := range workspaceList {
workspaceNames = append(workspaceNames, *workspaceInfo.Name)
}
*workspaceName, *projects, err = workspace_util.GetCreationDataFromPrompt(workspaceNames, gitProviders, manualFlag, multiProjectFlag)

apiServerConfig, res, err := apiClient.ServerAPI.GetConfig(context.Background()).Execute()
if err != nil {
log.Fatal(apiclient.HandleErrorResponse(res, err))
}

*workspaceName, *projects, err = workspace_util.GetCreationDataFromPrompt(apiServerConfig, workspaceNames, gitProviders, manualFlag, multiProjectFlag)
if err != nil {
log.Fatal(err)
return
Expand Down
9 changes: 6 additions & 3 deletions pkg/cmd/workspace/util/creation_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/daytonaio/daytona/pkg/views/workspace/create"
)

func GetCreationDataFromPrompt(workspaceNames []string, userGitProviders []serverapiclient.GitProvider, manual bool, multiProject bool) (string, []serverapiclient.CreateWorkspaceRequestProject, error) {
func GetCreationDataFromPrompt(apiServerConfig *serverapiclient.ServerConfig, workspaceNames []string, userGitProviders []serverapiclient.GitProvider, manual bool, multiProject bool) (string, []serverapiclient.CreateWorkspaceRequestProject, error) {
var projectList []serverapiclient.CreateWorkspaceRequestProject
var providerRepo serverapiclient.GitRepository
var providerRepoUrl string
Expand Down Expand Up @@ -82,7 +82,10 @@ func GetCreationDataFromPrompt(workspaceNames []string, userGitProviders []serve

suggestedName := create.GetSuggestedWorkspaceName(*workspaceCreationPromptResponse.PrimaryProject.Source.Repository.Url)

workspaceName, primaryContainerImage, primaryContainerUser = create.GetWorkspaceDataFromPrompt(suggestedName, workspaceNames, !multiProject)
workspaceName, primaryContainerImage, primaryContainerUser, err = create.GetWorkspaceDataFromPrompt(apiServerConfig, suggestedName, workspaceNames, !multiProject)
if err != nil {
return "", nil, err
}

if workspaceName == "" {
return "", nil, errors.New("workspace name is required")
Expand All @@ -97,7 +100,7 @@ func GetCreationDataFromPrompt(workspaceNames []string, userGitProviders []serve
}

if multiProject {
create.DisplayMultiSubmitForm(workspaceName, &projectList, &doneCheck)
create.DisplayMultiSubmitForm(workspaceName, &projectList, apiServerConfig, &doneCheck)
if !doneCheck {
return "", nil, errors.New("operation cancelled")
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/db/dto/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type ProjectStateDTO struct {

type ProjectDTO struct {
Name string `json:"name"`
Image string `json:"image"`
User string `json:"user"`
Repository RepositoryDTO `json:"repository"`
WorkspaceId string `json:"workspaceId"`
Target string `json:"target"`
Expand All @@ -34,6 +36,8 @@ type ProjectDTO struct {
func ToProjectDTO(project *workspace.Project, workspace *workspace.Workspace) ProjectDTO {
return ProjectDTO{
Name: project.Name,
Image: project.Image,
User: project.User,
Repository: ToRepositoryDTO(project.Repository),
WorkspaceId: project.WorkspaceId,
Target: project.Target,
Expand Down Expand Up @@ -76,6 +80,8 @@ func ToProjectStateDTO(state *workspace.ProjectState) *ProjectStateDTO {
func ToProject(projectDTO ProjectDTO) *workspace.Project {
return &workspace.Project{
Name: projectDTO.Name,
Image: projectDTO.Image,
User: projectDTO.User,
Repository: ToRepository(projectDTO.Repository),
WorkspaceId: projectDTO.WorkspaceId,
Target: projectDTO.Target,
Expand Down
28 changes: 17 additions & 11 deletions pkg/views/workspace/create/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,26 @@ import (

var configurationHelpLine = lipgloss.NewStyle().Foreground(views.Gray).Render("enter: next f10: advanced configuration")

func ConfigureProjects(projectList []serverapiclient.CreateWorkspaceRequestProject) ([]serverapiclient.CreateWorkspaceRequestProject, error) {
var containerImage string
var osUser string
func ConfigureProjects(projectList []serverapiclient.CreateWorkspaceRequestProject, defaultContainerImage string, defaultContainerUser string) ([]serverapiclient.CreateWorkspaceRequestProject, error) {
containerImage := defaultContainerImage
containerUser := defaultContainerUser
var doneCheck bool

projectName := selection.GetProjectRequestFromPrompt(projectList)
if projectName == "" {
return projectList, errors.New("project ID is required")
project := selection.GetProjectRequestFromPrompt(projectList)
if project == nil {
return projectList, errors.New("project is required")
}
if project.Image != nil {
containerImage = *project.Image
}
if project.User != nil {
containerUser = *project.User
}

GetProjectConfigurationGroup(&containerImage, &osUser)
GetProjectConfigurationGroup(&containerImage, &containerUser)

form := huh.NewForm(
GetProjectConfigurationGroup(&containerImage, &osUser),
GetProjectConfigurationGroup(&containerImage, &containerUser),
GetDoneCheckGroup(&doneCheck),
).WithTheme(views.GetCustomTheme())

Expand All @@ -39,14 +45,14 @@ func ConfigureProjects(projectList []serverapiclient.CreateWorkspaceRequestProje
}

for i := range projectList {
if projectList[i].Name == projectName {
if projectList[i].Name == project.Name {
projectList[i].Image = &containerImage
projectList[i].User = &osUser
projectList[i].User = &containerUser
}
}

if !doneCheck {
projectList, err = ConfigureProjects(projectList)
projectList, err = ConfigureProjects(projectList, defaultContainerImage, defaultContainerUser)
if err != nil {
return projectList, err
}
Expand Down
13 changes: 11 additions & 2 deletions pkg/views/workspace/create/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type SummaryModel struct {

var configureCheck bool

func DisplayMultiSubmitForm(workspaceName string, projectList *[]serverapiclient.CreateWorkspaceRequestProject, doneCheck *bool) {
func DisplayMultiSubmitForm(workspaceName string, projectList *[]serverapiclient.CreateWorkspaceRequestProject, apiServerConfig *serverapiclient.ServerConfig, doneCheck *bool) {
m := NewSummaryModel(workspaceName, *projectList, doneCheck)

if _, err := tea.NewProgram(m).Run(); err != nil {
Expand All @@ -40,7 +40,16 @@ func DisplayMultiSubmitForm(workspaceName string, projectList *[]serverapiclient
return
}

configuredProjects, err := ConfigureProjects(*projectList)
if apiServerConfig.DefaultProjectImage == nil || apiServerConfig.DefaultProjectUser == nil {
log.Fatal("Default project image and user are not set")
}

for i := range *projectList {
(*projectList)[i].Image = apiServerConfig.DefaultProjectImage
(*projectList)[i].User = apiServerConfig.DefaultProjectUser
}

configuredProjects, err := ConfigureProjects(*projectList, *apiServerConfig.DefaultProjectImage, *apiServerConfig.DefaultProjectUser)
if err != nil {
log.Fatal(err)
}
Expand Down
13 changes: 9 additions & 4 deletions pkg/views/workspace/create/workspacedata.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss"
"github.com/daytonaio/daytona/internal/util"
"github.com/daytonaio/daytona/pkg/serverapiclient"
"github.com/daytonaio/daytona/pkg/views"
)

Expand All @@ -27,8 +28,12 @@ type WorkspaceDataModel struct {
showConfigurationOption bool
}

func GetWorkspaceDataFromPrompt(suggestedName string, workspaceNames []string, showConfigurationOption bool) (string, string, string) {
workspaceName, containerImage, osUser := suggestedName, "", ""
func GetWorkspaceDataFromPrompt(apiServerConfig *serverapiclient.ServerConfig, suggestedName string, workspaceNames []string, showConfigurationOption bool) (string, string, string, error) {
if apiServerConfig.DefaultProjectImage == nil || apiServerConfig.DefaultProjectUser == nil {
return "", "", "", errors.New("default project image and user are not set")
}

workspaceName, containerImage, osUser := suggestedName, *apiServerConfig.DefaultProjectImage, *apiServerConfig.DefaultProjectUser

m := NewWorkspaceDataModel(workspaceNames, &workspaceName, &containerImage, &osUser, showConfigurationOption)

Expand All @@ -38,10 +43,10 @@ func GetWorkspaceDataFromPrompt(suggestedName string, workspaceNames []string, s
}

if !doneCheck {
return "", "", ""
return "", "", "", errors.New("workspace creation cancelled")
}

return workspaceName, containerImage, osUser
return workspaceName, containerImage, osUser, nil
}

func NewWorkspaceDataModel(workspaceNames []string, workspaceName *string, containerImage *string, osUser *string, showConfigurationOption bool) WorkspaceDataModel {
Expand Down
30 changes: 12 additions & 18 deletions pkg/views/workspace/selection/projectrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
tea "github.com/charmbracelet/bubbletea"
)

func selectProjectRequestPrompt(projects []serverapiclient.CreateWorkspaceRequestProject, choiceChan chan<- string) {
func selectProjectRequestPrompt(projects []serverapiclient.CreateWorkspaceRequestProject, choiceChan chan<- *serverapiclient.CreateWorkspaceRequestProject) {
items := []list.Item{}

for _, project := range projects {
Expand All @@ -30,23 +30,17 @@ func selectProjectRequestPrompt(projects []serverapiclient.CreateWorkspaceReques
if project.User != nil {
user = *project.User
}
newItem := item[string]{id: name, title: name, choiceProperty: name}
if image != "" {
if user != "" {
newItem.desc = fmt.Sprintf("%s (%s)", image, user)
} else {
newItem.desc = fmt.Sprintf("Image: %s", image)
}
} else if user != "" {
newItem.desc = fmt.Sprintf("User: %s", user)
} else {
newItem.desc = "Default configuration"
if user == "" {
user = "user not defined"
}
newItem := item[serverapiclient.CreateWorkspaceRequestProject]{id: name, title: name, choiceProperty: project}
newItem.desc = fmt.Sprintf("%s (%s)", image, user)

items = append(items, newItem)
}

l := views.GetStyledSelectList(items)
m := model[string]{list: l}
m := model[serverapiclient.CreateWorkspaceRequestProject]{list: l}
m.list.Title = "CHOOSE A PROJECT TO CONFIGURE"

p, err := tea.NewProgram(m, tea.WithAltScreen()).Run()
Expand All @@ -55,15 +49,15 @@ func selectProjectRequestPrompt(projects []serverapiclient.CreateWorkspaceReques
os.Exit(1)
}

if m, ok := p.(model[string]); ok && m.choice != nil {
choiceChan <- *m.choice
if m, ok := p.(model[serverapiclient.CreateWorkspaceRequestProject]); ok && m.choice != nil {
choiceChan <- m.choice
} else {
choiceChan <- ""
choiceChan <- nil
}
}

func GetProjectRequestFromPrompt(projects []serverapiclient.CreateWorkspaceRequestProject) string {
choiceChan := make(chan string)
func GetProjectRequestFromPrompt(projects []serverapiclient.CreateWorkspaceRequestProject) *serverapiclient.CreateWorkspaceRequestProject {
choiceChan := make(chan *serverapiclient.CreateWorkspaceRequestProject)

go selectProjectRequestPrompt(projects, choiceChan)

Expand Down

0 comments on commit 8910c03

Please sign in to comment.