@@ -56,17 +56,16 @@ func ensureNerdctlArchiveCache(y *limayaml.LimaYAML) (string, error) {
56
56
return "" , fileutils .Errors (errs )
57
57
}
58
58
59
- func Start (ctx context.Context , inst * store.Instance ) error {
60
- haPIDPath := filepath .Join (inst .Dir , filenames .HostAgentPID )
61
- if _ , err := os .Stat (haPIDPath ); ! errors .Is (err , os .ErrNotExist ) {
62
- return fmt .Errorf ("instance %q seems running (hint: remove %q if the instance is not actually running)" , inst .Name , haPIDPath )
63
- }
64
-
65
- haSockPath := filepath .Join (inst .Dir , filenames .HostAgentSock )
59
+ type Prepared struct {
60
+ Driver driver.Driver
61
+ NerdctlArchiveCache string
62
+ }
66
63
64
+ // Prepare ensures the disk, the nerdctl archive, etc.
65
+ func Prepare (_ context.Context , inst * store.Instance ) (* Prepared , error ) {
67
66
y , err := inst .LoadYAML ()
68
67
if err != nil {
69
- return err
68
+ return nil , err
70
69
}
71
70
72
71
limaDriver := driverutil .CreateTargetDriverInstance (& driver.BaseDriver {
@@ -75,13 +74,34 @@ func Start(ctx context.Context, inst *store.Instance) error {
75
74
})
76
75
77
76
if err := limaDriver .Validate (); err != nil {
78
- return err
77
+ return nil , err
79
78
}
80
79
81
80
if err := limaDriver .CreateDisk (); err != nil {
82
- return err
81
+ return nil , err
83
82
}
84
83
nerdctlArchiveCache , err := ensureNerdctlArchiveCache (y )
84
+ if err != nil {
85
+ return nil , err
86
+ }
87
+
88
+ return & Prepared {
89
+ Driver : limaDriver ,
90
+ NerdctlArchiveCache : nerdctlArchiveCache ,
91
+ }, nil
92
+ }
93
+
94
+ // Start starts the instance.
95
+ // Start calls Prepare by itself, so you do not need to call Prepare manually before calling Start.
96
+ func Start (ctx context.Context , inst * store.Instance ) error {
97
+ haPIDPath := filepath .Join (inst .Dir , filenames .HostAgentPID )
98
+ if _ , err := os .Stat (haPIDPath ); ! errors .Is (err , os .ErrNotExist ) {
99
+ return fmt .Errorf ("instance %q seems running (hint: remove %q if the instance is not actually running)" , inst .Name , haPIDPath )
100
+ }
101
+
102
+ haSockPath := filepath .Join (inst .Dir , filenames .HostAgentSock )
103
+
104
+ prepared , err := Prepare (ctx , inst )
85
105
if err != nil {
86
106
return err
87
107
}
@@ -117,11 +137,11 @@ func Start(ctx context.Context, inst *store.Instance) error {
117
137
"hostagent" ,
118
138
"--pidfile" , haPIDPath ,
119
139
"--socket" , haSockPath )
120
- if limaDriver .CanRunGUI () {
140
+ if prepared . Driver .CanRunGUI () {
121
141
args = append (args , "--run-gui" )
122
142
}
123
- if nerdctlArchiveCache != "" {
124
- args = append (args , "--nerdctl-archive" , nerdctlArchiveCache )
143
+ if prepared . NerdctlArchiveCache != "" {
144
+ args = append (args , "--nerdctl-archive" , prepared . NerdctlArchiveCache )
125
145
}
126
146
args = append (args , inst .Name )
127
147
haCmd := exec .CommandContext (ctx , self , args ... )
0 commit comments