Skip to content

Commit 1bff452

Browse files
arvi3411301shahidhk
authored andcommitted
handle server endpoints that contains a url path (#64)
1 parent 330bbbe commit 1bff452

32 files changed

+412
-325
lines changed

cli/cli.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package cli
99

1010
import (
1111
"bytes"
12+
"net/url"
1213
"os"
1314
"path/filepath"
1415
"regexp"
@@ -50,6 +51,18 @@ type HasuraGraphQLConfig struct {
5051
Endpoint string `json:"endpoint"`
5152
// AccessKey (optional) required to query the endpoint
5253
AccessKey string `json:"access_key,omitempty"`
54+
55+
ParsedEndpoint *url.URL
56+
}
57+
58+
// ParseEndpoint ensures the endpoint is valid.
59+
func (hgc *HasuraGraphQLConfig) ParseEndpoint() error {
60+
nurl, err := url.Parse(hgc.Endpoint)
61+
if err != nil {
62+
return err
63+
}
64+
hgc.ParsedEndpoint = nurl
65+
return nil
5366
}
5467

5568
// ExecutionContext contains various contextual information required by the cli
@@ -204,15 +217,14 @@ func (ec *ExecutionContext) readConfig() error {
204217
v.AddConfigPath(ec.ExecutionDirectory)
205218
err := v.ReadInConfig()
206219
if err != nil {
207-
ec.Logger.WithError(err).Error("cannot read config file")
208220
return errors.Wrap(err, "cannor read config file")
209221
}
210-
211222
ec.Config = &HasuraGraphQLConfig{
212223
Endpoint: v.GetString("endpoint"),
213224
AccessKey: v.GetString("access_key"),
214225
}
215-
return nil
226+
err = ec.Config.ParseEndpoint()
227+
return err
216228
}
217229

218230
// setupSpinner creates a default spinner if the context does not already have

cli/commands/console.go

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"fmt"
55
"net/http"
66
"net/url"
7-
"path/filepath"
8-
"runtime"
97
"sync"
108

119
"github.com/fatih/color"
@@ -15,6 +13,7 @@ import (
1513
"github.com/hasura/graphql-engine/cli/migrate/api"
1614
"github.com/hasura/graphql-engine/cli/util"
1715
"github.com/pkg/errors"
16+
"github.com/sirupsen/logrus"
1817
"github.com/skratchdot/open-golang/open"
1918
"github.com/spf13/cobra"
2019
"github.com/spf13/viper"
@@ -86,12 +85,7 @@ func (o *consoleOptions) run() error {
8685
r,
8786
}
8887

89-
u, err := url.Parse(o.EC.Config.Endpoint)
90-
if err != nil {
91-
return errors.Wrap(err, "cannot parse endpoint as url")
92-
}
93-
94-
router.setRoutes(u.Host, o.EC.Config.AccessKey, o.EC.MigrationDir)
88+
router.setRoutes(o.EC.Config.ParsedEndpoint, o.EC.Config.AccessKey, o.EC.MigrationDir, o.EC.Logger)
9589

9690
if o.EC.Version == nil {
9791
return errors.New("cannot validate version, object is nil")
@@ -105,7 +99,7 @@ func (o *consoleOptions) run() error {
10599
"apiHost": "http://" + o.Address,
106100
"apiPort": o.APIPort,
107101
"cliVersion": o.EC.Version.GetCLIVersion(),
108-
"dataApiUrl": o.EC.Config.Endpoint,
102+
"dataApiUrl": o.EC.Config.ParsedEndpoint.String(),
109103
"dataApiVersion": "",
110104
"accessKey": o.EC.Config.AccessKey,
111105
"assetsVersion": consoleAssetsVersion,
@@ -158,14 +152,15 @@ type consoleRouter struct {
158152
*gin.Engine
159153
}
160154

161-
func (router *consoleRouter) setRoutes(host, accessKey, migrationDir string) {
155+
func (router *consoleRouter) setRoutes(nurl *url.URL, accessKey, migrationDir string, logger *logrus.Logger) {
162156
apis := router.Group("/apis")
163157
{
158+
apis.Use(setLogger(logger))
159+
apis.Use(setFilePath(migrationDir))
160+
apis.Use(setDataPath(nurl, accessKey))
164161
// Migrate api endpoints and middleware
165162
migrateAPIs := apis.Group("/migrate")
166163
{
167-
migrateAPIs.Use(setFilePath(migrationDir))
168-
migrateAPIs.Use(setDataPath(host, accessKey))
169164
settingsAPIs := migrateAPIs.Group("/settings")
170165
{
171166
settingsAPIs.Any("", api.SettingsAPI)
@@ -175,32 +170,31 @@ func (router *consoleRouter) setRoutes(host, accessKey, migrationDir string) {
175170
// Migrate api endpoints and middleware
176171
metadataAPIs := apis.Group("/metadata")
177172
{
178-
metadataAPIs.Use(setFilePath(migrationDir))
179-
metadataAPIs.Use(setDataPath(host, accessKey))
180173
metadataAPIs.Any("", api.MetadataAPI)
181174
}
182175
}
183176
}
184177

185-
func setDataPath(hostName, accessKey string) gin.HandlerFunc {
178+
func setDataPath(nurl *url.URL, accessKey string) gin.HandlerFunc {
186179
return func(c *gin.Context) {
187-
host := url.URL{
188-
Scheme: "hasuradb",
189-
User: url.UserPassword("admin", accessKey),
190-
Host: hostName,
191-
}
180+
host := getDataPath(nurl, accessKey)
181+
192182
c.Set("dbpath", host)
193183
c.Next()
194184
}
195185
}
196186

197187
func setFilePath(dir string) gin.HandlerFunc {
198188
return func(c *gin.Context) {
199-
if runtime.GOOS == "windows" {
200-
c.Set("filedir", "file:///"+filepath.Clean(dir))
201-
} else {
202-
c.Set("filedir", "file://"+filepath.Clean(dir))
203-
}
189+
host := getFilePath(dir)
190+
c.Set("filedir", host)
191+
c.Next()
192+
}
193+
}
194+
195+
func setLogger(logger *logrus.Logger) gin.HandlerFunc {
196+
return func(c *gin.Context) {
197+
c.Set("logger", logger)
204198
c.Next()
205199
}
206200
}

cli/commands/console_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ func TestConsoleCmd(t *testing.T) {
2929
DontOpenBrowser: true,
3030
}
3131
opts.EC.Spinner.Writer = &fake.FakeWriter{}
32+
err := opts.EC.Config.ParseEndpoint()
33+
if err != nil {
34+
t.Fatal(err)
35+
}
3236

3337
go func() {
3438
t.Log("waiting for console to start")
@@ -38,7 +42,7 @@ func TestConsoleCmd(t *testing.T) {
3842
opts.WG.Done()
3943
opts.WG.Done()
4044
}()
41-
err := opts.run()
45+
err = opts.run()
4246
if err != nil {
4347
t.Fatalf("failed running console: %v", err)
4448
}

cli/commands/metadata.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
package commands
22

33
import (
4+
"encoding/json"
5+
"io/ioutil"
6+
"path/filepath"
7+
8+
"github.com/ghodss/yaml"
49
"github.com/hasura/graphql-engine/cli"
10+
"github.com/hasura/graphql-engine/cli/migrate"
11+
"github.com/pkg/errors"
512
"github.com/spf13/cobra"
613
"github.com/spf13/viper"
714
)
@@ -31,3 +38,50 @@ func NewMetadataCmd(ec *cli.ExecutionContext) *cobra.Command {
3138
v.BindPFlag("access_key", f.Lookup("access-key"))
3239
return metadataCmd
3340
}
41+
42+
func executeMetadata(cmd string, t *migrate.Migrate, metadata string) error {
43+
switch cmd {
44+
case "export":
45+
metaData, err := t.ExportMetadata()
46+
if err != nil {
47+
return errors.Wrap(err, "Cannot export metadata")
48+
}
49+
50+
t, err := json.Marshal(metaData)
51+
if err != nil {
52+
return errors.Wrap(err, "Cannot Marshal metadata")
53+
}
54+
55+
data, err := yaml.JSONToYAML(t)
56+
if err != nil {
57+
return err
58+
}
59+
60+
err = ioutil.WriteFile(filepath.Join(metadata, "metadata.yaml"), data, 0644)
61+
if err != nil {
62+
return errors.Wrap(err, "cannot save metadata")
63+
}
64+
case "reset":
65+
err := t.ResetMetadata()
66+
if err != nil {
67+
return errors.Wrap(err, "Cannot reset Metadata")
68+
}
69+
case "apply":
70+
data, err := ioutil.ReadFile(filepath.Join(metadata, "metadata.yaml"))
71+
if err != nil {
72+
return errors.Wrap(err, "cannot read metadata file")
73+
}
74+
75+
var q interface{}
76+
err = yaml.Unmarshal(data, &q)
77+
if err != nil {
78+
return errors.Wrap(err, "cannot parse metadata file")
79+
}
80+
81+
err = t.ApplyMetadata(q)
82+
if err != nil {
83+
return errors.Wrap(err, "cannot apply metadata on the database")
84+
}
85+
}
86+
return nil
87+
}

cli/commands/metadata_apply.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
package commands
22

33
import (
4-
"net/url"
5-
64
"github.com/hasura/graphql-engine/cli"
7-
"github.com/hasura/graphql-engine/cli/util"
8-
"github.com/pkg/errors"
95
"github.com/spf13/cobra"
106
)
117

@@ -36,12 +32,9 @@ type metadataApplyOptions struct {
3632
}
3733

3834
func (o *metadataApplyOptions) run() error {
39-
dbURL, err := url.Parse(o.EC.Config.Endpoint)
35+
migrateDrv, err := newMigrate(o.EC.MigrationDir, o.EC.Config.ParsedEndpoint, o.EC.Config.AccessKey, o.EC.Logger)
4036
if err != nil {
41-
return errors.Wrap(err, "error parsing Endpoint")
37+
return err
4238
}
43-
44-
dbURL.Scheme = "hasuradb"
45-
dbURL.User = url.UserPassword("admin", o.EC.Config.AccessKey)
46-
return util.ExecuteMetadata(o.actionType, "file://"+o.EC.MigrationDir, dbURL.String(), o.EC.ExecutionDirectory)
39+
return executeMetadata(o.actionType, migrateDrv, o.EC.ExecutionDirectory)
4740
}

cli/commands/metadata_apply_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package commands
22

33
import (
4+
"net/url"
45
"testing"
56
"time"
67

@@ -9,16 +10,17 @@ import (
910
"github.com/sirupsen/logrus/hooks/test"
1011
)
1112

12-
func testMetadataApply(t *testing.T, executionDir string, endpoint string) {
13+
func testMetadataApply(t *testing.T, executionDir string, endpoint *url.URL) {
1314
logger, _ := test.NewNullLogger()
1415
opts := &metadataApplyOptions{
1516
EC: &cli.ExecutionContext{
1617
Logger: logger,
1718
Spinner: spinner.New(spinner.CharSets[7], 100*time.Millisecond),
1819
ExecutionDirectory: executionDir,
1920
Config: &cli.HasuraGraphQLConfig{
20-
Endpoint: endpoint,
21-
AccessKey: "",
21+
Endpoint: endpoint.String(),
22+
AccessKey: "",
23+
ParsedEndpoint: endpoint,
2224
},
2325
},
2426
actionType: "apply",

cli/commands/metadata_export.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
package commands
22

33
import (
4-
"net/url"
5-
64
"github.com/hasura/graphql-engine/cli"
7-
"github.com/hasura/graphql-engine/cli/util"
8-
"github.com/pkg/errors"
95
"github.com/spf13/cobra"
106
)
117

@@ -36,12 +32,9 @@ type metadataExportOptions struct {
3632
}
3733

3834
func (o *metadataExportOptions) run() error {
39-
dbURL, err := url.Parse(o.EC.Config.Endpoint)
35+
migrateDrv, err := newMigrate(o.EC.MigrationDir, o.EC.Config.ParsedEndpoint, o.EC.Config.AccessKey, o.EC.Logger)
4036
if err != nil {
41-
return errors.Wrap(err, "error parsing Endpoint")
37+
return err
4238
}
43-
44-
dbURL.Scheme = "hasuradb"
45-
dbURL.User = url.UserPassword("admin", o.EC.Config.AccessKey)
46-
return util.ExecuteMetadata(o.actionType, "file://"+o.EC.MigrationDir, dbURL.String(), o.EC.ExecutionDirectory)
39+
return executeMetadata(o.actionType, migrateDrv, o.EC.ExecutionDirectory)
4740
}

cli/commands/metadata_export_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package commands
22

33
import (
4+
"net/url"
45
"testing"
56
"time"
67

@@ -9,16 +10,17 @@ import (
910
"github.com/sirupsen/logrus/hooks/test"
1011
)
1112

12-
func testMetadataExport(t *testing.T, executionDir string, endpoint string) {
13+
func testMetadataExport(t *testing.T, executionDir string, endpoint *url.URL) {
1314
logger, _ := test.NewNullLogger()
1415
opts := &metadataExportOptions{
1516
EC: &cli.ExecutionContext{
1617
Logger: logger,
1718
Spinner: spinner.New(spinner.CharSets[7], 100*time.Millisecond),
1819
ExecutionDirectory: executionDir,
1920
Config: &cli.HasuraGraphQLConfig{
20-
Endpoint: endpoint,
21-
AccessKey: "",
21+
Endpoint: endpoint.String(),
22+
AccessKey: "",
23+
ParsedEndpoint: endpoint,
2224
},
2325
},
2426
actionType: "export",

cli/commands/metadata_reset.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package commands
22

33
import (
4-
"net/url"
5-
64
"github.com/hasura/graphql-engine/cli"
7-
"github.com/hasura/graphql-engine/cli/util"
85
"github.com/pkg/errors"
96
"github.com/spf13/cobra"
107
)
@@ -36,14 +33,11 @@ type metadataResetOptions struct {
3633
}
3734

3835
func (o *metadataResetOptions) run() error {
39-
dbURL, err := url.Parse(o.EC.Config.Endpoint)
36+
migrateDrv, err := newMigrate(o.EC.MigrationDir, o.EC.Config.ParsedEndpoint, o.EC.Config.AccessKey, o.EC.Logger)
4037
if err != nil {
41-
return errors.Wrap(err, "error parsing Endpoint")
38+
return err
4239
}
43-
44-
dbURL.Scheme = "hasuradb"
45-
dbURL.User = url.UserPassword("admin", o.EC.Config.AccessKey)
46-
err = util.ExecuteMetadata(o.actionType, "file://"+o.EC.MigrationDir, dbURL.String(), o.EC.ExecutionDirectory)
40+
err = executeMetadata(o.actionType, migrateDrv, o.EC.ExecutionDirectory)
4741
if err != nil {
4842
return errors.Wrap(err, "Cannot reset metadata")
4943
}

0 commit comments

Comments
 (0)