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

Commit

Permalink
dont hard code mount dest, resolves #86
Browse files Browse the repository at this point in the history
Signed-off-by: Jason McCallister <jason@craftcms.com>
  • Loading branch information
jasonmccallister committed Jun 2, 2020
1 parent 88e7498 commit 24e275d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
12 changes: 6 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ func (c *Config) GetSites() []Site {
// 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 {
func (c *Config) AlreadyMounted(mount Mount) (bool, Mount) {
// get the home directory
home, err := homedir.Dir()
if err != nil {
return false
return false, Mount{}
}

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

// check each of the mounts in the config
Expand All @@ -60,16 +60,16 @@ func (c *Config) AlreadyMounted(mount Mount) bool {

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

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

return false
return false, Mount{}
}

// GetExpandedMounts will take all of the mounts in a config file
Expand Down
25 changes: 20 additions & 5 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -836,10 +836,11 @@ func TestConfig_AlreadyMounted(t *testing.T) {
m Mount
}
tests := []struct {
name string
fields fields
args args
want bool
name string
fields fields
args args
want bool
wantMount Mount
}{
{
name: "mounts that have a parent path return true",
Expand All @@ -856,6 +857,10 @@ func TestConfig_AlreadyMounted(t *testing.T) {
Dest: "/home/ubuntu/sites/example",
}},
want: true,
wantMount: Mount{
Source: current,
Dest: "/home/ubuntu/sites/example",
},
},
{
name: "mounts that exists return true",
Expand All @@ -872,6 +877,10 @@ func TestConfig_AlreadyMounted(t *testing.T) {
Dest: "/home/ubuntu/sites/example",
}},
want: true,
wantMount: Mount{
Source: current,
Dest: "/home/ubuntu/sites/example",
},
},
{
name: "mounts that do not exist return false",
Expand All @@ -891,9 +900,15 @@ func TestConfig_AlreadyMounted(t *testing.T) {
Databases: tt.fields.Databases,
Sites: tt.fields.Sites,
}
if got := c.AlreadyMounted(tt.args.m); got != tt.want {
got, m := c.AlreadyMounted(tt.args.m)

if got != tt.want {
t.Errorf("AlreadyMounted() = \n%v, \nwant \n%v", got, tt.want)
}

if m != tt.wantMount {
t.Errorf("AlreadyMounted() = \n%v, \nwant \n%v", m, tt.wantMount)
}
})
}
}
11 changes: 6 additions & 5 deletions internal/cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,15 @@ var addCommand = &cobra.Command{
}

webRootPath := fmt.Sprintf("/home/ubuntu/sites/%s/%s", directoryName, webrootDir)

// create a new mount
skipMount := true
mount := config.Mount{Source: absolutePath}
if configFile.AlreadyMounted(mount) {
// TODO get the mount dest
// get mount by source
fmt.Println(mount.Source, "is already mounted at", mount.Dest, ". Using that instead of creating a new mount.")
exists, foundMount := configFile.AlreadyMounted(mount)
if exists {
fmt.Println(mount.Source, "is already mounted at", foundMount.Dest, ". Using existing instead of creating new mount.")

webRootPath = foundMount.Dest + "/" + directoryName + "/" + webrootDir
fmt.Println("Setting webroot to", webRootPath)
} else {
mount.Dest = "/home/ubuntu/sites/" + directoryName
// add the mount to configfile
Expand Down
6 changes: 4 additions & 2 deletions internal/task/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ func Apply(machine string, configFile config.Config, mounts []config.Mount, site

// check if there are mounts we need to remove
for _, mount := range inMemoryConfig.Mounts {
if !configFile.AlreadyMounted(mount) {
exists, _ := configFile.AlreadyMounted(mount)
if !exists {
unmountAction, err := nitro.UnmountDir(machine, mount.Dest)
if err != nil {
return nil, err
Expand All @@ -34,7 +35,8 @@ func Apply(machine string, configFile config.Config, mounts []config.Mount, site

// check if there are mounts we need to create
for _, mount := range configFile.Mounts {
if !inMemoryConfig.AlreadyMounted(mount) {
exists, _ := inMemoryConfig.AlreadyMounted(mount)
if !exists {
mountAction, err := nitro.MountDir(machine, mount.AbsSourcePath(), mount.Dest)
if err != nil {
return nil, err
Expand Down

0 comments on commit 24e275d

Please # to comment.