Fix issue with relative paths and config files

This commit is contained in:
Vikram Rangnekar
2020-04-17 10:56:26 -04:00
parent 074aded5c0
commit 5356455904
11 changed files with 46 additions and 12 deletions

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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 {

View File

@ -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