Skip to content
This repository has been archived by the owner on Jul 11, 2022. It is now read-only.

Commit

Permalink
remove unsafe database characters for db add using slugs
Browse files Browse the repository at this point in the history
Signed-off-by: Jason McCallister <jason@craftcms.com>
  • Loading branch information
jasonmccallister committed Oct 5, 2020
1 parent 769ff59 commit f34066f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

## Added
- Added the `--silent` flag to `xon`, `xoff`, and all `php iniset <command>`'s to not show output.
- Added the ability to import `zip` and `gz` files for `db import`. If the backup file is not compressed Nitro will now detect the database backup type and automatically select compatible databases. ([#132](https://github.com/craftcms/nitro/issues/132))

## Fixed
- An issue causing `php iniset memory_limit` to not show as an available command.
- An issue where `php iniset` commands were not the setting correct values. ([#207](https://github.com/craftcms/nitro/issues/207))
- An issue where `php iniget` would not return the correct values.
- An issue where the hosts file would not get updated on Linux machines. ([#213](https://github.com/craftcms/nitro/issues/213))

### 1.0.1 - 2020-08-12

Expand Down
14 changes: 8 additions & 6 deletions internal/cmd/db_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (

"github.com/pixelandtonic/prompt"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/craftcms/nitro/internal/config"
"github.com/craftcms/nitro/internal/scripts"
"github.com/craftcms/nitro/internal/slug"
"github.com/craftcms/nitro/internal/validate"
)

Expand All @@ -24,12 +24,9 @@ var dbAddCommand = &cobra.Command{
if err != nil {
return err
}
p := prompt.NewPrompt()

script := scripts.New(mp, machine)

var cfg config.Config
if err := viper.Unmarshal(&cfg); err != nil {
cfg, err := config.Read()
if err != nil {
return err
}

Expand All @@ -43,6 +40,8 @@ var dbAddCommand = &cobra.Command{
containers = append(containers, db.Name())
}

p := prompt.NewPrompt()

// if there is only one
var container string
switch len(containers) {
Expand All @@ -63,6 +62,9 @@ var dbAddCommand = &cobra.Command{
return err
}

// clean the database name
database = slug.Generate(database)

// run the scripts
if strings.Contains(container, "mysql") {
_, err = script.Run(false, fmt.Sprintf(scripts.FmtDockerMysqlCreateDatabaseIfNotExists, container, database))
Expand Down
18 changes: 18 additions & 0 deletions internal/slug/slug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package slug

import "strings"

// Generate takes a string and will trim, remove spaces, and remove special characters
func Generate(s string) string {
sl := s

// trim the string
sl = strings.TrimSpace(sl)

// remove spaces
if strings.Contains(sl, " ") {
sl = strings.ReplaceAll(sl, " ", "_")
}

return sl
}
27 changes: 27 additions & 0 deletions internal/slug/slug_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package slug

import "testing"

func TestGenerate(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
want string
}{
{
name: "removes spaces from strings",
args: args{s: "this database"},
want: "this_database",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Generate(tt.args.s); got != tt.want {
t.Errorf("Generate() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit f34066f

Please # to comment.