Skip to content

Commit

Permalink
feat: tui redesign
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Dagelic <[email protected]>
  • Loading branch information
idagelic committed Apr 26, 2024
1 parent 2a6e25f commit 1fac630
Show file tree
Hide file tree
Showing 37 changed files with 1,141 additions and 226 deletions.
20 changes: 16 additions & 4 deletions pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package cmd

import (
"fmt"
"os"

"github.com/daytonaio/daytona/internal/util"
Expand All @@ -14,8 +13,9 @@ import (
. "github.com/daytonaio/daytona/pkg/cmd/ports"
. "github.com/daytonaio/daytona/pkg/cmd/profile"
. "github.com/daytonaio/daytona/pkg/cmd/server"
apikeyCmd "github.com/daytonaio/daytona/pkg/cmd/server/apikey"
. "github.com/daytonaio/daytona/pkg/cmd/workspace"
view_util "github.com/daytonaio/daytona/pkg/views/util"
view "github.com/daytonaio/daytona/pkg/views/initial"
log "github.com/sirupsen/logrus"

"github.com/spf13/cobra"
Expand All @@ -26,11 +26,23 @@ var rootCmd = &cobra.Command{
Short: "Daytona is a Dev Environment Manager",
Long: "Daytona is a Dev Environment Manager",
Run: func(cmd *cobra.Command, args []string) {
fmt.Print(view_util.GetLongDescription())
err := cmd.Help()
command, err := view.GetCommand()
if err != nil {
log.Fatal(err)
}

switch command {
case "list":
ListCmd.Run(cmd, args)
case "profile add":
ProfileAddCmd.Run(cmd, []string{})
case "server api-key new":
apikeyCmd.GenerateCmd.Run(cmd, []string{})
case "create":
CreateCmd.Run(cmd, []string{})
case "help":
cmd.Help()
}
},
}

Expand Down
4 changes: 1 addition & 3 deletions pkg/cmd/ide.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package cmd

import (
"fmt"

"github.com/daytonaio/daytona/cmd/daytona/config"
"github.com/daytonaio/daytona/pkg/views/ide"
"github.com/daytonaio/daytona/pkg/views/util"
Expand Down Expand Up @@ -45,6 +43,6 @@ var ideCmd = &cobra.Command{
log.Fatal(err)
}

util.RenderInfoMessage(fmt.Sprintf("Default IDE set to: %s", chosenIde.Name))
util.RenderDefaultIdeUpdated(chosenIde.Name)
},
}
8 changes: 4 additions & 4 deletions pkg/cmd/profile/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/spf13/cobra"
)

var profileAddCmd = &cobra.Command{
var ProfileAddCmd = &cobra.Command{
Use: "add",
Short: "Add profile",
Args: cobra.NoArgs,
Expand Down Expand Up @@ -97,7 +97,7 @@ var apiUrlFlag string
var apiKeyFlag string

func init() {
profileAddCmd.Flags().StringVarP(&profileNameFlag, "name", "n", "", "Profile name")
profileAddCmd.Flags().StringVarP(&apiUrlFlag, "api-url", "a", "", "API URL")
profileAddCmd.Flags().StringVarP(&apiKeyFlag, "api-key", "k", "", "API Key")
ProfileAddCmd.Flags().StringVarP(&profileNameFlag, "name", "n", "", "Profile name")
ProfileAddCmd.Flags().StringVarP(&apiUrlFlag, "api-url", "a", "", "API URL")
ProfileAddCmd.Flags().StringVarP(&apiKeyFlag, "api-key", "k", "", "API Key")
}
2 changes: 1 addition & 1 deletion pkg/cmd/profile/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var ProfileCmd = &cobra.Command{
func init() {
ProfileCmd.AddCommand(profileListCmd)
ProfileCmd.AddCommand(ProfileUseCmd)
ProfileCmd.AddCommand(profileAddCmd)
ProfileCmd.AddCommand(ProfileAddCmd)
ProfileCmd.AddCommand(profileEditCmd)
ProfileCmd.AddCommand(profileDeleteCmd)
}
2 changes: 1 addition & 1 deletion pkg/cmd/server/apikey/api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var ApiKeyCmd = &cobra.Command{
}

func init() {
ApiKeyCmd.AddCommand(generateCmd)
ApiKeyCmd.AddCommand(GenerateCmd)
ApiKeyCmd.AddCommand(revokeCmd)
ApiKeyCmd.AddCommand(listCmd)
}
4 changes: 2 additions & 2 deletions pkg/cmd/server/apikey/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

var saveFlag bool

var generateCmd = &cobra.Command{
var GenerateCmd = &cobra.Command{
Use: "generate [NAME]",
Short: "Generate a new API key",
Aliases: []string{"g", "new"},
Expand Down Expand Up @@ -85,5 +85,5 @@ func saveKeyToDefaultProfile(key string) error {
}

func init() {
generateCmd.Flags().BoolVarP(&saveFlag, "save", "s", false, "Save the API key to your default profile on this machine")
GenerateCmd.Flags().BoolVarP(&saveFlag, "save", "s", false, "Save the API key to your default profile on this machine")
}
12 changes: 11 additions & 1 deletion pkg/cmd/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ package server
import (
"fmt"

"github.com/charmbracelet/lipgloss"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/daytonaio/daytona/internal/util"
"github.com/daytonaio/daytona/pkg/cmd/output"
"github.com/daytonaio/daytona/pkg/server"
"github.com/daytonaio/daytona/pkg/views"
view_util "github.com/daytonaio/daytona/pkg/views/util"
)

var configCmd = &cobra.Command{
Expand All @@ -26,6 +29,13 @@ var configCmd = &cobra.Command{
apiUrl := util.GetFrpcApiUrl(config.Frps.Protocol, config.Id, config.Frps.Domain)
output.Output = apiUrl

fmt.Println(apiUrl)
output := ""
output += "If you want to connect to the server remotely:\n\n"

output += "1. Create an API key on this machine: "
output += lipgloss.NewStyle().Foreground(views.Green).Render("daytona server api-key new") + "\n"
output += "2. Add a profile on the client machine: \n\t"
output += lipgloss.NewStyle().Foreground(views.Green).Render(fmt.Sprintf("daytona profile add -a %s -k API_KEY", apiUrl))
view_util.RenderInfoMessage(output)
},
}
2 changes: 1 addition & 1 deletion pkg/cmd/server/provider/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var providerInstallCmd = &cobra.Command{

providerList := convertToDTO(providersManifest)

providerToInstall := provider.GetProviderFromPrompt(providerList, "CHOOSE A PROVIDER TO INSTALL")
providerToInstall := provider.GetProviderFromPrompt(providerList, "Choose a provider to install")

if providerToInstall == nil {
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/server/provider/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var providerUninstallCmd = &cobra.Command{
log.Fatal(err)
}

providerToUninstall := provider.GetProviderFromPrompt(providerList, "CHOOSE A PROVIDER TO UNINSTALL")
providerToUninstall := provider.GetProviderFromPrompt(providerList, "Choose a provider to uninstall")

if providerToUninstall == nil {
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/server/provider/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var providerUpdateCmd = &cobra.Command{
return
}

providerToUpdate := provider.GetProviderFromPrompt(providerList, "CHOOSE A PROVIDER TO UPDATE")
providerToUpdate := provider.GetProviderFromPrompt(providerList, "Choose a provider to update")
if providerToUpdate == nil {
return
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"github.com/daytonaio/daytona/pkg/server/headscale"
"github.com/daytonaio/daytona/pkg/server/providertargets"
"github.com/daytonaio/daytona/pkg/server/workspaces"
views_util "github.com/daytonaio/daytona/pkg/views/util"
started_view "github.com/daytonaio/daytona/pkg/views/server/started"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -59,7 +59,7 @@ var ServerCmd = &cobra.Command{
if err != nil {
log.Fatal(err)
}
printServerStartedMessage(c)
printServerStartedMessage(c, runAsDaemon)
return
}

Expand Down Expand Up @@ -202,7 +202,7 @@ var ServerCmd = &cobra.Command{
continue
}

printServerStartedMessage(c)
printServerStartedMessage(c, runAsDaemon)
break
}

Expand All @@ -227,8 +227,9 @@ func getDaytonaScriptUrl(config *server.Config) string {
return url
}

func printServerStartedMessage(c *server.Config) {
views_util.RenderBorderedMessage(fmt.Sprintf("Daytona Server running on port: %d.\nYou can now begin developing locally.\n\nIf you want to connect to the server remotely:\n\n1. Create an API key on this machine:\ndaytona server api-key new\n\n2. On the client machine run:\ndaytona profile add -a %s -k API_KEY", c.ApiPort, util.GetFrpcApiUrl(c.Frps.Protocol, c.Id, c.Frps.Domain)))
func printServerStartedMessage(c *server.Config, runAsDaemon bool) {
started_view.Render(c.ApiPort, util.GetFrpcApiUrl(c.Frps.Protocol, c.Id, c.Frps.Domain), runAsDaemon)
// views_util.RenderBorderedMessage(fmt.Sprintf("Daytona Server running on port: %d.\nYou can now begin developing locally.\n\nIf you want to connect to the server remotely:\n\n1. Create an API key on this machine:\ndaytona server api-key new\n\n2. On the client machine run:\ndaytona profile add -a %s -k API_KEY", c.ApiPort, util.GetFrpcApiUrl(c.Frps.Protocol, c.Id, c.Frps.Domain)))
}

func getDbPath() (string, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/server/target/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var targetSetCmd = &cobra.Command{
log.Fatal(err)
}

selectedProvider := provider.GetProviderFromPrompt(pluginList, "CHOOSE A PROVIDER")
selectedProvider := provider.GetProviderFromPrompt(pluginList, "Choose a provier")

if selectedProvider == nil {
return
Expand Down
66 changes: 52 additions & 14 deletions pkg/cmd/workspace/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"fmt"
"net/url"
"os"
"strings"
"time"

tea "github.com/charmbracelet/bubbletea"
Expand Down Expand Up @@ -131,7 +130,7 @@ var CreateCmd = &cobra.Command{

dialStartTime := time.Now()
dialTimeout := 3 * time.Minute
statusProgram.Send(status.ResultMsg{Line: "Establishing connection with the workspace"})
// statusProgram.Send(status.Message{Line: "Establishing connection with the workspace"})

waitForDial(tsConn, *createdWorkspace.Id, *createdWorkspace.Projects[0].Name, dialStartTime, dialTimeout, statusProgram)

Expand All @@ -145,22 +144,32 @@ var CreateCmd = &cobra.Command{
return
}

chosenIdeId := c.DefaultIdeId
if ideFlag != "" {
chosenIdeId = ideFlag
}

ideList := config.GetIdeList()
var chosenIde config.Ide

for _, ide := range ideList {
if ide.Id == chosenIdeId {
chosenIde = ide
}
}

fmt.Println()
info.Render(wsInfo)
info.Render(wsInfo, chosenIde.Name)

codeFlag, _ := cmd.Flags().GetBool("code")
if !codeFlag {
view_util.RenderCreationInfoMessage("Run 'daytona code' when you're ready to start developing")
return
}

ide := c.DefaultIdeId
if ideFlag != "" {
ide = ideFlag
}
view_util.RenderCreationInfoMessage("Opening the workspace in your preferred editor ...")

view_util.RenderInfoMessage(fmt.Sprintf("Opening the workspace project '%s' in your preferred IDE.", *wsInfo.Projects[0].Name))

err = openIDE(ide, activeProfile, *createdWorkspace.Id, *wsInfo.Projects[0].Name)
err = openIDE(chosenIdeId, activeProfile, *createdWorkspace.Id, *wsInfo.Projects[0].Name)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -305,15 +314,44 @@ func scanWorkspaceLogs(activeProfile config.Profile, workspaceId string, statusP
return
}

messages := strings.Split(string(msg), "\r")
for _, msg := range messages {
statusProgram.Send(status.ResultMsg{Line: msg})
delimiter := byte('\r')
messages := splitWithDelimiter(string(msg), delimiter)

for _, message := range messages {
line := fmt.Sprintf("%s %s", view_util.CheckmarkSymbol, message)
fmt.Print(line)
}
if *started {
statusProgram.Send(status.ResultMsg{Line: "END_SIGNAL"})
fmt.Print("\nWorkspace creation complete.")
statusProgram.Send(status.Message{Line: "END_SIGNAL"})
break
}
if len(messages) != 0 {
fmt.Print("\n")
}
}
}

func splitWithDelimiter(s string, delimiter byte) []string {
var parts []string
var buffer []byte

for i := 0; i < len(s); i++ {
if s[i] == delimiter {
parts = append(parts, string(buffer))
buffer = nil
parts = append(parts, string(delimiter))
} else {
buffer = append(buffer, s[i])
}
}

// Add the remaining characters in the buffer
if len(buffer) > 0 {
parts = append(parts, string(buffer))
}

return parts
}

func waitForDial(tsConn *tsnet.Server, workspaceId string, projectName string, dialStartTime time.Time, dialTimeout time.Duration, statusProgram *tea.Program) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/workspace/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var InfoCmd = &cobra.Command{
return
}

info.Render(workspace)
info.Render(workspace, "")
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) > 0 {
Expand Down
Loading

0 comments on commit 1fac630

Please sign in to comment.