Repository: lima-vm/lima
Pull request #3590 (2025-05-27 01:50:24)

Change package name of `instance/hostname` from `instance` to `hostname`
https://github.com/lima-vm/lima/pull/3578#discussion_r2107430960


Patch:
diff --git a/pkg/cidata/cidata.go b/pkg/cidata/cidata.go
index dba3cbc9..8439ca02 100644
--- a/pkg/cidata/cidata.go
+++ b/pkg/cidata/cidata.go
@@ -23,7 +23,7 @@ import (
 	"github.com/sirupsen/logrus"
 
 	"github.com/lima-vm/lima/pkg/debugutil"
-	instance "github.com/lima-vm/lima/pkg/instance/hostname"
+	"github.com/lima-vm/lima/pkg/instance/hostname"
 	"github.com/lima-vm/lima/pkg/iso9660util"
 	"github.com/lima-vm/lima/pkg/limayaml"
 	"github.com/lima-vm/lima/pkg/localpathutil"
@@ -124,7 +124,7 @@ func templateArgs(bootScripts bool, instDir, name string, instConfig *limayaml.L
 		Debug:              debugutil.Debug,
 		BootScripts:        bootScripts,
 		Name:               name,
-		Hostname:           instance.HostnameFromInstName(name), // TODO: support customization
+		Hostname:           hostname.FromInstName(name), // TODO: support customization
 		User:               *instConfig.User.Name,
 		Comment:            *instConfig.User.Comment,
 		Home:               *instConfig.User.Home,
diff --git a/pkg/hostagent/hostagent.go b/pkg/hostagent/hostagent.go
index ee8b42bd..a6e293e1 100644
--- a/pkg/hostagent/hostagent.go
+++ b/pkg/hostagent/hostagent.go
@@ -34,7 +34,7 @@ import (
 	hostagentapi "github.com/lima-vm/lima/pkg/hostagent/api"
 	"github.com/lima-vm/lima/pkg/hostagent/dns"
 	"github.com/lima-vm/lima/pkg/hostagent/events"
-	instance "github.com/lima-vm/lima/pkg/instance/hostname"
+	"github.com/lima-vm/lima/pkg/instance/hostname"
 	"github.com/lima-vm/lima/pkg/limayaml"
 	"github.com/lima-vm/lima/pkg/networks"
 	"github.com/lima-vm/lima/pkg/osutil"
@@ -288,8 +288,8 @@ func (a *HostAgent) Run(ctx context.Context) error {
 	if limayaml.FirstUsernetIndex(a.instConfig) == -1 && *a.instConfig.HostResolver.Enabled {
 		hosts := a.instConfig.HostResolver.Hosts
 		hosts["host.lima.internal"] = networks.SlirpGateway
-		hostname := instance.HostnameFromInstName(a.instName) // TODO: support customization
-		hosts[hostname] = networks.SlirpIPAddress
+		name := hostname.FromInstName(a.instName) // TODO: support customization
+		hosts[name] = networks.SlirpIPAddress
 		srvOpts := dns.ServerOptions{
 			UDPPort: a.udpDNSLocalPort,
 			TCPPort: a.tcpDNSLocalPort,
diff --git a/pkg/instance/hostname/hostname.go b/pkg/instance/hostname/hostname.go
index 38756c85..c67f0ff9 100644
--- a/pkg/instance/hostname/hostname.go
+++ b/pkg/instance/hostname/hostname.go
@@ -1,14 +1,14 @@
 // SPDX-FileCopyrightText: Copyright The Lima Authors
 // SPDX-License-Identifier: Apache-2.0
 
-package instance
+package hostname
 
 import "strings"
 
-// HostnameFromInstName generates a hostname from an instance name by prefixing
+// FromInstName generates a hostname from an instance name by prefixing
 // it with "lima-" and replacing all dots and underscores with dashes.
 // E.g. "my_example.com" becomes "lima-my-example-com".
-func HostnameFromInstName(instName string) string {
+func FromInstName(instName string) string {
 	s := strings.ReplaceAll(instName, ".", "-")
 	s = strings.ReplaceAll(s, "_", "-")
 	return "lima-" + s
diff --git a/pkg/limayaml/defaults.go b/pkg/limayaml/defaults.go
index 2351287a..a2193a28 100644
--- a/pkg/limayaml/defaults.go
+++ b/pkg/limayaml/defaults.go
@@ -28,7 +28,7 @@ import (
 	"github.com/sirupsen/logrus"
 	"golang.org/x/sys/cpu"
 
-	instance "github.com/lima-vm/lima/pkg/instance/hostname"
+	"github.com/lima-vm/lima/pkg/instance/hostname"
 	"github.com/lima-vm/lima/pkg/ioutilx"
 	"github.com/lima-vm/lima/pkg/localpathutil"
 	. "github.com/lima-vm/lima/pkg/must"
@@ -944,7 +944,7 @@ func executeGuestTemplate(format, instDir string, user User, param map[string]st
 		name := filepath.Base(instDir)
 		data := map[string]any{
 			"Name":     name,
-			"Hostname": instance.HostnameFromInstName(name), // TODO: support customization
+			"Hostname": hostname.FromInstName(name), // TODO: support customization
 			"UID":      *user.UID,
 			"User":     *user.Name,
 			"Home":     *user.Home,
diff --git a/pkg/sshutil/format.go b/pkg/sshutil/format.go
index 652b5429..08569650 100644
--- a/pkg/sshutil/format.go
+++ b/pkg/sshutil/format.go
@@ -8,7 +8,7 @@ import (
 	"io"
 	"strings"
 
-	instance "github.com/lima-vm/lima/pkg/instance/hostname"
+	"github.com/lima-vm/lima/pkg/instance/hostname"
 )
 
 // FormatT specifies the format type.
@@ -62,7 +62,7 @@ func quoteOption(o string) string {
 
 // Format formats the ssh options.
 func Format(w io.Writer, sshPath, instName string, format FormatT, opts []string) error {
-	fakeHostname := instance.HostnameFromInstName(instName) // TODO: support customization
+	fakeHostname := hostname.FromInstName(instName) // TODO: support customization
 	switch format {
 	case FormatCmd:
 		args := []string{sshPath}
diff --git a/pkg/store/instance.go b/pkg/store/instance.go
index b9748c9b..19b4b227 100644
--- a/pkg/store/instance.go
+++ b/pkg/store/instance.go
@@ -22,7 +22,7 @@ import (
 	"github.com/sirupsen/logrus"
 
 	hostagentclient "github.com/lima-vm/lima/pkg/hostagent/api/client"
-	instancehostname "github.com/lima-vm/lima/pkg/instance/hostname"
+	"github.com/lima-vm/lima/pkg/instance/hostname"
 	"github.com/lima-vm/lima/pkg/limayaml"
 	"github.com/lima-vm/lima/pkg/store/dirnames"
 	"github.com/lima-vm/lima/pkg/store/filenames"
@@ -74,7 +74,7 @@ func Inspect(instName string) (*Instance, error) {
 	inst := &Instance{
 		Name: instName,
 		// TODO: support customizing hostname
-		Hostname: instancehostname.HostnameFromInstName(instName),
+		Hostname: hostname.FromInstName(instName),
 		Status:   StatusUnknown,
 	}
 	// InstanceDir validates the instName but does not check whether the instance exists


Test patch:
diff --git a/pkg/instance/hostname/hostname_test.go b/pkg/instance/hostname/hostname_test.go
index 8173d62d..e702167e 100644
--- a/pkg/instance/hostname/hostname_test.go
+++ b/pkg/instance/hostname/hostname_test.go
@@ -1,18 +1,18 @@
 // SPDX-FileCopyrightText: Copyright The Lima Authors
 // SPDX-License-Identifier: Apache-2.0
 
-package instance_test
+package hostname_test
 
 import (
 	"testing"
 
 	"gotest.tools/v3/assert"
 
-	instance "github.com/lima-vm/lima/pkg/instance/hostname"
+	"github.com/lima-vm/lima/pkg/instance/hostname"
 )
 
-func TestHostnameFromInstName(t *testing.T) {
-	assert.Equal(t, instance.HostnameFromInstName("default"), "lima-default")
-	assert.Equal(t, instance.HostnameFromInstName("ubuntu-24.04"), "lima-ubuntu-24-04")
-	assert.Equal(t, instance.HostnameFromInstName("foo_bar.baz"), "lima-foo-bar-baz")
+func TestFromInstName(t *testing.T) {
+	assert.Equal(t, hostname.FromInstName("default"), "lima-default")
+	assert.Equal(t, hostname.FromInstName("ubuntu-24.04"), "lima-ubuntu-24-04")
+	assert.Equal(t, hostname.FromInstName("foo_bar.baz"), "lima-foo-bar-baz")
 }