Skip to content

Commit

Permalink
Merge pull request #759 from Scalingo/fix/387/db-console_env
Browse files Browse the repository at this point in the history
feat(db): environment variable name is customizable
  • Loading branch information
EtienneM authored Aug 5, 2022
2 parents e9a7c2b + 9c8e596 commit bfccc71
Show file tree
Hide file tree
Showing 13 changed files with 133 additions and 120 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### To be Released

* feat(db): environment variable name is customizable [#759](https://github.com/Scalingo/cli/issues/759)
* feat(goreleaser): use goreleaser to make releases using github action [#752](https://github.com/Scalingo/cli/issues/752)
* feat(make-release): use gox [#747](https://github.com/Scalingo/cli/pull/747)
* fix(install.sh): better error message if fails to get the version [#748](https://github.com/Scalingo/cli/pull/748)
Expand Down
14 changes: 7 additions & 7 deletions cmd/db_tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ var (
database.
Example
$ scalingo -a my-app db-tunnel SCALINGO_MONGO_URL
$ scalingo -a my-app db-tunnel mongodb://user:pass@host:port/db
$ scalingo --app my-app db-tunnel SCALINGO_MONGO_URL
$ scalingo --app my-app db-tunnel mongodb://user:pass@host:port/db
Once the tunnel is built, the port which has been allocated will be
displayed (default is 10000), example: "localhost:10000". You can
choose this port manually with the '-p' option.
Example
$ scalingo -a my-app db-tunnel -p 20000 MONGO_URL
$ scalingo --app my-app db-tunnel -p 20000 MONGO_URL
Building tunnel to my-app-1.mongo.dbs.scalingo.com:12345
You can access your database on '127.0.0.1:20000'
Expand All @@ -53,7 +53,7 @@ var (
you want to use to authenticate thanks to the '-i' flag.
Example
$ scalingo -a rails-app db-tunnel -i ~/.ssh/custom_key DATABASE_URL`,
$ scalingo --app rails-app db-tunnel -i ~/.ssh/custom_key DATABASE_URL`,
Action: func(c *cli.Context) {
currentApp := detect.CurrentApp(c)
var sshIdentity string
Expand All @@ -68,15 +68,15 @@ var (
cli.ShowCommandHelp(c, "db-tunnel")
return
}
opts := db.TunnelOpts{

err := db.Tunnel(db.TunnelOpts{
App: currentApp,
DBEnvVar: c.Args()[0],
Identity: sshIdentity,
Port: c.Int("port"),
Bind: c.String("bind"),
Reconnect: c.BoolT("reconnect"),
}
err := db.Tunnel(opts)
})
if err != nil {
errorQuit(err)
}
Expand Down
21 changes: 13 additions & 8 deletions cmd/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ var (
Usage: "Run an interactive console with your InfluxDB addon",
Flags: []cli.Flag{appFlag,
cli.StringFlag{Name: "size, s", Value: "", Usage: "Size of the container"},
cli.StringFlag{Name: "env, e", Value: "", Usage: "Environment variable name to use for the connection to the database"},
},
Description: ` Run an interactive console with your InfluxDB addon.
Examples
scalingo --app myapp influxdb-console
scalingo --app myapp influxdb-console --size L
scalingo --app my-app influxdb-console
scalingo --app my-app influxdb-console --size L
scalingo --app my-app influxdb-console --env MY_INFLUXDB_URL
The --size flag makes it easy to specify the size of the container executing
the InfluxDB console. Each container size has different price and performance.
Expand All @@ -30,14 +32,17 @@ var (
# See also 'mongo-console' and 'mysql-console'
`,
Action: func(c *cli.Context) {
currentApp := detect.CurrentApp(c)
opts := db.InfluxDBConsoleOpts{
App: currentApp,
Size: c.String("s"),
}
if len(c.Args()) != 0 {
cli.ShowCommandHelp(c, "influxdb-console")
} else if err := db.InfluxDBConsole(opts); err != nil {
return
}

err := db.InfluxDBConsole(db.InfluxDBConsoleOpts{
App: detect.CurrentApp(c),
Size: c.String("s"),
VariableName: c.String("e"),
})
if err != nil {
errorQuit(err)
}
},
Expand Down
21 changes: 13 additions & 8 deletions cmd/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ var (
Usage: "Run an interactive console with your MongoDB addon",
Flags: []cli.Flag{appFlag,
cli.StringFlag{Name: "size, s", Value: "", Usage: "Size of the container"},
cli.StringFlag{Name: "env, e", Value: "", Usage: "Environment variable name to use for the connection to the database"},
},
Description: ` Run an interactive console with your MongoDB addon.
Examples
scalingo --app myapp mongo-console
scalingo --app myapp mongo-console --size L
scalingo --app my-app mongo-console
scalingo --app my-app mongo-console --size L
scalingo --app my-app mongo-console --env MY_MONGO_URL
The --size flag makes it easy to specify the size of the container executing
the MongoDB console. Each container size has different price and performance.
Expand All @@ -30,14 +32,17 @@ var (
# See also 'redis-console' and 'mysql-console'
`,
Action: func(c *cli.Context) {
currentApp := detect.CurrentApp(c)
opts := db.MongoConsoleOpts{
App: currentApp,
Size: c.String("s"),
}
if len(c.Args()) != 0 {
cli.ShowCommandHelp(c, "mongo-console")
} else if err := db.MongoConsole(opts); err != nil {
return
}

err := db.MongoConsole(db.MongoConsoleOpts{
App: detect.CurrentApp(c),
Size: c.String("s"),
VariableName: c.String("e"),
})
if err != nil {
errorQuit(err)
}
},
Expand Down
21 changes: 13 additions & 8 deletions cmd/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ var (
Usage: "Run an interactive console with your MySQL addon",
Flags: []cli.Flag{appFlag,
cli.StringFlag{Name: "size, s", Value: "", Usage: "Size of the container"},
cli.StringFlag{Name: "env, e", Value: "", Usage: "Environment variable name to use for the connection to the database"},
},
Description: ` Run an interactive console with your MySQL addon.
Examples
scalingo --app myapp mysql-console
scalingo --app myapp mysql-console --size L
scalingo --app my-app mysql-console
scalingo --app my-app mysql-console --size L
scalingo --app my-app mysql-console --env MY_MYSQL_URL
The --size flag makes it easy to specify the size of the container executing
the MySQL console. Each container size has different price and performance.
Expand All @@ -30,14 +32,17 @@ var (
# See also 'mongo-console' and 'pgsql-console'
`,
Action: func(c *cli.Context) {
currentApp := detect.CurrentApp(c)
opts := db.MySQLConsoleOpts{
App: currentApp,
Size: c.String("s"),
}
if len(c.Args()) != 0 {
cli.ShowCommandHelp(c, "mysql-console")
} else if err := db.MySQLConsole(opts); err != nil {
return
}

err := db.MySQLConsole(db.MySQLConsoleOpts{
App: detect.CurrentApp(c),
Size: c.String("s"),
VariableName: c.String("e"),
})
if err != nil {
errorQuit(err)
}
},
Expand Down
21 changes: 13 additions & 8 deletions cmd/pgsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ var (
Usage: "Run an interactive console with your PostgreSQL addon",
Flags: []cli.Flag{appFlag,
cli.StringFlag{Name: "size, s", Value: "", Usage: "Size of the container"},
cli.StringFlag{Name: "env, e", Value: "", Usage: "Environment variable name to use for the connection to the database"},
},
Description: ` Run an interactive console with your PostgreSQL addon.
Examples
scalingo --app myapp pgsql-console
scalingo --app myapp pgsql-console --size L
scalingo --app my-app pgsql-console
scalingo --app my-app pgsql-console --size L
scalingo --app my-app pgsql-console --env MY_PSQL_URL
The --size flag makes it easy to specify the size of the container executing
the PostgreSQL console. Each container size has different price and performance.
Expand All @@ -31,14 +33,17 @@ var (
# See also 'mongo-console' and 'mysql-console'
`,
Action: func(c *cli.Context) {
currentApp := detect.CurrentApp(c)
opts := db.PgSQLConsoleOpts{
App: currentApp,
Size: c.String("s"),
}
if len(c.Args()) != 0 {
cli.ShowCommandHelp(c, "pgsql-console")
} else if err := db.PgSQLConsole(opts); err != nil {
return
}

err := db.PgSQLConsole(db.PgSQLConsoleOpts{
App: detect.CurrentApp(c),
Size: c.String("s"),
VariableName: c.String("e"),
})
if err != nil {
errorQuit(err)
}
},
Expand Down
21 changes: 13 additions & 8 deletions cmd/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ var (
Usage: "Run an interactive console with your Redis addon",
Flags: []cli.Flag{appFlag,
cli.StringFlag{Name: "size, s", Value: "", Usage: "Size of the container"},
cli.StringFlag{Name: "env, e", Value: "", Usage: "Environment variable name to use for the connection to the database"},
},
Description: ` Run an interactive console with your Redis addon.
Examples
scalingo --app myapp redis-console
scalingo --app myapp redis-console --size L
scalingo --app my-app redis-console
scalingo --app my-app redis-console --size L
scalingo --app my-app redis-console --env MY_REDIS_URL
The --size flag makes it easy to specify the size of the container executing
the Redis console. Each container size has different price and performance.
Expand All @@ -30,14 +32,17 @@ var (
# See also 'mongo-console' and 'mysql-console'
`,
Action: func(c *cli.Context) {
currentApp := detect.CurrentApp(c)
opts := db.RedisConsoleOpts{
App: currentApp,
Size: c.String("s"),
}
if len(c.Args()) != 0 {
cli.ShowCommandHelp(c, "redis-console")
} else if err := db.RedisConsole(opts); err != nil {
return
}

err := db.RedisConsole(db.RedisConsoleOpts{
App: detect.CurrentApp(c),
Size: c.String("s"),
VariableName: c.String("e"),
})
if err != nil {
errorQuit(err)
}
},
Expand Down
21 changes: 13 additions & 8 deletions db/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (

"gopkg.in/errgo.v1"

"github.com/Scalingo/go-scalingo/v4"
"github.com/Scalingo/cli/config"
)

func dbURL(c *scalingo.Client, app, envWord string, urlSchemes []string) (*url.URL, string, string, error) {
u, err := dbURLFromAPI(c, app, envWord, urlSchemes)
func dbURL(appName, envVariableName string, urlSchemes []string) (*url.URL, string, string, error) {
u, err := dbURLFromAPI(appName, envVariableName, urlSchemes)
if err != nil {
return nil, "", "", errgo.Mask(err)
}
Expand All @@ -28,20 +28,25 @@ func dbURL(c *scalingo.Client, app, envWord string, urlSchemes []string) (*url.U
return dbURL, user, password, nil
}

func dbURLFromAPI(c *scalingo.Client, app, envWord string, urlSchemes []string) (string, error) {
environ, err := c.VariablesListWithoutAlias(app)
func dbURLFromAPI(appName, envVariableName string, urlSchemes []string) (string, error) {
scalingoClient, err := config.ScalingoClient()
if err != nil {
return "", errgo.Notef(err, "fail to get Scalingo client to list the variables")
}

variables, err := scalingoClient.VariablesListWithoutAlias(appName)
if err != nil {
return "", errgo.Mask(err)
}
for _, variable := range environ {
for _, variable := range variables {
for _, scheme := range urlSchemes {
if strings.Contains(variable.Name, envWord) && strings.HasPrefix(variable.Value, scheme) {
if strings.Contains(variable.Name, envVariableName) && strings.HasPrefix(variable.Value, scheme+"://") {
return variable.Value, nil
}
}
}

return "", errgo.Newf("no %v addon detected", strings.ToLower(envWord))
return "", errgo.Newf("no %v addon detected", strings.ToLower(envVariableName))
}

func extractCredentials(u *url.URL) (string, string, error) {
Expand Down
16 changes: 7 additions & 9 deletions db/influxdb_console.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@ import (
"gopkg.in/errgo.v1"

"github.com/Scalingo/cli/apps"
"github.com/Scalingo/cli/config"
)

type InfluxDBConsoleOpts struct {
App string
Size string
App string
Size string
VariableName string
}

func InfluxDBConsole(opts InfluxDBConsoleOpts) error {
c, err := config.ScalingoClient()
if err != nil {
return errgo.Notef(err, "fail to get Scalingo client")
if opts.VariableName == "" {
opts.VariableName = "SCALINGO_INFLUX"
}

influxdbURL, username, password, err := dbURL(c, opts.App, "SCALINGO_INFLUX", []string{"http://", "https://"})
influxdbURL, username, password, err := dbURL(opts.App, opts.VariableName, []string{"http", "https"})
if err != nil {
return errgo.Mask(err)
}
Expand All @@ -48,7 +46,7 @@ func InfluxDBConsole(opts InfluxDBConsoleOpts) error {

err = apps.Run(runOpts)
if err != nil {
return errgo.Newf("Fail to run InfluxDB console: %v", err)
return errgo.Newf("fail to run InfluxDB console: %v", err)
}

return nil
Expand Down
28 changes: 11 additions & 17 deletions db/mongo_console.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
package db

import (
"gopkg.in/errgo.v1" // "mysql2://" for ruby driver 'mysql2'
"gopkg.in/errgo.v1"

"github.com/Scalingo/cli/apps"
"github.com/Scalingo/cli/config"
)

type MongoConsoleOpts struct {
App string
Size string
App string
Size string
VariableName string
}

func MongoConsole(opts MongoConsoleOpts) error {
c, err := config.ScalingoClient()
if err != nil {
return errgo.Notef(err, "fail to get Scalingo client")
if opts.VariableName == "" {
opts.VariableName = "SCALINGO_MONGO"
}

mongoURL, _, _, err := dbURL(c, opts.App, "SCALINGO_MONGO", []string{"mongodb://"})
mongoURL, _, _, err := dbURL(opts.App, opts.VariableName, []string{"mongodb"})
if err != nil {
return errgo.Mask(err)
}
Expand All @@ -28,18 +26,14 @@ func MongoConsole(opts MongoConsoleOpts) error {
command = append(command, "--ssl", "--sslAllowInvalidCertificates")
}

command = append(command, "'"+mongoURL.String()+"'")

runOpts := apps.RunOpts{
err = apps.Run(apps.RunOpts{
DisplayCmd: "mongo-console",
App: opts.App,
Cmd: command,
Cmd: append(command, "'"+mongoURL.String()+"'"),
Size: opts.Size,
}

err = apps.Run(runOpts)
})
if err != nil {
return errgo.Newf("Fail to run MongoDB console: %v", err)
return errgo.Newf("fail to run MongoDB console: %v", err)
}

return nil
Expand Down
Loading

0 comments on commit bfccc71

Please sign in to comment.