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

Commit

Permalink
Merge branch 'release/1.0.0-beta.10'
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmccallister committed Jun 4, 2020
2 parents b4def0e + d8bd6ff commit e3588e4
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 7 deletions.
6 changes: 6 additions & 0 deletions ADVANCED.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ You can customize Nitro’s MySQL settings by editing `mysql.conf` and restartin
2. Apply your changes to `~/.nitro/databases/mysql/conf.d/mysql.conf`.
3. Exit the machine tunnel by running `exit`.
4. Restart MySQL using `nitro db restart`.

## `NITRO_EDIT_HOSTS`

If you add a `NITRO_EDIT_HOSTS` environment variable to your system and set it to `false`, Nitro will never edit the `hosts` file on the host machine.

This is useful for people running some host file manager applications.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

### Unreleased

### 1.0.0-beta.10 - 2020-06-04

## Added
- Added support for a `NITRO_EDIT_HOSTS` environment variable so that when set to `false`, Nitro will never edit the host machine’s `hosts` file.

## Changed
- The `destroy` command now has a `--skip-hosts` option.

## 1.0.0-beta.9 - 2020-06-02

### Changed
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ sites:
Then run `nitro apply` to apply your `nitro.yaml` changes to the machine.
> 💡 **Tip:** To avoid permission issues, we recommend you always mount folders into `/nitro/*` within the
> 💡 **Tip:** To avoid permission issues, we recommend you always mount folders into `/home/ubuntu/*` within the
machine.
> ⚠️ **Warning:** If your projects contain any symlinks, such as `path` Composer repositories, those symlinks
Expand Down Expand Up @@ -336,7 +336,7 @@ Options:
<dl>
<dt><code>-m</code>, <code>--machine</code></dt>
<dd>The name of the machine to use. Defaults to <code>nitro-dev</code>.</dd>
<dt><code>-m</code>, <code>--skip-hosts</code></dt>
<dt><code>--skip-hosts</code></dt>
<dd>Skips updating the <code>hosts</code> file.</dd>
</dl>
Expand Down Expand Up @@ -365,6 +365,8 @@ Options:
<dd>The hostname to use for accessing the site. If not passed, the command will prompt for it.</dd>
<dt><code>--webroot</code></dt>
<dd>The relative path to the site’s webroot. If not passed, the command will prompt for it.</dd>
<dt><code>--skip-hosts</code></dt>
<dd>Skips updating the <code>hosts</code> file.</dd>
</dl>
Example:
Expand Down Expand Up @@ -628,6 +630,8 @@ Options:
<dd>The name of the machine to use. Defaults to <code>nitro-dev</code>.</dd>
<dt><code>--clean</code></dt>
<dd>Remove the configuration file after destroying the machine. Defaults to `false`</dd>
<dt><code>--skip-hosts</code></dt>
<dd>Skips updating the <code>hosts</code> file.</dd>
</dl>
### `edit`
Expand Down
7 changes: 4 additions & 3 deletions internal/cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,12 @@ var addCommand = &cobra.Command{
// create a new mount
skipMount := true
mount := config.Mount{Source: absolutePath}
exists, foundMount := configFile.AlreadyMounted(mount)
exists, found := configFile.AlreadyMounted(mount)
if exists {
fmt.Println(mount.Source, "is already mounted at", foundMount.Dest, ". Using existing instead of creating new mount.")
fmt.Println(mount.Source, "is already mounted at", found.Dest, ". Using existing instead of creating new mount.")

webRootPath = webroot.ForExistingMount(found, absolutePath, webrootDir)

webRootPath = foundMount.Dest + "/" + directoryName + "/" + webrootDir
fmt.Println("Setting webroot to", webRootPath)
} else {
mount.Dest = "/home/ubuntu/sites/" + directoryName
Expand Down
7 changes: 6 additions & 1 deletion internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func loadConfig() {
viper.AddConfigPath(home + "/" + ".nitro")
viper.SetConfigType("yaml")

// check for a default machine
defaultMachine := os.Getenv("NITRO_DEFAULT_MACHINE")

if flagMachineName != "" {
viper.SetConfigName(flagMachineName)
} else if defaultMachine != "" {
Expand All @@ -75,5 +75,10 @@ func loadConfig() {
viper.SetConfigName("nitro-dev")
}

// if hosts editing is disabled, always skip hosts editing on the host machine
if os.Getenv("NITRO_EDIT_HOSTS") == "false" {
flagSkipHosts = true
}

_ = viper.ReadInConfig()
}
33 changes: 33 additions & 0 deletions internal/webroot/webroot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"path/filepath"
"strings"

"github.com/craftcms/nitro/config"
)

// Find takes a directory and will search for the "webroot" automatically.
Expand Down Expand Up @@ -64,3 +66,34 @@ func Matches(output, webroot string) (bool, string) {

return false, sp[1]
}

// ForExistingMount will determine the webroot on the virtual machine using an existing mount
// it accounts for nested folders.
func ForExistingMount(mount config.Mount, absolutePath, webrootDirectory string) string {
existing := strings.Split(mount.AbsSourcePath(), string(os.PathSeparator))
newPath := strings.Split(absolutePath, string(os.PathSeparator))

// find where things don't match up
index := 0
for i, e := range existing {
if e != newPath[i] {
index = i
continue
}

index = i
}

// combine the
dest := []string{mount.Dest}

// append the remaining elements
remainder := newPath[index+1:]
dest = append(dest, remainder...)

// append the webroot directory
dest = append(dest, webrootDirectory)

// join with the linux path separator
return strings.Join(dest, "/")
}
63 changes: 62 additions & 1 deletion internal/webroot/webroot_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package webroot

import "testing"
import (
"testing"

"github.com/craftcms/nitro/config"
)

func TestFindWebRoot(t *testing.T) {
type args struct {
Expand Down Expand Up @@ -58,3 +62,60 @@ func TestFindWebRoot(t *testing.T) {
})
}
}

func TestForExistingMount(t *testing.T) {
type args struct {
mount config.Mount
absPath string
webrootDir string
}
tests := []struct {
name string
args args
want string
}{
{
name: "use case",
args: args{
mount: config.Mount{
Source: "/Users/jasonmccallister/dev",
Dest: "/home/ubuntu/sites",
},
absPath: "/Users/jasonmccallister/dev/someproject",
webrootDir: "web",
},
want: "/home/ubuntu/sites/someproject/web",
},
{
name: "returns properly subnested folders",
args: args{
mount: config.Mount{
Source: "/Users/someuser/dev-folder",
Dest: "/home/ubuntu/dev-folder",
},
absPath: "/Users/someuser/dev-folder/something/nested",
webrootDir: "web",
},
want: "/home/ubuntu/dev-folder/something/nested/web",
},
{
name: "returns webroot if not nested",
args: args{
mount: config.Mount{
Source: "/Users/someuser/dev-folder",
Dest: "/home/ubuntu/dev-folder",
},
absPath: "/Users/someuser/dev-folder/something",
webrootDir: "web",
},
want: "/home/ubuntu/dev-folder/something/web",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ForExistingMount(tt.args.mount, tt.args.absPath, tt.args.webrootDir); got != tt.want {
t.Errorf("webrootForExistingMount() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit e3588e4

Please # to comment.