Skip to content

Commit c9fdbd2

Browse files
committed
Move identifierutil → instance/hostname
Part of the crusade against gratuituous use of `util` package suffix. The package only contains a single `HostnameFromInstName()` function. It can't be moved into the `instance` package because that would create a circular dependency between `instance` and `cidata`. Signed-off-by: Jan Dubois <[email protected]>
1 parent f3b1461 commit c9fdbd2

File tree

8 files changed

+40
-37
lines changed

8 files changed

+40
-37
lines changed

pkg/cidata/cidata.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121

2222
"github.com/docker/go-units"
2323
"github.com/lima-vm/lima/pkg/debugutil"
24-
"github.com/lima-vm/lima/pkg/identifierutil"
24+
instance "github.com/lima-vm/lima/pkg/instance/hostname"
2525
"github.com/lima-vm/lima/pkg/iso9660util"
2626
"github.com/lima-vm/lima/pkg/limayaml"
2727
"github.com/lima-vm/lima/pkg/localpathutil"
@@ -123,7 +123,7 @@ func templateArgs(bootScripts bool, instDir, name string, instConfig *limayaml.L
123123
Debug: debugutil.Debug,
124124
BootScripts: bootScripts,
125125
Name: name,
126-
Hostname: identifierutil.HostnameFromInstName(name), // TODO: support customization
126+
Hostname: instance.HostnameFromInstName(name), // TODO: support customization
127127
User: *instConfig.User.Name,
128128
Comment: *instConfig.User.Comment,
129129
Home: *instConfig.User.Home,

pkg/hostagent/hostagent.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
hostagentapi "github.com/lima-vm/lima/pkg/hostagent/api"
3232
"github.com/lima-vm/lima/pkg/hostagent/dns"
3333
"github.com/lima-vm/lima/pkg/hostagent/events"
34-
"github.com/lima-vm/lima/pkg/identifierutil"
34+
instance "github.com/lima-vm/lima/pkg/instance/hostname"
3535
"github.com/lima-vm/lima/pkg/limayaml"
3636
"github.com/lima-vm/lima/pkg/networks"
3737
"github.com/lima-vm/lima/pkg/osutil"
@@ -288,7 +288,7 @@ func (a *HostAgent) Run(ctx context.Context) error {
288288
if limayaml.FirstUsernetIndex(a.instConfig) == -1 && *a.instConfig.HostResolver.Enabled {
289289
hosts := a.instConfig.HostResolver.Hosts
290290
hosts["host.lima.internal"] = networks.SlirpGateway
291-
hostname := identifierutil.HostnameFromInstName(a.instName) // TODO: support customization
291+
hostname := instance.HostnameFromInstName(a.instName) // TODO: support customization
292292
hosts[hostname] = networks.SlirpIPAddress
293293
srvOpts := dns.ServerOptions{
294294
UDPPort: a.udpDNSLocalPort,

pkg/identifierutil/identifierutil_test.go

Lines changed: 0 additions & 16 deletions
This file was deleted.

pkg/identifierutil/identifierutil.go renamed to pkg/instance/hostname/hostname.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
// SPDX-FileCopyrightText: Copyright The Lima Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package identifierutil
4+
package instance
55

66
import "strings"
77

8+
// HostnameFromInstName generates a hostname from an instance name by prefixing
9+
// it with "lima-" and replacing all dots and underscores with dashes.
10+
// E.g. "my_example.com" becomes "lima-my-example-com".
811
func HostnameFromInstName(instName string) string {
912
s := strings.ReplaceAll(instName, ".", "-")
1013
s = strings.ReplaceAll(s, "_", "-")
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// SPDX-FileCopyrightText: Copyright The Lima Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package instance_test
5+
6+
import (
7+
"testing"
8+
9+
instance "github.com/lima-vm/lima/pkg/instance/hostname"
10+
"gotest.tools/v3/assert"
11+
)
12+
13+
func TestHostnameFromInstName(t *testing.T) {
14+
assert.Equal(t, instance.HostnameFromInstName("default"), "lima-default")
15+
assert.Equal(t, instance.HostnameFromInstName("ubuntu-24.04"), "lima-ubuntu-24-04")
16+
assert.Equal(t, instance.HostnameFromInstName("foo_bar.baz"), "lima-foo-bar-baz")
17+
}

pkg/limayaml/defaults.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,20 @@ import (
2424
"github.com/coreos/go-semver/semver"
2525
"github.com/docker/go-units"
2626
"github.com/goccy/go-yaml"
27+
instance "github.com/lima-vm/lima/pkg/instance/hostname"
2728
"github.com/lima-vm/lima/pkg/ioutilx"
28-
"github.com/lima-vm/lima/pkg/version"
29-
"github.com/pbnjay/memory"
30-
"github.com/sirupsen/logrus"
31-
"golang.org/x/sys/cpu"
32-
33-
"github.com/lima-vm/lima/pkg/identifierutil"
3429
"github.com/lima-vm/lima/pkg/localpathutil"
3530
. "github.com/lima-vm/lima/pkg/must"
3631
"github.com/lima-vm/lima/pkg/networks"
3732
"github.com/lima-vm/lima/pkg/osutil"
3833
"github.com/lima-vm/lima/pkg/ptr"
3934
"github.com/lima-vm/lima/pkg/store/dirnames"
4035
"github.com/lima-vm/lima/pkg/store/filenames"
36+
"github.com/lima-vm/lima/pkg/version"
4137
"github.com/lima-vm/lima/pkg/version/versionutil"
38+
"github.com/pbnjay/memory"
39+
"github.com/sirupsen/logrus"
40+
"golang.org/x/sys/cpu"
4241
)
4342

4443
const (
@@ -944,7 +943,7 @@ func executeGuestTemplate(format, instDir string, user User, param map[string]st
944943
name := filepath.Base(instDir)
945944
data := map[string]any{
946945
"Name": name,
947-
"Hostname": identifierutil.HostnameFromInstName(name), // TODO: support customization
946+
"Hostname": instance.HostnameFromInstName(name), // TODO: support customization
948947
"UID": *user.UID,
949948
"User": *user.Name,
950949
"Home": *user.Home,

pkg/sshutil/format.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"io"
99
"strings"
1010

11-
"github.com/lima-vm/lima/pkg/identifierutil"
11+
instance "github.com/lima-vm/lima/pkg/instance/hostname"
1212
)
1313

1414
// FormatT specifies the format type.
@@ -42,11 +42,11 @@ const (
4242
// Port 60022
4343
FormatConfig = FormatT("config")
4444

45-
// TODO: consider supporting "url" format (ssh://USER@HOSTNAME:PORT)
46-
//
47-
// TODO: consider supporting "json" format
48-
// It is unclear whether we can just map ssh "config" into JSON, as "config" has duplicated keys.
49-
// (JSON supports duplicated keys too, but not all JSON implementations expect JSON with duplicated keys)
45+
// TODO: consider supporting "url" format (ssh://USER@HOSTNAME:PORT).
46+
//
47+
// TODO: consider supporting "json" format.
48+
// It is unclear whether we can just map ssh "config" into JSON, as "config" has duplicated keys.
49+
// (JSON supports duplicated keys too, but not all JSON implementations expect JSON with duplicated keys).
5050
)
5151

5252
// Formats is the list of the supported formats.
@@ -62,7 +62,7 @@ func quoteOption(o string) string {
6262

6363
// Format formats the ssh options.
6464
func Format(w io.Writer, sshPath, instName string, format FormatT, opts []string) error {
65-
fakeHostname := identifierutil.HostnameFromInstName(instName) // TODO: support customization
65+
fakeHostname := instance.HostnameFromInstName(instName) // TODO: support customization
6666
switch format {
6767
case FormatCmd:
6868
args := []string{sshPath}

pkg/store/instance.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020

2121
"github.com/docker/go-units"
2222
hostagentclient "github.com/lima-vm/lima/pkg/hostagent/api/client"
23-
"github.com/lima-vm/lima/pkg/identifierutil"
23+
instancehostname "github.com/lima-vm/lima/pkg/instance/hostname"
2424
"github.com/lima-vm/lima/pkg/limayaml"
2525
"github.com/lima-vm/lima/pkg/store/dirnames"
2626
"github.com/lima-vm/lima/pkg/store/filenames"
@@ -73,7 +73,7 @@ func Inspect(instName string) (*Instance, error) {
7373
inst := &Instance{
7474
Name: instName,
7575
// TODO: support customizing hostname
76-
Hostname: identifierutil.HostnameFromInstName(instName),
76+
Hostname: instancehostname.HostnameFromInstName(instName),
7777
Status: StatusUnknown,
7878
}
7979
// InstanceDir validates the instName but does not check whether the instance exists

0 commit comments

Comments
 (0)