From 5356455904d2fcb04c03a8d2f5be9d286395f7e5 Mon Sep 17 00:00:00 2001 From: Vikram Rangnekar Date: Fri, 17 Apr 2020 10:56:26 -0400 Subject: [PATCH] Fix issue with relative paths and config files --- config/dev.yml | 2 +- config/prod.yml | 2 +- core/init.go | 1 + docs/guide/guide.md | 10 +++++++++- go.mod | 1 + go.sum | 4 ++++ internal/serv/cmd_migrate.go | 9 +++++---- internal/serv/cmd_seed.go | 14 ++++++++++++-- internal/serv/config.go | 9 +++++++++ internal/serv/init.go | 4 ++-- internal/serv/tmpl/prod.yml | 2 +- 11 files changed, 46 insertions(+), 12 deletions(-) diff --git a/config/dev.yml b/config/dev.yml index d5ef922..3376434 100644 --- a/config/dev.yml +++ b/config/dev.yml @@ -30,7 +30,7 @@ reload_on_config_change: true # seed_file: seed.js # Path pointing to where the migrations can be found -migrations_path: ./config/migrations +migrations_path: ./migrations # Secret key for general encryption operations like # encrypting the cursor data diff --git a/config/prod.yml b/config/prod.yml index ac54027..b3d2564 100644 --- a/config/prod.yml +++ b/config/prod.yml @@ -30,7 +30,7 @@ enable_tracing: true # seed_file: seed.js # Path pointing to where the migrations can be found -# migrations_path: migrations +# migrations_path: ./migrations # Secret key for general encryption operations like # encrypting the cursor data diff --git a/core/init.go b/core/init.go index aefe8ea..b3aa8d5 100644 --- a/core/init.go +++ b/core/init.go @@ -108,6 +108,7 @@ func addTables(c *Config, di *psql.DBInfo) error { if err := addTable(di, t.Columns, t); err != nil { return err } + } return nil } diff --git a/docs/guide/guide.md b/docs/guide/guide.md index 0031025..775aa66 100644 --- a/docs/guide/guide.md +++ b/docs/guide/guide.md @@ -465,6 +465,14 @@ shuffle_strings numerify ``` +Other utility functions + +``` +shuffle_strings(string_array) +make_slug(text) +make_slug_lang(text, lang) +``` + ### Migrations Easy database migrations is the most important thing when building products backend by a relational database. We make it super easy to manage and migrate your database. @@ -1701,7 +1709,7 @@ reload_on_config_change: true # seed_file: seed.js # Path pointing to where the migrations can be found -migrations_path: ./config/migrations +migrations_path: ./migrations # Postgres related environment Variables # SG_DATABASE_HOST diff --git a/go.mod b/go.mod index fc0cc83..61089ab 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( github.com/garyburd/redigo v1.6.0 github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect github.com/gobuffalo/flect v0.2.1 + github.com/gosimple/slug v1.9.0 github.com/jackc/pgtype v1.3.0 github.com/jackc/pgx/v4 v4.6.0 github.com/mitchellh/mapstructure v1.2.2 // indirect diff --git a/go.sum b/go.sum index efa99ab..8a94a0d 100644 --- a/go.sum +++ b/go.sum @@ -83,6 +83,8 @@ github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGa github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= +github.com/gosimple/slug v1.9.0 h1:r5vDcYrFz9BmfIAMC829un9hq7hKM4cHUrsv36LbEqs= +github.com/gosimple/slug v1.9.0/go.mod h1:AMZ+sOVe65uByN3kgEyf9WEBKBCSS+dJjMX9x4vDJbg= github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= @@ -199,6 +201,8 @@ github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y8 github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= +github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be h1:ta7tUOvsPHVHGom5hKW5VXNc2xZIkfCKP8iaqOyYtUQ= +github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be/go.mod h1:MIDFMn7db1kT65GmV94GzpX9Qdi7N/pQlwb+AN8wh+Q= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik= diff --git a/internal/serv/cmd_migrate.go b/internal/serv/cmd_migrate.go index 95b7fca..e50a17e 100644 --- a/internal/serv/cmd_migrate.go +++ b/internal/serv/cmd_migrate.go @@ -98,8 +98,9 @@ func cmdDBNew(cmd *cobra.Command, args []string) { initConfOnce() name := args[0] + migrationsPath := conf.relPath(conf.MigrationsPath) - m, err := migrate.FindMigrations(conf.MigrationsPath) + m, err := migrate.FindMigrations(migrationsPath) if err != nil { log.Fatalf("ERR error loading migrations: %s", err) } @@ -107,7 +108,7 @@ func cmdDBNew(cmd *cobra.Command, args []string) { mname := fmt.Sprintf("%d_%s.sql", len(m), name) // Write new migration - mpath := filepath.Join(conf.MigrationsPath, mname) + mpath := filepath.Join(migrationsPath, mname) mfile, err := os.OpenFile(mpath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0666) if err != nil { log.Fatalf("ERR %s", err) @@ -144,7 +145,7 @@ func cmdDBMigrate(cmd *cobra.Command, args []string) { m.Data = getMigrationVars() - err = m.LoadMigrations(path.Join(conf.cpath, conf.MigrationsPath)) + err = m.LoadMigrations(conf.relPath(conf.MigrationsPath)) if err != nil { log.Fatalf("ERR failed to load migrations: %s", err) } @@ -236,7 +237,7 @@ func cmdDBStatus(cmd *cobra.Command, args []string) { m.Data = getMigrationVars() - err = m.LoadMigrations(conf.MigrationsPath) + err = m.LoadMigrations(conf.relPath(conf.MigrationsPath)) if err != nil { log.Fatalf("ERR failed to load migrations: %s", err) } diff --git a/internal/serv/cmd_seed.go b/internal/serv/cmd_seed.go index 564a4b4..c5a8eff 100644 --- a/internal/serv/cmd_seed.go +++ b/internal/serv/cmd_seed.go @@ -16,6 +16,7 @@ import ( "github.com/brianvoe/gofakeit/v5" "github.com/dop251/goja" "github.com/dosco/super-graph/core" + "github.com/gosimple/slug" "github.com/spf13/cobra" ) @@ -61,6 +62,10 @@ func cmdDBSeed(cmd *cobra.Command, args []string) { setFakeFuncs(fake) vm.Set("fake", fake) + util := vm.NewObject() + setUtilFuncs(util) + vm.Set("util", util) + _, err = vm.RunScript("seed.js", string(b)) if err != nil { log.Fatalf("ERR failed to execute script: %s", err) @@ -405,9 +410,14 @@ func setFakeFuncs(f *goja.Object) { f.Set("letter", gofakeit.Letter) f.Set("lexify", gofakeit.Lexify) f.Set("rand_string", getRandValue) - f.Set("shuffle_strings", gofakeit.ShuffleStrings) f.Set("numerify", gofakeit.Numerify) //f.Set("programming_language", gofakeit.ProgrammingLanguage) - +} + +func setUtilFuncs(f *goja.Object) { + // Slugs + f.Set("make_slug", slug.Make) + f.Set("make_slug_lang", slug.MakeLang) + f.Set("shuffle_strings", gofakeit.ShuffleStrings) } diff --git a/internal/serv/config.go b/internal/serv/config.go index 1dd1206..7af5cc5 100644 --- a/internal/serv/config.go +++ b/internal/serv/config.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "path" + "path/filepath" "strings" "github.com/spf13/viper" @@ -113,3 +114,11 @@ func GetConfigName() string { return ge } + +func (c *Config) relPath(p string) string { + if filepath.IsAbs(p) { + return p + } + + return path.Join(c.cpath, p) +} diff --git a/internal/serv/init.go b/internal/serv/init.go index 48cb687..6a0cfac 100644 --- a/internal/serv/init.go +++ b/internal/serv/init.go @@ -128,7 +128,7 @@ func initDB(c *Config, useDB bool) (*sql.DB, error) { if strings.Contains(c.DB.ServerCert, PEM_SIG) { pem = []byte(c.DB.ServerCert) } else { - pem, err = ioutil.ReadFile(c.DB.ServerCert) + pem, err = ioutil.ReadFile(c.relPath(c.DB.ServerCert)) } if err != nil { @@ -145,7 +145,7 @@ func initDB(c *Config, useDB bool) (*sql.DB, error) { if strings.Contains(c.DB.ClientCert, PEM_SIG) { certs, err = tls.X509KeyPair([]byte(c.DB.ClientCert), []byte(c.DB.ClientKey)) } else { - certs, err = tls.LoadX509KeyPair(c.DB.ClientCert, c.DB.ClientKey) + certs, err = tls.LoadX509KeyPair(c.relPath(c.DB.ClientCert), c.relPath(c.DB.ClientKey)) } if err != nil { diff --git a/internal/serv/tmpl/prod.yml b/internal/serv/tmpl/prod.yml index b70a363..a40fa57 100644 --- a/internal/serv/tmpl/prod.yml +++ b/internal/serv/tmpl/prod.yml @@ -34,7 +34,7 @@ reload_on_config_change: false # seed_file: seed.js # Path pointing to where the migrations can be found -# migrations_path: migrations +# migrations_path: ./migrations # Secret key for general encryption operations like # encrypting the cursor data