Skip to content

Commit

Permalink
Merge pull request #1439 from onflow/chasefleming/start-emulator-on-dev
Browse files Browse the repository at this point in the history
Start emulator on `flow dev` with flag
  • Loading branch information
chasefleming authored Mar 8, 2024
2 parents 6f79a6a + 6c89295 commit 3f9f395
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ jobs:
- uses: golangci/[email protected]
with:
version: v1.52.2
only-new-issues: true
skip-pkg-cache: true
args: --timeout=3m
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/psiemens/sconfig v0.1.0
github.com/radovskyb/watcher v1.0.7
github.com/rs/zerolog v1.29.0
github.com/sergi/go-diff v1.3.1
github.com/spf13/afero v1.10.0
github.com/spf13/cobra v1.8.0
Expand Down Expand Up @@ -201,7 +202,6 @@ require (
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/rs/cors v1.8.0 // indirect
github.com/rs/zerolog v1.29.0 // indirect
github.com/sethvargo/go-retry v0.2.3 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/skeema/knownhosts v1.2.1 // indirect
Expand Down
66 changes: 55 additions & 11 deletions internal/super/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,27 @@
package super

import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"strings"
"syscall"

"github.com/onflow/cadence/runtime/parser"
"github.com/spf13/cobra"

"github.com/onflow/flow-emulator/server"
"github.com/onflow/flowkit"
"github.com/onflow/flowkit/output"
"github.com/rs/zerolog"
"github.com/spf13/cobra"

"github.com/onflow/flow-cli/internal/command"
)

type flagsDev struct{}
type flagsDev struct {
StartEmulator bool `default:"false" flag:"start-emulator" info:"Start emulator if not already running"`
}

var devFlags = flagsDev{}

Expand Down Expand Up @@ -61,22 +67,60 @@ func dev(
return nil, err
}

err = flow.Ping()
serviceAccount, err := state.EmulatorServiceAccount()
if err != nil {
logger.Error("Error connecting to emulator. Make sure you started an emulator using 'flow emulator' command.")
logger.Info(fmt.Sprintf("%s This tool requires emulator to function. Emulator needs to be run inside the project root folder where the configuration file ('flow.json') exists.\n\n", output.TryEmoji()))
return nil, nil
return nil, err
}

service, err := state.EmulatorServiceAccount()
if err != nil {
return nil, err
if devFlags.StartEmulator {
privateKey, err := serviceAccount.Key.PrivateKey()
if err != nil {
return nil, fmt.Errorf("failed to get private key: %s", err)
}

consoleWriter := zerolog.ConsoleWriter{Out: os.Stdout}
log := zerolog.New(consoleWriter).With().Timestamp().Logger()

emulatorServer := server.NewEmulatorServer(&log, &server.Config{
ServicePublicKey: (*privateKey).PublicKey(),
ServicePrivateKey: *privateKey,
ServiceKeySigAlgo: serviceAccount.Key.SigAlgo(),
ServiceKeyHashAlgo: serviceAccount.Key.HashAlgo(),
})

emuErr := emulatorServer.Listen()
if emuErr != nil {
return nil, fmt.Errorf("failed to prepare the emulator for connections: %s", emuErr)
}

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
emulatorServer.Start()
<-c
os.Exit(1)
}()

ctx := context.Background()
err = flow.WaitServer(ctx)
if err != nil {
logger.Error("Error connecting to emulator. Make sure you started an emulator using 'flow emulator' command.")
logger.Info(fmt.Sprintf("%s This tool requires emulator to function. Emulator needs to be run inside the project root folder where the configuration file ('flow.json') exists.\n\n", output.TryEmoji()))
return nil, err
}
} else {
err = flow.Ping()
if err != nil {
logger.Error("Error connecting to emulator. Make sure you started an emulator using 'flow emulator' command.")
logger.Info(fmt.Sprintf("%s This tool requires emulator to function. Emulator needs to be run inside the project root folder where the configuration file ('flow.json') exists.\n\n", output.TryEmoji()))
return nil, err
}
}

flow.SetLogger(output.NewStdoutLogger(output.NoneLog))

project, err := newProject(
*service,
*serviceAccount,
flow,
state,
newProjectFiles(dir),
Expand Down

0 comments on commit 3f9f395

Please sign in to comment.