这是indexloc提供的服务,不要输入任何密码
Skip to content
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
26 changes: 17 additions & 9 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ const (
ENV_ACCESS_KEY = "HASURA_GRAPHQL_ACCESS_KEY"
)

// Other constants used in the package
const (
// Name of the global configuration directory
GLOBAL_CONFIG_DIR_NAME = ".hasura-graphql"
// Name of the global configuration file
GLOBAL_CONFIG_FILE_NAME = "config.json"
)

// version is set at build time, denotes the CLI version.
var version string

Expand Down Expand Up @@ -96,13 +104,6 @@ type ExecutionContext struct {
LogLevel string
}

// Spin stops any existing spinner and starts a new one with the given message.
func (ec *ExecutionContext) Spin(message string) {
ec.Spinner.Stop()
ec.Spinner.Prefix = message
ec.Spinner.Start()
}

// Prepare as the name suggests, prepares the ExecutionContext ec by
// initializing most of the variables to sensible defaults, if it is not already
// set.
Expand Down Expand Up @@ -207,6 +208,13 @@ func (ec *ExecutionContext) setupSpinner() {
}
}

// Spin stops any existing spinner and starts a new one with the given message.
func (ec *ExecutionContext) Spin(message string) {
ec.Spinner.Stop()
ec.Spinner.Prefix = message
ec.Spinner.Start()
}

// setupLogger creates a default logger if context does not have one set.
func (ec *ExecutionContext) setupLogger() {
if ec.Logger == nil {
Expand Down Expand Up @@ -237,14 +245,14 @@ func (ec *ExecutionContext) setupGlobalConfigDir() error {
if err != nil {
return errors.Wrap(err, "cannot get home directory")
}
globalConfigDir := filepath.Join(home, ".hasura-graphql")
globalConfigDir := filepath.Join(home, GLOBAL_CONFIG_DIR_NAME)
ec.GlobalConfigDir = globalConfigDir
}
err := os.MkdirAll(ec.GlobalConfigDir, os.ModePerm)
if err != nil {
return errors.Wrap(err, "cannot create config directory")
}
ec.GlobalConfigFile = filepath.Join(ec.GlobalConfigDir, "config.json")
ec.GlobalConfigFile = filepath.Join(ec.GlobalConfigDir, GLOBAL_CONFIG_FILE_NAME)
return nil
}

Expand Down
74 changes: 74 additions & 0 deletions cli/cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package cli_test

import (
"math/rand"
"os"
"path/filepath"
"strconv"
"testing"
"time"

"github.com/hasura/graphql-engine/cli"
"github.com/hasura/graphql-engine/cli/commands"
"github.com/spf13/viper"
)

func init() {
rand.Seed(time.Now().UTC().UnixNano())
}

func TestPrepare(t *testing.T) {
ec := &cli.ExecutionContext{}
err := ec.Prepare()
if err != nil {
t.Fatalf("prepare failed: %v", err)
}
if ec.CMDName == "" {
t.Fatalf("expected CMDName, got: %v", ec.CMDName)
}
if ec.Spinner == nil {
t.Fatal("got spinner empty")
}
if ec.Logger == nil {
t.Fatal("got empty logger")
}
if ec.GlobalConfigDir == "" {
t.Fatalf("global config dir: expected $HOME/%s, got %s", cli.GLOBAL_CONFIG_DIR_NAME, ec.GlobalConfigDir)
}
if ec.GlobalConfigFile == "" {
t.Fatalf("global config file: expected $HOME/%s/%s, got %s", cli.GLOBAL_CONFIG_DIR_NAME, cli.GLOBAL_CONFIG_FILE_NAME, ec.GlobalConfigFile)
}
if ec.Config == nil {
t.Fatal("nil HasuraGraphQLConfig")
}
}

func TestValidate(t *testing.T) {
ec := &cli.ExecutionContext{}
ec.ExecutionDirectory = filepath.Join(os.TempDir(), "hasura-gql-tests-"+strconv.Itoa(rand.Intn(1000)))
ec.Viper = viper.New()

// validate a directory created by init
initCmd := commands.NewInitCmd(ec)
initCmd.Flags().Set("directory", ec.ExecutionDirectory)
err := initCmd.Execute()
if err != nil {
t.Fatalf("execution failed: %v", err)
}
err = ec.Validate()
if err != nil {
t.Fatalf("validate failed: %v", err)
}

// remove config.yaml and validate, should result in an error
err = os.Remove(filepath.Join(ec.ExecutionDirectory, "config.yaml"))
if err != nil {
t.Fatalf("remove failed: %v", err)
}
err = ec.Validate()
if err == nil {
t.Fatal("validate succeeded with no config.yaml")
}

os.RemoveAll(ec.ExecutionDirectory)
}
3 changes: 0 additions & 3 deletions cli/commands/commands.go

This file was deleted.

20 changes: 13 additions & 7 deletions cli/commands/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"path/filepath"
"runtime"
"sync"
"time"

"github.com/fatih/color"
"github.com/gin-contrib/cors"
Expand Down Expand Up @@ -49,6 +48,7 @@ func NewConsoleCmd(ec *cli.ExecutionContext) *cobra.Command {
f.StringVar(&opts.APIPort, "api-port", "9693", "port for serving migrate api")
f.StringVar(&opts.ConsolePort, "console-port", "9695", "port for serving console")
f.StringVar(&opts.Address, "address", "localhost", "address to use")
f.BoolVar(&opts.DontOpenBrowser, "no-browser", false, "do not automatically open console in browser")

f.String("endpoint", "", "http(s) endpoint for Hasura GraphQL Engine")
f.String("access-key", "", "access key for Hasura GraphQL Engine")
Expand All @@ -65,6 +65,10 @@ type consoleOptions struct {
APIPort string
ConsolePort string
Address string

DontOpenBrowser bool

WG *sync.WaitGroup
}

func (o *consoleOptions) Run() error {
Expand Down Expand Up @@ -110,6 +114,7 @@ func (o *consoleOptions) Run() error {

// Create WaitGroup for running 3 servers
wg := &sync.WaitGroup{}
o.WG = wg
wg.Add(1)
go func() {
err = router.Run(o.Address + ":" + o.APIPort)
Expand All @@ -129,15 +134,16 @@ func (o *consoleOptions) Run() error {

consoleURL := fmt.Sprintf("http://%s:%s", o.Address, o.ConsolePort)

o.EC.Spin(color.CyanString("Opening console using default browser..."))
defer o.EC.Spinner.Stop()
if !o.DontOpenBrowser {
o.EC.Spin(color.CyanString("Opening console using default browser..."))
defer o.EC.Spinner.Stop()

err = open.Run(consoleURL)
if err != nil {
o.EC.Logger.WithError(err).Warn("Error opening browser, try to open the url manually?")
err = open.Run(consoleURL)
if err != nil {
o.EC.Logger.WithError(err).Warn("Error opening browser, try to open the url manually?")
}
}

time.Sleep(2 * time.Second)
o.EC.Spinner.Stop()
log.Infof("console running at: %s", consoleURL)

Expand Down
41 changes: 41 additions & 0 deletions cli/commands/console_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package commands

import (
"testing"
"time"

"github.com/briandowns/spinner"
"github.com/hasura/graphql-engine/cli"
"github.com/sirupsen/logrus"
)

func TestConsoleCmd(t *testing.T) {
opts := &consoleOptions{
EC: &cli.ExecutionContext{
Logger: logrus.New(),
Spinner: spinner.New(spinner.CharSets[7], 100*time.Millisecond),
Config: &cli.HasuraGraphQLConfig{
Endpoint: "http://localhost:8080",
AccessKey: "",
},
},
APIPort: "9693",
ConsolePort: "9695",
Address: "localhost",
DontOpenBrowser: true,
}

go func() {
t.Log("waiting for console to start")
for opts.WG == nil {
time.Sleep(1 * time.Second)
}
opts.WG.Done()
opts.WG.Done()
}()
err := opts.Run()
if err != nil {
t.Fatalf("failed running console: %v", err)
}
// TODO: (shahidhk) curl the console endpoint for 200 response
}
Loading