From 03617f68b1bcc4ffca5fd846379855ae4b1d3bd6 Mon Sep 17 00:00:00 2001 From: Alec Fong Date: Sat, 11 May 2024 18:30:26 +0000 Subject: [PATCH] fix linting, use async --- pkg/cmd/envsetup/envsetup.go | 1 - pkg/cmd/ollama/ollama.go | 63 +++++++++++++----------------- pkg/cmd/open/open.go | 1 - pkg/cmd/updatemodel/updatemodel.go | 3 +- pkg/cmd/upgrade/upgrade.go | 1 - pkg/collections/collections.go | 24 ++++++++++++ pkg/entity/entity.go | 2 +- pkg/store/organization.go | 3 -- pkg/store/ssh.go | 3 -- 9 files changed, 53 insertions(+), 48 deletions(-) diff --git a/pkg/cmd/envsetup/envsetup.go b/pkg/cmd/envsetup/envsetup.go index 400ce924..03ba625d 100644 --- a/pkg/cmd/envsetup/envsetup.go +++ b/pkg/cmd/envsetup/envsetup.go @@ -452,7 +452,6 @@ logs: stringToAppend := fmt.Sprintf("\nlogs_enabled: true\nhostname: %s\n", hostname) // add logs_enabled: true to /etc/datadog-agent/datadog.yaml err = e.store.AppendString("/etc/datadog-agent/datadog.yaml", stringToAppend) - if err != nil { return breverrors.WrapAndTrace(err) } diff --git a/pkg/cmd/ollama/ollama.go b/pkg/cmd/ollama/ollama.go index 562b0da9..1eea7bb2 100644 --- a/pkg/cmd/ollama/ollama.go +++ b/pkg/cmd/ollama/ollama.go @@ -9,6 +9,7 @@ import ( "github.com/google/uuid" "github.com/brevdev/brev-cli/pkg/cmd/util" + "github.com/brevdev/brev-cli/pkg/collections" "github.com/brevdev/brev-cli/pkg/config" "github.com/brevdev/brev-cli/pkg/entity" "github.com/brevdev/brev-cli/pkg/store" @@ -71,22 +72,20 @@ func NewCmdOllama(t *terminal.Terminal, ollamaStore OllamaStore) *cobra.Command return fmt.Errorf("invalid model type: %s", model) } - // Channel to get the result of the network call - resultCh := make(chan error) - defer close(resultCh) // TODO: im thinking this is needed? - // Start the network call in a goroutine - go func() { + res := collections.Async(func() (any, error) { err := runOllamaWorkspace(t, model, ollamaStore) - resultCh <- err - }() + if err != nil { + return nil, breverrors.WrapAndTrace(err) + } + return nil, nil + }) // Type out the creating workspace message hello.TypeItToMeUnskippable27("🤙🦙🤙🦙🤙🦙🤙") t.Vprint(t.Green("\n\n\n")) - // Wait for the network call to finish - err := <-resultCh + _, err := res.Await() if err != nil { return breverrors.WrapAndTrace(err) } @@ -98,7 +97,7 @@ func NewCmdOllama(t *terminal.Terminal, ollamaStore OllamaStore) *cobra.Command return cmd } -func runOllamaWorkspace(t *terminal.Terminal, model string, ollamaStore OllamaStore) error { +func runOllamaWorkspace(t *terminal.Terminal, model string, ollamaStore OllamaStore) error { //nolint:funlen // todo _, err := ollamaStore.GetCurrentUser() if err != nil { return breverrors.WrapAndTrace(err) @@ -118,26 +117,22 @@ func runOllamaWorkspace(t *terminal.Terminal, model string, ollamaStore OllamaSt hello.TypeItToMeUnskippable27(fmt.Sprintf("Creating Ollama server %s with model %s in org %s", t.Green(cwOptions.Name), t.Green(model), t.Green(org.ID))) - w := &entity.Workspace{} - workspaceCh := make(chan *entity.Workspace) s := t.NewSpinner() - go func() { - w, err = ollamaStore.CreateWorkspace(org.ID, cwOptions) - if err != nil { - // ideally log something here? - workspaceCh <- nil - return + createWorkspaceRes := collections.Async(func() (*entity.Workspace, error) { + w, errr := ollamaStore.CreateWorkspace(org.ID, cwOptions) + if errr != nil { + return nil, breverrors.WrapAndTrace(errr) } - workspaceCh <- w - }() + return w, nil + }) s.Suffix = " Creating your workspace. Hang tight 🤙" s.Start() - w = <-workspaceCh - if w == nil { - return breverrors.New("workspace creation failed") + w, err := createWorkspaceRes.Await() + if err != nil { + return breverrors.WrapAndTrace(err) } var vmStatus bool @@ -154,24 +149,20 @@ func runOllamaWorkspace(t *terminal.Terminal, model string, ollamaStore OllamaSt // TODO: look into timing of verb call time.Sleep(time.Second * 10) - lf := &store.BuildVerbRes{} - verbCh := make(chan *store.BuildVerbRes) - go func() { - lf, err = ollamaStore.BuildVerbContainer(w.ID, verbYaml) - if err != nil { - // ideally log something here? - verbCh <- nil - return + verbBuildRes := collections.Async(func() (*store.BuildVerbRes, error) { + lf, errr := ollamaStore.BuildVerbContainer(w.ID, verbYaml) + if errr != nil { + return nil, breverrors.WrapAndTrace(errr) } - verbCh <- lf - }() + return lf, nil + }) s.Start() s.Suffix = "Starting the Ollama server. Hang tight 🤙" - lf = <-verbCh - if lf == nil { - return breverrors.New("verb container build did not start") + _, err = verbBuildRes.Await() + if err != nil { + return breverrors.WrapAndTrace(err) } var vstatus bool diff --git a/pkg/cmd/open/open.go b/pkg/cmd/open/open.go index cfb5d02d..1509bd3d 100644 --- a/pkg/cmd/open/open.go +++ b/pkg/cmd/open/open.go @@ -101,7 +101,6 @@ func runOpenCommand(t *terminal.Terminal, tstore OpenStore, wsIDOrName string, s } workspace, err = util.GetUserWorkspaceByNameOrIDErr(tstore, wsIDOrName) - if err != nil { return breverrors.WrapAndTrace(err) } diff --git a/pkg/cmd/updatemodel/updatemodel.go b/pkg/cmd/updatemodel/updatemodel.go index 85ed26d6..032563f2 100644 --- a/pkg/cmd/updatemodel/updatemodel.go +++ b/pkg/cmd/updatemodel/updatemodel.go @@ -179,7 +179,6 @@ func (u updateModel) RunE(_ *cobra.Command, _ []string) error { //nolint:funlen ReposV1: rm.MergeBE(), }, ) - if err != nil { return breverrors.WrapAndTrace(err) } @@ -212,7 +211,7 @@ func (u updateModel) RunE(_ *cobra.Command, _ []string) error { //nolint:funlen } return multierror.Append(acc, err) } - if acc == nil && err != nil { + if acc == nil { return breverrors.WrapAndTrace(err) } return acc diff --git a/pkg/cmd/upgrade/upgrade.go b/pkg/cmd/upgrade/upgrade.go index 772ddd7b..8cdadb9f 100644 --- a/pkg/cmd/upgrade/upgrade.go +++ b/pkg/cmd/upgrade/upgrade.go @@ -103,7 +103,6 @@ var upgradeFuncs = map[string]uFunc{ url, "/usr/local/bin/brev", ) - if err != nil { return breverrors.WrapAndTrace(err) } diff --git a/pkg/collections/collections.go b/pkg/collections/collections.go index 4b422a9d..e569ea60 100644 --- a/pkg/collections/collections.go +++ b/pkg/collections/collections.go @@ -590,3 +590,27 @@ func GetRequestWithContext(ctx context.Context, url string) (*http.Response, err } return resp, nil } + +type AsyncResult[T any] struct { + result chan result[T] +} + +type result[T any] struct { + value T + err error +} + +func Async[T any](f func() (T, error)) *AsyncResult[T] { + r := &AsyncResult[T]{result: make(chan result[T], 1)} // Buffered channel + go func() { + value, err := f() + r.result <- result[T]{value: value, err: err} + }() + return r +} + +// Await blocks until the asynchronous operation completes, returning the result and error. +func (ar *AsyncResult[T]) Await() (T, error) { + r := <-ar.result // This will block until the result is available + return r.value, r.err +} diff --git a/pkg/entity/entity.go b/pkg/entity/entity.go index 45f9ab23..1e27d5ce 100644 --- a/pkg/entity/entity.go +++ b/pkg/entity/entity.go @@ -342,7 +342,7 @@ type Tunnel struct { TunnelStatus string `json:"tunnelStatus"` } -// TODO Change this to Application. Theres an older application struct that should be removed +// CFApplication TODO Change this to Application. Theres an older application struct that should be removed type CFApplication struct { CloudflareApplicationID string `json:"cloudflareApplicationID"` CloudflareDNSRecordID string `json:"cloudflareDnsRecordID"` diff --git a/pkg/store/organization.go b/pkg/store/organization.go index 1ca13e9d..dedea1a8 100644 --- a/pkg/store/organization.go +++ b/pkg/store/organization.go @@ -30,9 +30,6 @@ func (f FileStore) ClearDefaultOrganization() error { return breverrors.WrapAndTrace(err) } path := files.GetActiveOrgsPath(home) - if err != nil { - return breverrors.WrapAndTrace(err) - } err = files.DeleteFile(f.fs, path) if err != nil { return breverrors.WrapAndTrace(err) diff --git a/pkg/store/ssh.go b/pkg/store/ssh.go index fff42602..7c32d4c6 100644 --- a/pkg/store/ssh.go +++ b/pkg/store/ssh.go @@ -180,9 +180,6 @@ func (f FileStore) CreateNewSSHConfigBackup() error { return breverrors.WrapAndTrace(err) } backupFilePath := files.GetNewBackupSSHConfigFilePath(home) - if err != nil { - return breverrors.WrapAndTrace(err) - } csp, err := files.GetUserSSHConfigPath(home) if err != nil {