Skip to content

Commit

Permalink
fix linting, use async
Browse files Browse the repository at this point in the history
  • Loading branch information
theFong committed May 11, 2024
1 parent 90812ad commit 03617f6
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 48 deletions.
1 change: 0 additions & 1 deletion pkg/cmd/envsetup/envsetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
63 changes: 27 additions & 36 deletions pkg/cmd/ollama/ollama.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion pkg/cmd/open/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/cmd/updatemodel/updatemodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ func (u updateModel) RunE(_ *cobra.Command, _ []string) error { //nolint:funlen
ReposV1: rm.MergeBE(),
},
)

if err != nil {
return breverrors.WrapAndTrace(err)
}
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion pkg/cmd/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ var upgradeFuncs = map[string]uFunc{
url,
"/usr/local/bin/brev",
)

if err != nil {
return breverrors.WrapAndTrace(err)
}
Expand Down
24 changes: 24 additions & 0 deletions pkg/collections/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion pkg/entity/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
3 changes: 0 additions & 3 deletions pkg/store/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions pkg/store/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 03617f6

Please sign in to comment.