Skip to content

Commit

Permalink
refactor: active profile error handling (#1281)
Browse files Browse the repository at this point in the history
Signed-off-by: Toma Puljak <[email protected]>
  • Loading branch information
Tpuljak authored Oct 22, 2024
1 parent ede3673 commit 8964d52
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 41 deletions.
8 changes: 7 additions & 1 deletion cmd/daytona/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,20 @@ func GetConfig() (*Config, error) {
return &c, nil
}

var ErrNoProfilesFound = errors.New("no profiles found. Run `daytona serve` to create a default profile or `daytona profile add` to connect to a remote server")

func (c *Config) GetActiveProfile() (Profile, error) {
if len(c.Profiles) == 0 {
return Profile{}, ErrNoProfilesFound
}

for _, profile := range c.Profiles {
if profile.Id == c.ActiveProfileId {
return profile, nil
}
}

return Profile{}, errors.New("active profile not found")
return Profile{}, errors.New("active profile not found. Set an active profile with `daytona profile use`")
}

func (c *Config) Save() error {
Expand Down
40 changes: 0 additions & 40 deletions pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package cmd

import (
"errors"
"fmt"
"os"
"os/signal"
Expand Down Expand Up @@ -94,13 +93,6 @@ func Execute() error {
return cmd.Help()
}

if !isCompletion {
err = ensureProfiles(cmd)
if err != nil {
return err
}
}

err = rootCmd.Execute()

endTime := time.Now()
Expand Down Expand Up @@ -157,38 +149,6 @@ func validateCommands(rootCmd *cobra.Command, args []string) (cmd *cobra.Command
return currentCmd, flags, false, nil
}

func ensureProfiles(cmd *cobra.Command) error {
exemptions := []string{
"daytona",
"daytona autocomplete",
"daytona help",
"daytona docs",
"daytona generate-docs",
"daytona version",
"daytona profile add",
"daytona serve",
"daytona server",
"daytona ide",
"daytona telemetry enable",
"daytona telemetry disable",
}

if slices.Contains(exemptions, cmd.CommandPath()) {
return nil
}

c, err := config.GetConfig()
if err != nil {
return err
}

if len(c.Profiles) == 0 {
return errors.New("no profiles found. Run `daytona serve` to create a default profile or `daytona profile add` to connect to a remote server.")
}

return nil
}

func SetupRootCommand(cmd *cobra.Command) {
// Common commands
cmd.AddCommand(AutoCompleteCmd)
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/profile/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ var profileListCmd = &cobra.Command{
return err
}

if len(c.Profiles) == 0 {
return config.ErrNoProfilesFound
}

if format.FormatFlag != "" {
formattedData := format.NewFormatter(c.Profiles)
formattedData.Print()
Expand Down

0 comments on commit 8964d52

Please sign in to comment.