Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

telemetry: track command errors #1164

Merged
merged 3 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions cmd/daytona/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ import (

func main() {
if internal.WorkspaceMode() {
workspacemode.Execute()
err := workspacemode.Execute()
if err != nil {
log.Fatal(err)
}
return
}

cmd.Execute()
err := cmd.Execute()
if err != nil {
log.Fatal(err)
}
}

func init() {
Expand Down
11 changes: 4 additions & 7 deletions pkg/cmd/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var AgentCmd = &cobra.Command{
Use: "agent",
Short: "Start the agent process",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
setLogLevel()

agentMode := config.ModeProject
Expand All @@ -37,7 +37,7 @@ var AgentCmd = &cobra.Command{

c, err := config.GetConfig(agentMode)
if err != nil {
log.Fatal(err)
return err
}
c.ProjectDir = filepath.Join(os.Getenv("HOME"), c.ProjectName)

Expand All @@ -50,7 +50,7 @@ var AgentCmd = &cobra.Command{
if c.LogFilePath != nil {
logFile, err := os.OpenFile(*c.LogFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
return err
}
defer logFile.Close()
gitLogWriter = io.MultiWriter(os.Stdout, logFile)
Expand Down Expand Up @@ -91,10 +91,7 @@ var AgentCmd = &cobra.Command{
TelemetryEnabled: telemetryEnabled,
}

err = agent.Start()
if err != nil {
log.Fatal(err)
}
return agent.Start()
},
}

Expand Down
14 changes: 7 additions & 7 deletions pkg/cmd/agent/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ package agent

import (
"context"
"errors"
"fmt"
"io"
"os"

"github.com/daytonaio/daytona/internal/util"
"github.com/daytonaio/daytona/pkg/agent/config"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -20,16 +20,16 @@ var followFlag bool
var logsCmd = &cobra.Command{
Use: "logs",
Short: "Output Daytona Agent logs",
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
logFilePath := config.GetLogFilePath()

if logFilePath == nil {
log.Fatal("Log file path not set")
return errors.New("log file path not set")
}

file, err := os.Open(*logFilePath)
if err != nil {
log.Fatal(err)
return err
}
defer file.Close()

Expand All @@ -41,13 +41,13 @@ var logsCmd = &cobra.Command{
for {
select {
case <-context.Background().Done():
return
return nil
case err := <-errChan:
if err != nil {
if err != io.EOF {
log.Fatal(err)
return err
}
return
return nil
}
case msg := <-msgChan:
fmt.Println(string(msg))
Expand Down
17 changes: 9 additions & 8 deletions pkg/cmd/apikey/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ package apikey

import (
"context"
"errors"

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

"github.com/daytonaio/daytona/internal/util"
Expand All @@ -21,18 +21,18 @@ var GenerateCmd = &cobra.Command{
Short: "Generate a new API key",
Aliases: []string{"g", "new"},
Args: cobra.RangeArgs(0, 1),
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
var keyName string

apiClient, err := apiclient_util.GetApiClient(nil)
if err != nil {
log.Fatal(err)
return err
}

apiKeyList, _, err := apiClient.ApiKeyAPI.ListClientApiKeys(ctx).Execute()
if err != nil {
log.Fatal(apiclient_util.HandleErrorResponse(nil, err))
return apiclient_util.HandleErrorResponse(nil, err)
}

if len(args) == 1 {
Expand All @@ -43,26 +43,27 @@ var GenerateCmd = &cobra.Command{

for _, key := range apiKeyList {
if key.Name == keyName {
log.Fatal("key name already exists, please choose a different one")
return errors.New("key name already exists, please choose a different one")
}
}

key, _, err := apiClient.ApiKeyAPI.GenerateApiKey(ctx, keyName).Execute()
if err != nil {
log.Fatal(apiclient_util.HandleErrorResponse(nil, err))
return apiclient_util.HandleErrorResponse(nil, err)
}

serverConfig, _, err := apiClient.ServerAPI.GetConfigExecute(apiclient.ApiGetConfigRequest{})
if err != nil {
log.Fatal(err)
return err
}

if serverConfig.Frps == nil {
log.Fatal("frps config is missing")
return errors.New("frps config is missing")
}

apiUrl := util.GetFrpcApiUrl(serverConfig.Frps.Protocol, serverConfig.Id, serverConfig.Frps.Domain)

view.Render(key, apiUrl)
return nil
},
}
12 changes: 6 additions & 6 deletions pkg/cmd/apikey/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,34 @@ import (
"github.com/daytonaio/daytona/internal/util/apiclient"
"github.com/daytonaio/daytona/pkg/cmd/format"
"github.com/daytonaio/daytona/pkg/views/server/apikey"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var listCmd = &cobra.Command{
Use: "list",
Short: "List API keys",
Aliases: []string{"ls"},
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()

apiClient, err := apiclient.GetApiClient(nil)
if err != nil {
log.Fatal(err)
return err
}

apiKeyList, _, err := apiClient.ApiKeyAPI.ListClientApiKeys(ctx).Execute()
apiKeyList, res, err := apiClient.ApiKeyAPI.ListClientApiKeys(ctx).Execute()
if err != nil {
log.Fatal(apiclient.HandleErrorResponse(nil, err))
return apiclient.HandleErrorResponse(res, err)
}

if format.FormatFlag != "" {
formattedData := format.NewFormatter(apiKeyList)
formattedData.Print()
return
return nil
}

apikey.ListApiKeys(apiKeyList)
return nil
},
}

Expand Down
26 changes: 14 additions & 12 deletions pkg/cmd/apikey/revoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ package apikey

import (
"context"
"errors"
"fmt"

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

"github.com/daytonaio/daytona/cmd/daytona/config"
Expand All @@ -27,29 +27,29 @@ var revokeCmd = &cobra.Command{
Short: "Revoke an API key",
Aliases: []string{"r", "rm", "delete"},
Args: cobra.RangeArgs(0, 1),
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()

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

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

apiClient, err := apiclient_util.GetApiClient(nil)
if err != nil {
log.Fatal(err)
return err
}

var selectedApiKey *apiclient.ApiKey

apiKeyList, _, err := apiClient.ApiKeyAPI.ListClientApiKeys(ctx).Execute()
if err != nil {
log.Fatal(apiclient_util.HandleErrorResponse(nil, err))
return apiclient_util.HandleErrorResponse(nil, err)
}

if len(args) == 1 {
Expand All @@ -63,15 +63,15 @@ var revokeCmd = &cobra.Command{
selectedApiKey, err = apikey.GetApiKeyFromPrompt(apiKeyList, "Select an API key to revoke", false)
if err != nil {
if common.IsCtrlCAbort(err) {
return
return nil
} else {
log.Fatal(err)
return err
}
}
}

if selectedApiKey == nil {
log.Fatal("No API key selected")
return errors.New("No API key selected")
}

if !yesFlag {
Expand All @@ -93,20 +93,22 @@ var revokeCmd = &cobra.Command{

err := form.Run()
if err != nil {
log.Fatal(err)
return err
}
}

if yesFlag {
_, err = apiClient.ApiKeyAPI.RevokeApiKey(ctx, selectedApiKey.Name).Execute()
res, err := apiClient.ApiKeyAPI.RevokeApiKey(ctx, selectedApiKey.Name).Execute()
if err != nil {
log.Fatal(apiclient_util.HandleErrorResponse(nil, err))
return apiclient_util.HandleErrorResponse(res, err)
}

views.RenderInfoMessage("API key revoked")
} else {
fmt.Println("Operation canceled.")
}

return nil
},
}

Expand Down
22 changes: 9 additions & 13 deletions pkg/cmd/autocomplete.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ var AutoCompleteCmd = &cobra.Command{
Use: "autocomplete [bash|zsh|fish|powershell]",
Short: "Adds completion script for your shell enviornment",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
shell := args[0]
homeDir, err := os.UserHomeDir()
if err != nil {
fmt.Printf("Error finding user home directory: %s\n", err)
return
return fmt.Errorf("Error finding user home directory: %s\n", err)
}

var filePath, profilePath string
Expand All @@ -39,14 +38,12 @@ var AutoCompleteCmd = &cobra.Command{
filePath = filepath.Join(homeDir, "daytona.completion_script.ps1")
profilePath = filepath.Join(homeDir, "Documents", "WindowsPowerShell", "Microsoft.PowerShell_profile.ps1")
default:
fmt.Println("Unsupported shell type. Please use bash, zsh, fish, or powershell.")
return
return fmt.Errorf("Unsupported shell type. Please use bash, zsh, fish, or powershell.")
}

file, err := os.Create(filePath)
if err != nil {
fmt.Printf("Error creating completion script file: %s\n", err)
return
return fmt.Errorf("Error creating completion script file: %s\n", err)
}
defer file.Close()

Expand All @@ -62,8 +59,7 @@ var AutoCompleteCmd = &cobra.Command{
}

if err != nil {
fmt.Printf("Error generating completion script: %s\n", err)
return
return fmt.Errorf("Error generating completion script: %s\n", err)
}

sourceCommand := fmt.Sprintf("\nsource %s\n", filePath)
Expand All @@ -87,19 +83,19 @@ var AutoCompleteCmd = &cobra.Command{
// Append the source command to the shell's profile file if not present
profile, err := os.OpenFile(profilePath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
fmt.Printf("Error opening profile file (%s): %s\n", profilePath, err)
return
return fmt.Errorf("Error opening profile file (%s): %s\n", profilePath, err)
}
defer profile.Close()

if _, err := profile.WriteString(sourceCommand); err != nil {
fmt.Printf("Error writing to profile file (%s): %s\n", profilePath, err)
return
return fmt.Errorf("Error writing to profile file (%s): %s\n", profilePath, err)
}
}

fmt.Println("Autocomplete script generated and injected successfully.")
fmt.Printf("Please source your %s profile to apply the changes or restart your terminal.\n", shell)
fmt.Printf("For manual sourcing, use: source %s\n", profilePath)

return nil
},
}
Loading
Loading