Skip to content

Commit 224c965

Browse files
authored
Merge pull request #1250 from deitch/template-stdin
add support for reading template from stdin
2 parents 2b516bb + fb551bc commit 224c965

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

cmd/limactl/start.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"errors"
55
"fmt"
6+
"io"
67
"net/http"
78
"os"
89
"path/filepath"
@@ -43,6 +44,9 @@ $ limactl start --name=default /usr/local/share/lima/examples/fedora.yaml
4344
4445
To create an instance "default" from a remote URL (use carefully, with a trustable source):
4546
$ limactl start --name=default https://raw.githubusercontent.com/lima-vm/lima/master/examples/alpine.yaml
47+
48+
To create an instance "local" from a template passed to stdin (--name parameter is required):
49+
$ cat template.yaml | limactl start --name=local -
4650
`,
4751
Short: "Start an instance of Lima",
4852
Args: cobra.MaximumNArgs(1),
@@ -67,6 +71,13 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string) (*store.Instance, e
6771
st = &creatorState{}
6872
err error
6973
)
74+
75+
// Create an instance, with menu TUI when TTY is available
76+
tty, err := cmd.Flags().GetBool("tty")
77+
if err != nil {
78+
return nil, err
79+
}
80+
7081
st.instName, err = cmd.Flags().GetString("name")
7182
if err != nil {
7283
return nil, err
@@ -136,6 +147,20 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string) (*store.Instance, e
136147
if err != nil {
137148
return nil, err
138149
}
150+
} else if arg == "-" {
151+
if st.instName == "" {
152+
return nil, errors.New("must pass instance name with --name when reading template from stdin")
153+
}
154+
st.yBytes, err = io.ReadAll(os.Stdin)
155+
if err != nil && err != io.EOF {
156+
return nil, fmt.Errorf("unexpected error reading stdin: %w", err)
157+
}
158+
// see if the tty was set explicitly or not
159+
ttySet := cmd.Flags().Changed("tty")
160+
if ttySet && tty {
161+
return nil, errors.New("cannot use --tty=true and read template from stdin together")
162+
}
163+
tty = false
139164
} else {
140165
if arg == "" {
141166
if st.instName == "" {
@@ -173,11 +198,6 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string) (*store.Instance, e
173198
}
174199
}
175200

176-
// Create an instance, with menu TUI when TTY is available
177-
tty, err := cmd.Flags().GetBool("tty")
178-
if err != nil {
179-
return nil, err
180-
}
181201
if tty {
182202
var err error
183203
st, err = chooseNextCreatorState(st)

0 commit comments

Comments
 (0)