cmd/limactl: include name in network list --json output

`limactl network list --json` displayed the Network value struct only.
The name is the map key and was dropped...

Wrap each row in an anonymous struct embedding networks.Network plus a Name field.
Rename the local `networks` slice to `networkNames` (it shadowed the imported networks package).
Fix the not-found log to report name instead of the zero-value nw.

Signed-off-by: coulof <florian.coulombel@suse.com>
(cherry picked from commit cdae63ce38)
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
This commit is contained in:
coulof authored and Akihiro Suda committed 2026-07-02 20:45:28 +09:00
1 parent e4d96d5b8c
commit 7a24fe7580
1 file changed
+9 -6
+9 -6
View File
@@ -93,29 +93,32 @@ func networkListAction(cmd *cobra.Command, args []string) error {
allNetworks := slices.Sorted(maps.Keys(config.Networks))
networks := []string{}
networkNames := []string{}
if len(args) > 0 {
for _, arg := range args {
matches := nameMatches(arg, allNetworks)
if len(matches) > 0 {
networks = append(networks, matches...)
networkNames = append(networkNames, matches...)
} else {
logrus.Warnf("No network matching %v found.", arg)
}
}
} else {
networks = allNetworks
networkNames = allNetworks
}
if jsonFormat {
w := cmd.OutOrStdout()
for _, name := range networks {
for _, name := range networkNames {
nw, ok := config.Networks[name]
if !ok {
logrus.Errorf("network %#q does not exist", nw)
continue
}
j, err := json.Marshal(nw)
j, err := json.Marshal(struct {
Name string `json:"name"`
networks.Network
}{Name: name, Network: nw})
if err != nil {
return err
}
@@ -126,7 +129,7 @@ func networkListAction(cmd *cobra.Command, args []string) error {
w := tabwriter.NewWriter(cmd.OutOrStdout(), 4, 8, 4, ' ', 0)
fmt.Fprintln(w, "NAME\tMODE\tGATEWAY\tINTERFACE")
for _, name := range networks {
for _, name := range networkNames {
nw, ok := config.Networks[name]
if !ok {
logrus.Errorf("network %#q does not exist", nw)