From f34066f2bf34845a03ec6e6c7efce915ea51abb0 Mon Sep 17 00:00:00 2001 From: Jason McCallister Date: Mon, 5 Oct 2020 09:45:51 -0400 Subject: [PATCH] remove unsafe database characters for db add using slugs Signed-off-by: Jason McCallister --- CHANGELOG.md | 2 ++ internal/cmd/db_add.go | 14 ++++++++------ internal/slug/slug.go | 18 ++++++++++++++++++ internal/slug/slug_test.go | 27 +++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 internal/slug/slug.go create mode 100644 internal/slug/slug_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 0751526c..3055890f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,11 +4,13 @@ ## Added - Added the `--silent` flag to `xon`, `xoff`, and all `php iniset `'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 diff --git a/internal/cmd/db_add.go b/internal/cmd/db_add.go index 512e84bb..1a70553b 100644 --- a/internal/cmd/db_add.go +++ b/internal/cmd/db_add.go @@ -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" ) @@ -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 } @@ -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) { @@ -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)) diff --git a/internal/slug/slug.go b/internal/slug/slug.go new file mode 100644 index 00000000..d0b84a33 --- /dev/null +++ b/internal/slug/slug.go @@ -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 +} diff --git a/internal/slug/slug_test.go b/internal/slug/slug_test.go new file mode 100644 index 00000000..4da18f90 --- /dev/null +++ b/internal/slug/slug_test.go @@ -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) + } + }) + } +}