Skip to content

Commit c420190

Browse files
committed
Refactor: move FormatData from list to store
Signed-off-by: Anders F Björklund <[email protected]>
1 parent 9aa6664 commit c420190

File tree

2 files changed

+26
-28
lines changed

2 files changed

+26
-28
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

pkg/store/instance.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/docker/go-units"
1515
hostagentclient "github.com/lima-vm/lima/pkg/hostagent/api/client"
1616
"github.com/lima-vm/lima/pkg/limayaml"
17+
"github.com/lima-vm/lima/pkg/store/dirnames"
1718
"github.com/lima-vm/lima/pkg/store/filenames"
1819
)
1920

@@ -164,3 +165,26 @@ func ReadPIDFile(path string) (int, error) {
164165
}
165166
return pid, nil
166167
}
168+
169+
type FormatData struct {
170+
Instance
171+
LimaHome string
172+
IdentityFile string
173+
}
174+
175+
func AddGlobalFields(inst *Instance) (FormatData, error) {
176+
var data FormatData
177+
data.Instance = *inst
178+
// Add IdentityFile
179+
configDir, err := dirnames.LimaConfigDir()
180+
if err != nil {
181+
return FormatData{}, err
182+
}
183+
data.IdentityFile = filepath.Join(configDir, filenames.UserPrivateKey)
184+
// Add LimaHome
185+
data.LimaHome, err = dirnames.LimaDir()
186+
if err != nil {
187+
return FormatData{}, err
188+
}
189+
return data, nil
190+
}

0 commit comments

Comments
 (0)