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

Commit

Permalink
add AlreadyMounted to config
Browse files Browse the repository at this point in the history
Signed-off-by: Jason McCallister <jason@craftcms.com>
  • Loading branch information
jasonmccallister committed May 29, 2020
1 parent 1b80df0 commit bd5b9fe
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
39 changes: 39 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"gopkg.in/yaml.v2"

"github.com/craftcms/nitro/internal/helpers"
"github.com/craftcms/nitro/internal/resolve"
)

type Config struct {
Expand All @@ -33,6 +34,44 @@ func (c *Config) GetSites() []Site {
return c.Sites
}

// AlreadyMounted takes a new mount and will check if the
// mount source is already mounted to the virtual machine
// and will also check if the new mount is a parent mount
func (c *Config) AlreadyMounted(mount Mount) bool {
// get the home directory
home, err := homedir.Dir()
if err != nil {
return false
}

// get the local path of the mount
newLocal, err := resolve.AbsPath(mount.Source, home)
if err != nil {
return false
}

// check each of the mounts in the config
for _, m := range c.Mounts {
// get the abs path of the existing mount
existingLocal, err := resolve.AbsPath(m.Source, home)
if err != nil {
continue
}

// if it is an exact match
if existingLocal == newLocal {
return true
}

// if it is a sub folder of the mount
if strings.Contains(existingLocal, newLocal) {
return true
}
}

return false
}

// GetExpandedMounts will take all of the mounts in a config file
// and "expand" or get the full path mount source and return
// a slice of mounts
Expand Down
84 changes: 84 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package config

import (
"os"
"reflect"
"testing"

"github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
)

Expand Down Expand Up @@ -813,3 +815,85 @@ func TestConfig_FindMountBySiteWebroot(t *testing.T) {
})
}
}

func TestConfig_AlreadyMounted(t *testing.T) {
_, err := homedir.Dir()
if err != nil {
t.Fatal(err)
}
current, err := os.Getwd()
if err != nil {
t.Fatal(err)
}

type fields struct {
PHP string
Mounts []Mount
Databases []Database
Sites []Site
}
type args struct {
m Mount
}
tests := []struct {
name string
fields fields
args args
want bool
}{
{
name: "mounts that have a parent path return true",
fields: fields{
Mounts: []Mount{
{
Source: current + "/testdata/new-mount",
Dest: "/home/ubuntu/sites/example",
},
},
},
args: args{m: Mount{
Source: current,
Dest: "/home/ubuntu/sites/example",
}},
want: true,
},
{
name: "mounts that exists return true",
fields: fields{
Mounts: []Mount{
{
Source: current,
Dest: "/home/ubuntu/sites/example",
},
},
},
args: args{m: Mount{
Source: current,
Dest: "/home/ubuntu/sites/example",
}},
want: true,
},
{
name: "mounts that do not exist return false",
fields: fields{Mounts: nil},
args: args{m: Mount{
Source: current,
Dest: "/home/ubuntu/sites/example.site",
}},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Config{
PHP: tt.fields.PHP,
Mounts: tt.fields.Mounts,
Databases: tt.fields.Databases,
Sites: tt.fields.Sites,
}
if got := c.AlreadyMounted(tt.args.m); got != tt.want {
t.Errorf("AlreadyMounted() = \n%v, \nwant \n%v", got, tt.want)
}
})
}
}

0 comments on commit bd5b9fe

Please # to comment.