Skip to content

Commit 47fa754

Browse files
committed
Allow setting a custom instance name in Lima. roots#514
1 parent 99bee2f commit 47fa754

File tree

7 files changed

+36
-10
lines changed

7 files changed

+36
-10
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ Current supported settings:
228228
| `manager` | VM manager (Options: `auto` (depends on OS), `lima`)| string | "auto" |
229229
| `ubuntu` | Ubuntu OS version (Options: `18.04`, `20.04`, `22.04`, `24.04`)| string |
230230
| `hosts_resolver` | VM hosts resolver (Options: `hosts_file`)| string |
231+
| `instance_name` | Custom name for the VM instance | string | First site name alphabetically |
231232
| `images` | Custom OS image | object | Set based on `ubuntu` version |
232233
233234
#### `images`
@@ -246,6 +247,9 @@ open:
246247
site: "https://mysite.com"
247248
admin: "https://mysite.com/wp/wp-admin"
248249
virtualenv_integration: true
250+
vm:
251+
manager: "lima"
252+
instance_name: "custom-instance-name" # Optional: Set a specific VM instance name
249253
```
250254
251255
Example env var usage:

cli_config/cli_config.go

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type VmConfig struct {
2121
HostsResolver string `yaml:"hosts_resolver"`
2222
Images []VmImage `yaml:"images"`
2323
Ubuntu string `yaml:"ubuntu"`
24+
InstanceName string `yaml:"instance_name"`
2425
}
2526

2627
type Config struct {

cmd/vm_delete.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (c *VmDeleteCommand) Run(args []string) int {
5151
return 1
5252
}
5353

54-
siteName, _, err := c.Trellis.MainSiteFromEnvironment("development")
54+
instanceName, err := c.Trellis.GetVmInstanceName()
5555
if err != nil {
5656
c.UI.Error(err.Error())
5757
return 1
@@ -64,7 +64,7 @@ func (c *VmDeleteCommand) Run(args []string) int {
6464
}
6565

6666
if c.force || c.confirmDeletion() {
67-
if err := manager.DeleteInstance(siteName); err != nil {
67+
if err := manager.DeleteInstance(instanceName); err != nil {
6868
c.UI.Error("Error: " + err.Error())
6969
return 1
7070
}

cmd/vm_shell.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ func (c *VmShellCommand) Run(args []string) int {
4141

4242
args = c.flags.Args()
4343

44+
instanceName, err := c.Trellis.GetVmInstanceName()
45+
if err != nil {
46+
c.UI.Error(err.Error())
47+
return 1
48+
}
49+
4450
siteName, _, err := c.Trellis.MainSiteFromEnvironment("development")
4551
if err != nil {
4652
c.UI.Error(err.Error())
@@ -60,7 +66,7 @@ func (c *VmShellCommand) Run(args []string) int {
6066
shellArgs := []string{"--workdir", c.workdir}
6167
shellArgs = append(shellArgs, args...)
6268

63-
if err := manager.OpenShell(siteName, c.workdir, args); err != nil {
69+
if err := manager.OpenShell(instanceName, c.workdir, args); err != nil {
6470
c.UI.Error(err.Error())
6571
return 1
6672
}

cmd/vm_start.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ func (c *VmStartCommand) Run(args []string) int {
4949
return 1
5050
}
5151

52-
siteName, _, err := c.Trellis.MainSiteFromEnvironment("development")
52+
instanceName, err := c.Trellis.GetVmInstanceName()
5353
if err != nil {
54-
c.UI.Error("Error: could not automatically set VM name: " + err.Error())
54+
c.UI.Error(err.Error())
5555
return 1
5656
}
5757

@@ -61,7 +61,7 @@ func (c *VmStartCommand) Run(args []string) int {
6161
return 1
6262
}
6363

64-
err = manager.StartInstance(siteName)
64+
err = manager.StartInstance(instanceName)
6565
if err == nil {
6666
c.printInstanceInfo()
6767
return 0
@@ -74,13 +74,13 @@ func (c *VmStartCommand) Run(args []string) int {
7474
}
7575

7676
// VM doesn't exist yet, create and start it
77-
if err = manager.CreateInstance(siteName); err != nil {
77+
if err = manager.CreateInstance(instanceName); err != nil {
7878
c.UI.Error("Error creating VM.")
7979
c.UI.Error(err.Error())
8080
return 1
8181
}
8282

83-
if err = manager.StartInstance(siteName); err != nil {
83+
if err = manager.StartInstance(instanceName); err != nil {
8484
c.UI.Error("Error starting VM.")
8585
c.UI.Error(err.Error())
8686
return 1

cmd/vm_stop.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (c *VmStopCommand) Run(args []string) int {
4747
return 1
4848
}
4949

50-
siteName, _, err := c.Trellis.MainSiteFromEnvironment("development")
50+
instanceName, err := c.Trellis.GetVmInstanceName()
5151
if err != nil {
5252
c.UI.Error(err.Error())
5353
return 1
@@ -59,7 +59,7 @@ func (c *VmStopCommand) Run(args []string) int {
5959
return 1
6060
}
6161

62-
if err := manager.StopInstance(siteName); err != nil {
62+
if err := manager.StopInstance(instanceName); err != nil {
6363
c.UI.Error(err.Error())
6464
return 1
6565
}

trellis/trellis.go

+15
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ var DefaultCliConfig = cli_config.Config{
3939
Manager: "auto",
4040
HostsResolver: "hosts_file",
4141
Ubuntu: "24.04",
42+
InstanceName: "",
4243
},
4344
}
4445

@@ -234,6 +235,20 @@ func (t *Trellis) ValidateEnvironment(name string) (err error) {
234235
return fmt.Errorf("Error: %s is not a valid environment, valid options are %s", name, t.EnvironmentNames())
235236
}
236237

238+
func (t *Trellis) GetVmInstanceName() (string, error) {
239+
if t.CliConfig.Vm.InstanceName != "" {
240+
return t.CliConfig.Vm.InstanceName, nil
241+
}
242+
243+
// Fallback: get main site from environment if no instance name is set.
244+
siteName, _, err := t.MainSiteFromEnvironment("development")
245+
if err != nil {
246+
return "", fmt.Errorf("Error: could not automatically set VM name: %w", err)
247+
}
248+
return siteName, nil
249+
}
250+
251+
237252
func (t *Trellis) SiteNamesFromEnvironment(environment string) []string {
238253
var names []string
239254

0 commit comments

Comments
 (0)