Repository: roots/trellis-cli
Pull request #374 (2023-03-25 21:30:24)

Lima config updates
* Only output `portForwards` if any exist
* Delete VM config on `vm delete`


Patch:
diff --git a/cmd/alias.go b/cmd/alias.go
index 5a8b6f8..c64a930 100644
--- a/cmd/alias.go
+++ b/cmd/alias.go
@@ -119,16 +119,10 @@ func (c *AliasCommand) Run(args []string) int {
 		}
 
 		if !c.skipLocal && c.local == environment {
-			_, site, err := c.Trellis.MainSiteFromEnvironment(environment)
-
-			if err != nil {
-				spinner.StopFail()
-				c.UI.Error(err.Error())
-				return 1
-			}
-
+			siteName, _ := c.Trellis.FindSiteNameFromEnvironment(environment, "")
+			mainHost := c.Trellis.SiteFromEnvironmentAndName(environment, siteName).MainHost()
 			args = append(args, "-e", "include_local_env=true")
-			args = append(args, "-e", "local_hostname_alias="+site.MainHost())
+			args = append(args, "-e", "local_hostname_alias="+mainHost)
 		}
 
 		mockUi := cli.NewMockUi()
diff --git a/cmd/key_generate.go b/cmd/key_generate.go
index a416841..568a4ff 100644
--- a/cmd/key_generate.go
+++ b/cmd/key_generate.go
@@ -99,9 +99,9 @@ func (c *KeyGenerateCommand) Run(args []string) int {
 	}
 
 	if c.keyName == "" {
-		siteName, _, siteErr := c.Trellis.MainSiteFromEnvironment("development")
-		if siteErr != nil {
-			c.UI.Error(siteErr.Error())
+		siteName, siteNameErr := c.Trellis.FindSiteNameFromEnvironment("development", "")
+		if siteNameErr != nil {
+			c.UI.Error(siteNameErr.Error())
 			return 1
 		}
 
diff --git a/cmd/open.go b/cmd/open.go
index f1c1afa..3eb952a 100644
--- a/cmd/open.go
+++ b/cmd/open.go
@@ -34,13 +34,14 @@ func (c *OpenCommand) Run(args []string) int {
 	var openArgs = []string{}
 
 	if len(args) == 0 {
-		_, site, siteErr := c.Trellis.MainSiteFromEnvironment("development")
-		if siteErr != nil {
-			c.UI.Error(siteErr.Error())
+		siteName, siteNameErr := c.Trellis.FindSiteNameFromEnvironment("development", "")
+		if siteNameErr != nil {
+			c.UI.Error(siteNameErr.Error())
 			return 1
 		}
 
-		openArgs = []string{site.MainUrl()}
+		url := c.Trellis.SiteFromEnvironmentAndName("development", siteName).MainUrl()
+		openArgs = []string{url}
 	} else {
 		value, exists := c.Trellis.CliConfig.Open[args[0]]
 
diff --git a/cmd/vm_delete.go b/cmd/vm_delete.go
index fa00db8..a656640 100644
--- a/cmd/vm_delete.go
+++ b/cmd/vm_delete.go
@@ -51,7 +51,7 @@ func (c *VmDeleteCommand) Run(args []string) int {
 		return 1
 	}
 
-	siteName, _, err := c.Trellis.MainSiteFromEnvironment("development")
+	siteName, err := c.Trellis.FindSiteNameFromEnvironment("development", "")
 	if err != nil {
 		c.UI.Error(err.Error())
 		return 1
diff --git a/cmd/vm_shell.go b/cmd/vm_shell.go
index ec67565..794ccd5 100644
--- a/cmd/vm_shell.go
+++ b/cmd/vm_shell.go
@@ -20,7 +20,7 @@ func (c *VmShellCommand) Run(args []string) int {
 
 	c.Trellis.CheckVirtualenv(c.UI)
 
-	siteName, _, err := c.Trellis.MainSiteFromEnvironment("development")
+	siteName, err := c.Trellis.FindSiteNameFromEnvironment("development", "")
 	if err != nil {
 		c.UI.Error(err.Error())
 		return 1
diff --git a/cmd/vm_start.go b/cmd/vm_start.go
index d694dbf..69f715b 100644
--- a/cmd/vm_start.go
+++ b/cmd/vm_start.go
@@ -49,9 +49,9 @@ func (c *VmStartCommand) Run(args []string) int {
 		return 1
 	}
 
-	siteName, _, err := c.Trellis.MainSiteFromEnvironment("development")
+	siteName, err := c.Trellis.FindSiteNameFromEnvironment("development", "")
 	if err != nil {
-		c.UI.Error("Error: could not automatically set VM name: " + err.Error())
+		c.UI.Error(err.Error())
 		return 1
 	}
 
diff --git a/cmd/vm_stop.go b/cmd/vm_stop.go
index a090425..e0e22f0 100644
--- a/cmd/vm_stop.go
+++ b/cmd/vm_stop.go
@@ -47,7 +47,7 @@ func (c *VmStopCommand) Run(args []string) int {
 		return 1
 	}
 
-	siteName, _, err := c.Trellis.MainSiteFromEnvironment("development")
+	siteName, err := c.Trellis.FindSiteNameFromEnvironment("development", "")
 	if err != nil {
 		c.UI.Error(err.Error())
 		return 1
diff --git a/go.mod b/go.mod
index a525322..2841ce1 100644
--- a/go.mod
+++ b/go.mod
@@ -4,11 +4,11 @@ go 1.18
 
 require (
 	github.com/digitalocean/godo v1.83.0
-	github.com/fatih/color v1.15.0
+	github.com/fatih/color v1.14.1
 	github.com/google/go-cmp v0.5.9
 	github.com/hashicorp/go-version v1.6.0
 	github.com/manifoldco/promptui v0.9.0
-	github.com/mattn/go-isatty v0.0.18
+	github.com/mattn/go-isatty v0.0.17
 	github.com/mcuadros/go-version v0.0.0-20190830083331-035f6764e8d2
 	github.com/mholt/archiver v3.1.1+incompatible
 	github.com/mitchellh/cli v1.1.5
diff --git a/go.sum b/go.sum
index f72fb85..b0dd2ff 100644
--- a/go.sum
+++ b/go.sum
@@ -32,8 +32,8 @@ github.com/dsnet/compress v0.0.1 h1:PlZu0n3Tuv04TzpfPbrnI0HW/YwodEXDS+oPKahKF0Q=
 github.com/dsnet/compress v0.0.1/go.mod h1:Aw8dCMJ7RioblQeTqt88akK31OvO8Dhf5JflhBbQEHo=
 github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY=
 github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
-github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
-github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
+github.com/fatih/color v1.14.1 h1:qfhVLaG5s+nCROl1zJsZRxFeYrHLqWroPOQ8BWiNb4w=
+github.com/fatih/color v1.14.1/go.mod h1:2oHN61fhTpgcxD3TSWCgKDiH1+x4OiDVVGH8WlgGZGg=
 github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
 github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
 github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
@@ -82,8 +82,8 @@ github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxec
 github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
 github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
 github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
-github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
-github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
+github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
+github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
 github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
 github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
 github.com/mcuadros/go-version v0.0.0-20190830083331-035f6764e8d2 h1:YocNLcTBdEdvY3iDK6jfWXvEaM5OCKkjxPKoJRdB3Gg=
diff --git a/pkg/lima/files/config.yml b/pkg/lima/files/config.yml
index abe3b70..a66d7be 100644
--- a/pkg/lima/files/config.yml
+++ b/pkg/lima/files/config.yml
@@ -15,11 +15,13 @@ mounts:
 mountType: "virtiofs"
 networks:
 - vzNAT: true
+{{ if .Config.PortForwards }}
 portForwards:
 {{ range $port := .Config.PortForwards -}}
 - guestPort: {{ $port.GuestPort}}
   hostPort: {{ $port.HostPort }}
 {{ end }}
+{{ end -}}
 containerd:
   user: false
 provision:
diff --git a/pkg/lima/instance.go b/pkg/lima/instance.go
index e37c618..48368b9 100644
--- a/pkg/lima/instance.go
+++ b/pkg/lima/instance.go
@@ -91,6 +91,15 @@ func (i *Instance) CreateInventoryFile() error {
 	return nil
 }
 
+func (i *Instance) DeleteConfig() error {
+	err := os.Remove(i.ConfigFile)
+	if err != nil {
+		return fmt.Errorf("Could not delete config file: %v", err)
+	}
+
+	return nil
+}
+
 /*
 Gets the IP address of the instance using the output of `ip route`:
   default via 192.168.64.1 proto dhcp src 192.168.64.2 metric 100
diff --git a/pkg/lima/manager.go b/pkg/lima/manager.go
index 4179317..64a5bdf 100644
--- a/pkg/lima/manager.go
+++ b/pkg/lima/manager.go
@@ -105,10 +105,20 @@ func (m *Manager) DeleteInstance(name string) error {
 	}
 
 	if instance.Stopped() {
-		return command.WithOptions(
+		err := command.WithOptions(
 			command.WithTermOutput(),
 			command.WithLogging(m.ui),
 		).Cmd("limactl", []string{"delete", instance.Name}).Run()
+
+		if err != nil {
+			return err
+		}
+
+		if err := instance.DeleteConfig(); err != nil {
+			return err
+		}
+
+		return nil
 	} else {
 		return fmt.Errorf("Error: VM is running. Run `trellis vm stop` to stop it.")
 	}
diff --git a/trellis/trellis.go b/trellis/trellis.go
index 3dbb737..5fd6396 100644
--- a/trellis/trellis.go
+++ b/trellis/trellis.go
@@ -265,18 +265,6 @@ func (t *Trellis) FindSiteNameFromEnvironment(environment string, siteNameArg st
 	return "", fmt.Errorf("Error: %s is not a valid site. Valid options are %s", siteNameArg, siteNames)
 }
 
-func (t *Trellis) MainSiteFromEnvironment(environment string) (string, *Site, error) {
-	sites := t.SiteNamesFromEnvironment(environment)
-
-	if len(sites) == 0 {
-		return "", nil, fmt.Errorf("Error: No sites found in %s environment", environment)
-	}
-
-	name := sites[0]
-
-	return name, t.Environments[environment].WordPressSites[name], nil
-}
-
 func (t *Trellis) getDefaultSiteNameFromEnvironment(environment string) (siteName string, err error) {
 	sites := t.SiteNamesFromEnvironment(environment)
 


Test patch:
diff --git a/pkg/lima/instance_test.go b/pkg/lima/instance_test.go
index f1075b2..a407688 100644
--- a/pkg/lima/instance_test.go
+++ b/pkg/lima/instance_test.go
@@ -68,6 +68,7 @@ mounts:
 mountType: "virtiofs"
 networks:
 - vzNAT: true
+
 portForwards:
 - guestPort: 80
   hostPort: 1234
diff --git a/trellis/trellis_test.go b/trellis/trellis_test.go
index d23dbd6..e3b2e81 100644
--- a/trellis/trellis_test.go
+++ b/trellis/trellis_test.go
@@ -239,33 +239,6 @@ func TestSiteFromEnvironmentAndName(t *testing.T) {
 	}
 }
 
-func TestMainSiteFromEnvironment(t *testing.T) {
-	expected := &Site{}
-
-	environments := make(map[string]*Config)
-	environments["a"] = &Config{
-		WordPressSites: make(map[string]*Site),
-	}
-
-	environments["a"].WordPressSites["a1"] = expected
-	environments["a"].WordPressSites["a2"] = &Site{}
-	environments["a"].WordPressSites["a3"] = &Site{}
-
-	trellis := Trellis{
-		Environments: environments,
-	}
-
-	name, actual, _ := trellis.MainSiteFromEnvironment("a")
-
-	if name != "a1" {
-		t.Errorf("expected a1 got %s", name)
-	}
-
-	if actual != expected {
-		t.Error("expected site not returned")
-	}
-}
-
 func TestActivateProjectForProjects(t *testing.T) {
 	defer LoadFixtureProject(t)()