Skip to content

Commit

Permalink
chore: add linter configuration (#261)
Browse files Browse the repository at this point in the history
Add golangci-lint configuration and fix all linting errors

Signed-off-by: Toma Puljak <[email protected]>
  • Loading branch information
Tpuljak authored Mar 21, 2024
1 parent 86a5482 commit 308c859
Show file tree
Hide file tree
Showing 17 changed files with 104 additions and 55 deletions.
4 changes: 3 additions & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ ADD postcreate.sh /setup/postcreate.sh
RUN chmod +x /setup/postcreate.sh

RUN apt update -y && \
apt install curl protobuf-compiler openjdk-11-jdk libprotobuf-dev -y
apt install curl protobuf-compiler openjdk-11-jdk libprotobuf-dev -y

RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.57.1
8 changes: 8 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
linters-settings:
errcheck:
exclude-functions:
- (*github.com/gin-gonic/gin.Context).AbortWithError
- io.Copy
- syscall.Syscall
- (github.com/gliderlabs/ssh.Session).Exit
- (io.Writer).Write
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Follow the following steps to ensure your contribution goes smoothly.
1. Create a [GitHub issue](https://github.com/daytonaio/daytona/issues) if one doesn't exist already.
1. [Prepare your changes](/PREPARING_YOUR_CHANGES.md) and ensure your commits are descriptive. The document contains an optional commit template, if desired.
1. Ensure that you sign off on all your commits to comply with the DCO v1.1. We have more details in [Prepare your changes](/PREPARING_YOUR_CHANGES.md).
1. Ensure that you have no lint errors. We use `golangci-lint` as our linter which you can install by following instructions found [here](https://golangci-lint.run/welcome/install/#local-installation) (or simply open Daytona in a Dev Container). You can check for linting errors by running `golangci-lint run` in the root of the project.
1. Create a pull request on GitHub. If you're new to GitHub, read about [pull requests](https://help.github.com/articles/about-pull-requests/). You are welcome to submit your pull request for commentary or review before it is complete by creating a [draft pull request](https://help.github.com/en/articles/about-pull-requests#draft-pull-requests). Please include specific questions or items you'd like feedback on.
1. A member of the Daytona team will review your PR within three business days (excluding any holidays) and either merge, comment, and/or assign someone for review.
1. Work with the reviewer to complete a code review. For each change, create a new commit and push it to make changes to your pull request. When necessary, the reviewer can trigger CI to run tests prior to merging.
Expand Down
2 changes: 1 addition & 1 deletion internal/util/apiclient/server/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func GetApiClient(profile *config.Profile) (*serverapiclient.APIClient, error) {
return nil, err
}

serverUrl := "http://localhost:3000"
var serverUrl string

if envApiUrl, ok := os.LookupEnv("DAYTONA_SERVER_API_URL"); ok {
serverUrl = envApiUrl
Expand Down
5 changes: 3 additions & 2 deletions internal/util/git_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
package util

import (
"errors"

"github.com/daytonaio/daytona/pkg/types"
)

func GetGitContextFromUrl(name string) (*types.Repository, error) {
panic("not implemented")
return &types.Repository{}, nil
return nil, errors.New("not implemented")
}
8 changes: 6 additions & 2 deletions pkg/agent/ssh/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package ssh
import (
"fmt"
"io"
"log"
"os"
"os/exec"
"strings"
Expand All @@ -19,6 +18,8 @@ import (
"github.com/pkg/sftp"
crypto_ssh "golang.org/x/crypto/ssh"
"golang.org/x/sys/unix"

log "github.com/sirupsen/logrus"
)

func Start() {
Expand Down Expand Up @@ -184,7 +185,10 @@ func handleNonPty(s ssh.Session) {
go func() {
for sig := range sigs {
signal := osSignalFrom(sig)
cmd.Process.Signal(signal)
err := cmd.Process.Signal(signal)
if err != nil {
log.Println("signal error: ", err)
}
}
}()
err = cmd.Wait()
Expand Down
5 changes: 3 additions & 2 deletions pkg/agent/tailscale/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"flag"
"fmt"
"io"
"log"
"net"
"net/http"
"net/netip"
Expand All @@ -18,6 +17,8 @@ import (
"github.com/daytonaio/daytona/pkg/agent/config"
"github.com/daytonaio/daytona/pkg/serverapiclient"
"tailscale.com/tsnet"

log "github.com/sirupsen/logrus"
)

func Start(c *config.Config) error {
Expand Down Expand Up @@ -54,7 +55,7 @@ func Start(c *config.Config) error {
defer src.Close()
dst, err := net.Dial("tcp", fmt.Sprintf("localhost:%d", destPort))
if err != nil {
log.Printf("Dial failed: %v", err)
log.Errorf("Dial failed: %v", err)
return
}
defer dst.Close()
Expand Down
5 changes: 4 additions & 1 deletion pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ var rootCmd = &cobra.Command{
Long: "Daytona is a Dev Environment Manager",
Run: func(cmd *cobra.Command, args []string) {
fmt.Print(view_util.GetLongDescription())
cmd.Help()
err := cmd.Help()
if err != nil {
log.Fatal(err)
}
},
}

Expand Down
2 changes: 0 additions & 2 deletions pkg/cmd/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"github.com/spf13/cobra"
)

var providerNameArg string

var ProviderCmd = &cobra.Command{
Use: "provider",
Short: "Manage providers",
Expand Down
2 changes: 0 additions & 2 deletions pkg/cmd/target/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"github.com/spf13/cobra"
)

var targetNameArg string

var TargetCmd = &cobra.Command{
Use: "target",
Short: "Manage provider targets",
Expand Down
20 changes: 16 additions & 4 deletions pkg/cmd/workspace/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ var CreateCmd = &cobra.Command{
fmt.Println("Error running status program:", err)
statusProgram.Send(status.ClearScreenMsg{})
statusProgram.Send(tea.Quit())
statusProgram.ReleaseTerminal()
err := statusProgram.ReleaseTerminal()
if err != nil {
log.Error(err)
}
os.Exit(1)
}
}()
Expand Down Expand Up @@ -227,7 +230,10 @@ func processPrompting(cmd *cobra.Command, apiClient *serverapiclient.APIClient,

if argRepos != nil {
view_util.RenderInfoMessage("Error: workspace name argument is required for this command")
cmd.Help()
err := cmd.Help()
if err != nil {
log.Fatal(err)
}
os.Exit(1)
}

Expand Down Expand Up @@ -277,7 +283,10 @@ func processCmdArguments(cmd *cobra.Command, args []string, apiClient *serverapi
repoUrls = argRepos
} else {
view_util.RenderInfoMessage("Error: --repo flag is required for this command")
cmd.Help()
err := cmd.Help()
if err != nil {
log.Fatal(err)
}
os.Exit(1)
}

Expand Down Expand Up @@ -347,7 +356,10 @@ func waitForDial(tsConn *tsnet.Server, workspaceId string, projectName string, d
func cleanUpTerminal(statusProgram *tea.Program, err error) {
statusProgram.Send(status.ClearScreenMsg{})
statusProgram.Send(tea.Quit())
statusProgram.ReleaseTerminal()
releaseError := statusProgram.ReleaseTerminal()
if releaseError != nil {
log.Error(releaseError)
}
if err != nil {
log.Fatal(err)
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/cmd/workspace/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ var DeleteCmd = &cobra.Command{
log.Fatal(apiclient.HandleErrorResponse(res, err))
}

config.RemoveWorkspaceSshEntries(activeProfile.Id, *workspace.Id)
err = config.RemoveWorkspaceSshEntries(activeProfile.Id, *workspace.Id)
if err != nil {
log.Fatal(err)
}

util.RenderInfoMessage(fmt.Sprintf("Workspace %s successfully deleted", *workspace.Name))
},
Expand Down
5 changes: 4 additions & 1 deletion pkg/cmd/workspace/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ func init() {
StartCmd.PersistentFlags().StringVarP(&startProjectFlag, "project", "p", "", "Start a single project in the workspace (project name)")
StartCmd.PersistentFlags().BoolVarP(&allFlag, "all", "a", false, "Start all workspaces")

StartCmd.RegisterFlagCompletionFunc("project", getProjectNameCompletions)
err := StartCmd.RegisterFlagCompletionFunc("project", getProjectNameCompletions)
if err != nil {
log.Error("failed to register completion function: ", err)
}
}

func startAllWorkspaces() error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/api/controllers/binary/binaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func getBinaryPath(binaryName, binaryVersion string) (string, error) {
return "", err
}

binaryOs := daytona_os.Linux_64_86
var binaryOs daytona_os.OperatingSystem
split := strings.Split(binaryName, "-")
if len(split) != 3 {
return "", fmt.Errorf("invalid binary name: %s", binaryName)
Expand Down
6 changes: 5 additions & 1 deletion pkg/server/api/controllers/workspace/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ func CreateWorkspace(ctx *gin.Context) {
}

log.Debug(w)
db.SaveWorkspace(w)
err = db.SaveWorkspace(w)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, fmt.Errorf("failed to save workspace: %s", err.Error()))
return
}

err = provisioner.CreateWorkspace(w)
if err != nil {
Expand Down
55 changes: 44 additions & 11 deletions pkg/server/provisioner/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,37 +71,46 @@ func CreateWorkspace(workspace *types.Workspace) error {
projectLogWriter.Write([]byte(fmt.Sprintf("Creating project %s\n", project.Name)))

// todo: go routines
event_bus.Publish(event_bus.Event{
err = event_bus.Publish(event_bus.Event{
Name: event_bus.WorkspaceEventProjectCreating,
Payload: event_bus.WorkspaceEventPayload{
WorkspaceName: workspace.Name,
ProjectName: project.Name,
},
})
if err != nil {
log.Error(err)
}
_, err = (*provider).CreateProject(&provider_types.ProjectRequest{
TargetOptions: target.Options,
Project: project,
})
if err != nil {
return err
}
event_bus.Publish(event_bus.Event{
err = event_bus.Publish(event_bus.Event{
Name: event_bus.WorkspaceEventProjectCreated,
Payload: event_bus.WorkspaceEventPayload{
WorkspaceName: workspace.Name,
ProjectName: project.Name,
},
})
if err != nil {
log.Error(err)
}

projectLogWriter.Write([]byte(fmt.Sprintf("Project %s created\n", project.Name)))
}

event_bus.Publish(event_bus.Event{
err = event_bus.Publish(event_bus.Event{
Name: event_bus.WorkspaceEventCreated,
Payload: event_bus.WorkspaceEventPayload{
WorkspaceName: workspace.Name,
},
})
if err != nil {
log.Error(err)
}

wsLogWriter.Write([]byte("Workspace creation completed\n"))

Expand Down Expand Up @@ -134,17 +143,23 @@ func StartWorkspace(workspace *types.Workspace) error {
return err
}

(*provider).StartWorkspace(&provider_types.WorkspaceRequest{
_, err = (*provider).StartWorkspace(&provider_types.WorkspaceRequest{
TargetOptions: target.Options,
Workspace: workspace,
})
if err != nil {
return err
}

event_bus.Publish(event_bus.Event{
err = event_bus.Publish(event_bus.Event{
Name: event_bus.WorkspaceEventStarting,
Payload: event_bus.WorkspaceEventPayload{
WorkspaceName: workspace.Name,
},
})
if err != nil {
log.Error(err)
}

for _, project := range workspace.Projects {

Expand Down Expand Up @@ -172,12 +187,15 @@ func StartWorkspace(workspace *types.Workspace) error {
}
}

event_bus.Publish(event_bus.Event{
err = event_bus.Publish(event_bus.Event{
Name: event_bus.WorkspaceEventStarted,
Payload: event_bus.WorkspaceEventPayload{
WorkspaceName: workspace.Name,
},
})
if err != nil {
log.Error(err)
}
return nil
}

Expand Down Expand Up @@ -216,17 +234,23 @@ func StopWorkspace(workspace *types.Workspace) error {
return err
}

(*provider).StopWorkspace(&provider_types.WorkspaceRequest{
_, err = (*provider).StopWorkspace(&provider_types.WorkspaceRequest{
TargetOptions: target.Options,
Workspace: workspace,
})
if err != nil {
return err
}

event_bus.Publish(event_bus.Event{
err = event_bus.Publish(event_bus.Event{
Name: event_bus.WorkspaceEventStopping,
Payload: event_bus.WorkspaceEventPayload{
WorkspaceName: workspace.Name,
},
})
if err != nil {
log.Error(err)
}

for _, project := range workspace.Projects {
// todo: go routines
Expand All @@ -239,12 +263,15 @@ func StopWorkspace(workspace *types.Workspace) error {
}
}

event_bus.Publish(event_bus.Event{
err = event_bus.Publish(event_bus.Event{
Name: event_bus.WorkspaceEventStopped,
Payload: event_bus.WorkspaceEventPayload{
WorkspaceName: workspace.Name,
},
})
if err != nil {
log.Error(err)
}

return nil
}
Expand Down Expand Up @@ -284,12 +311,15 @@ func DestroyWorkspace(workspace *types.Workspace) error {
return err
}

event_bus.Publish(event_bus.Event{
err = event_bus.Publish(event_bus.Event{
Name: event_bus.WorkspaceEventRemoving,
Payload: event_bus.WorkspaceEventPayload{
WorkspaceName: workspace.Name,
},
})
if err != nil {
log.Error(err)
}

for _, project := range workspace.Projects {
// todo: go routines
Expand All @@ -310,12 +340,15 @@ func DestroyWorkspace(workspace *types.Workspace) error {
return err
}

event_bus.Publish(event_bus.Event{
err = event_bus.Publish(event_bus.Event{
Name: event_bus.WorkspaceEventRemoved,
Payload: event_bus.WorkspaceEventPayload{
WorkspaceName: workspace.Name,
},
})
if err != nil {
log.Error(err)
}

err = config.DeleteWorkspaceLogs(workspace.Id)
if err != nil {
Expand Down
Loading

0 comments on commit 308c859

Please sign in to comment.