Skip to content

Commit ecc4f8b

Browse files
authored
Merge pull request #472 from afbjorklund/message
Add optional message to be shown to template
2 parents 9c4cb2d + 2a7b7ed commit ecc4f8b

File tree

8 files changed

+99
-38
lines changed

8 files changed

+99
-38
lines changed

cmd/limactl/list.go

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7-
"path/filepath"
87
"reflect"
98
"sort"
109
"strings"
@@ -13,38 +12,13 @@ import (
1312

1413
"github.com/docker/go-units"
1514
"github.com/lima-vm/lima/pkg/store"
16-
"github.com/lima-vm/lima/pkg/store/dirnames"
17-
"github.com/lima-vm/lima/pkg/store/filenames"
1815
"github.com/sirupsen/logrus"
1916
"github.com/spf13/cobra"
2017
)
2118

22-
type formatData struct {
23-
store.Instance
24-
LimaHome string
25-
IdentityFile string
26-
}
27-
28-
func addGlobalFields(inst *store.Instance) (formatData, error) {
29-
var data formatData
30-
data.Instance = *inst
31-
// Add IdentityFile
32-
configDir, err := dirnames.LimaConfigDir()
33-
if err != nil {
34-
return formatData{}, err
35-
}
36-
data.IdentityFile = filepath.Join(configDir, filenames.UserPrivateKey)
37-
// Add LimaHome
38-
data.LimaHome, err = dirnames.LimaDir()
39-
if err != nil {
40-
return formatData{}, err
41-
}
42-
return data, nil
43-
}
44-
4519
func fieldNames() []string {
4620
names := []string{}
47-
var data formatData
21+
var data store.FormatData
4822
t := reflect.TypeOf(data)
4923
for i := 0; i < t.NumField(); i++ {
5024
f := t.Field(i)
@@ -161,7 +135,7 @@ func listAction(cmd *cobra.Command, args []string) error {
161135
logrus.WithError(err).Errorf("instance %q does not exist?", instName)
162136
continue
163137
}
164-
data, err := addGlobalFields(inst)
138+
data, err := store.AddGlobalFields(inst)
165139
if err != nil {
166140
logrus.WithError(err).Error("Cannot add global fields to instance data")
167141
continue

examples/docker.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,7 @@ probes:
6666
portForwards:
6767
- guestSocket: "/run/user/{{.UID}}/docker.sock"
6868
hostSocket: "{{.Dir}}/sock/docker.sock"
69+
message: |
70+
To run `docker` on the host (assumes docker-cli is installed):
71+
$ export DOCKER_HOST=unix://{{.Dir}}/sock/docker.sock
72+
$ docker ...

examples/podman.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,7 @@ probes:
5353
portForwards:
5454
- guestSocket: "/run/user/{{.UID}}/podman/podman.sock"
5555
hostSocket: "{{.Dir}}/sock/podman.sock"
56+
message: |
57+
To run `podman` on the host (assumes podman-remote is installed):
58+
$ export CONTAINER_HOST=unix://{{.Dir}}/sock/podman.sock
59+
$ podman{{if eq .HostOS "linux"}}-remote{{end}} ...

pkg/limayaml/default.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,13 @@ networks:
196196
# hostPortRange: [1, 65535]
197197
# # Any port still not matched by a rule will not be forwarded (ignored)
198198

199+
# Message. Information to be shown to the user, given as a Go template for the instance.
200+
# The same template variables as for listing instances can be used, for example {{.Dir}}.
201+
# You can view the complete list of variables using `limactl list --list-fields` command.
202+
# It also includes {{.HostOS}} and {{.HostArch}} vars, for the runtime GOOS and GOARCH.
203+
# message: |
204+
# This will be shown to the user.
205+
199206
# Extra environment variables that will be loaded into the VM at start up.
200207
# These variables are consumed by internal init scripts, and also added
201208
# to /etc/environment.

pkg/limayaml/defaults.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func FillDefault(y, d, o *LimaYAML, filePath string) {
7171
if o.Arch != nil {
7272
y.Arch = o.Arch
7373
}
74-
y.Arch = pointer.String(resolveArch(y.Arch))
74+
y.Arch = pointer.String(ResolveArch(y.Arch))
7575

7676
y.Images = append(append(o.Images, y.Images...), d.Images...)
7777
for i := range y.Images {
@@ -400,13 +400,21 @@ func FillPortForwardDefaults(rule *PortForward, instDir string) {
400400
}
401401
}
402402

403-
func resolveArch(s *string) Arch {
403+
func NewArch(arch string) Arch {
404+
switch arch {
405+
case "amd64":
406+
return X8664
407+
case "arm64":
408+
return AARCH64
409+
default:
410+
logrus.Warnf("Unknown arch: %s", arch)
411+
return arch
412+
}
413+
}
414+
415+
func ResolveArch(s *string) Arch {
404416
if s == nil || *s == "" || *s == "default" {
405-
if runtime.GOARCH == "amd64" {
406-
return X8664
407-
} else {
408-
return AARCH64
409-
}
417+
return NewArch(runtime.GOARCH)
410418
}
411419
return *s
412420
}

pkg/limayaml/limayaml.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type LimaYAML struct {
2020
Containerd Containerd `yaml:"containerd,omitempty" json:"containerd,omitempty"`
2121
Probes []Probe `yaml:"probes,omitempty" json:"probes,omitempty"`
2222
PortForwards []PortForward `yaml:"portForwards,omitempty" json:"portForwards,omitempty"`
23+
Message string `yaml:"message,omitempty" json:"message,omitempty"`
2324
Networks []Network `yaml:"networks,omitempty" json:"networks,omitempty"`
2425
Network NetworkDeprecated `yaml:"network,omitempty" json:"network,omitempty"` // DEPRECATED, use `networks` instead
2526
Env map[string]string `yaml:"env,omitempty" json:"env,omitempty"`

pkg/start/start.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package start
22

33
import (
4+
"bufio"
5+
"bytes"
46
"context"
57
"errors"
68
"fmt"
79
"os"
810
"os/exec"
911
"path/filepath"
12+
"text/template"
1013
"time"
1114

1215
"github.com/lima-vm/lima/pkg/downloader"
@@ -147,7 +150,7 @@ func Start(ctx context.Context, inst *store.Instance) error {
147150

148151
watchErrCh := make(chan error)
149152
go func() {
150-
watchErrCh <- watchHostAgentEvents(ctx, inst.Name, haStdoutPath, haStderrPath, begin)
153+
watchErrCh <- watchHostAgentEvents(ctx, inst, haStdoutPath, haStderrPath, begin)
151154
close(watchErrCh)
152155
}()
153156
waitErrCh := make(chan error)
@@ -181,7 +184,7 @@ func waitHostAgentStart(ctx context.Context, haPIDPath, haStderrPath string) err
181184
}
182185
}
183186

184-
func watchHostAgentEvents(ctx context.Context, instName, haStdoutPath, haStderrPath string, begin time.Time) error {
187+
func watchHostAgentEvents(ctx context.Context, inst *store.Instance, haStdoutPath, haStderrPath string, begin time.Time) error {
185188
ctx2, cancel := context.WithTimeout(ctx, 10*time.Minute)
186189
defer cancel()
187190

@@ -210,7 +213,8 @@ func watchHostAgentEvents(ctx context.Context, instName, haStdoutPath, haStderrP
210213
return true
211214
}
212215

213-
logrus.Infof("READY. Run `%s` to open the shell.", LimactlShellCmd(instName))
216+
logrus.Infof("READY. Run `%s` to open the shell.", LimactlShellCmd(inst.Name))
217+
ShowMessage(inst)
214218
err = nil
215219
return true
216220
}
@@ -239,3 +243,29 @@ func LimactlShellCmd(instName string) string {
239243
}
240244
return shellCmd
241245
}
246+
247+
func ShowMessage(inst *store.Instance) error {
248+
if inst.Message == "" {
249+
return nil
250+
}
251+
t, err := template.New("message").Parse(inst.Message)
252+
if err != nil {
253+
return err
254+
}
255+
data, err := store.AddGlobalFields(inst)
256+
if err != nil {
257+
return err
258+
}
259+
var b bytes.Buffer
260+
if err := t.Execute(&b, data); err != nil {
261+
return err
262+
}
263+
scanner := bufio.NewScanner(&b)
264+
for scanner.Scan() {
265+
logrus.Info(scanner.Text())
266+
}
267+
if err := scanner.Err(); err != nil {
268+
return err
269+
}
270+
return nil
271+
}

pkg/store/instance.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"os"
88
"path/filepath"
9+
"runtime"
910
"strconv"
1011
"strings"
1112
"syscall"
@@ -14,6 +15,7 @@ import (
1415
"github.com/docker/go-units"
1516
hostagentclient "github.com/lima-vm/lima/pkg/hostagent/api/client"
1617
"github.com/lima-vm/lima/pkg/limayaml"
18+
"github.com/lima-vm/lima/pkg/store/dirnames"
1719
"github.com/lima-vm/lima/pkg/store/filenames"
1820
)
1921

@@ -34,6 +36,7 @@ type Instance struct {
3436
CPUs int `json:"cpus,omitempty"`
3537
Memory int64 `json:"memory,omitempty"` // bytes
3638
Disk int64 `json:"disk,omitempty"` // bytes
39+
Message string `json:"message,omitempty"`
3740
Networks []limayaml.Network `json:"network,omitempty"`
3841
SSHLocalPort int `json:"sshLocalPort,omitempty"`
3942
HostAgentPID int `json:"hostAgentPID,omitempty"`
@@ -81,6 +84,7 @@ func Inspect(instName string) (*Instance, error) {
8184
if err == nil {
8285
inst.Disk = disk
8386
}
87+
inst.Message = y.Message
8488
inst.Networks = y.Networks
8589
inst.SSHLocalPort = *y.SSH.LocalPort // maybe 0
8690

@@ -164,3 +168,32 @@ func ReadPIDFile(path string) (int, error) {
164168
}
165169
return pid, nil
166170
}
171+
172+
type FormatData struct {
173+
Instance
174+
HostOS string
175+
HostArch string
176+
LimaHome string
177+
IdentityFile string
178+
}
179+
180+
func AddGlobalFields(inst *Instance) (FormatData, error) {
181+
var data FormatData
182+
data.Instance = *inst
183+
// Add HostOS
184+
data.HostOS = runtime.GOOS
185+
// Add HostArch
186+
data.HostArch = limayaml.NewArch(runtime.GOARCH)
187+
// Add IdentityFile
188+
configDir, err := dirnames.LimaConfigDir()
189+
if err != nil {
190+
return FormatData{}, err
191+
}
192+
data.IdentityFile = filepath.Join(configDir, filenames.UserPrivateKey)
193+
// Add LimaHome
194+
data.LimaHome, err = dirnames.LimaDir()
195+
if err != nil {
196+
return FormatData{}, err
197+
}
198+
return data, nil
199+
}

0 commit comments

Comments
 (0)