diff --git a/cmd/edenNetwork.go b/cmd/edenNetwork.go index c30461832..d359e0886 100644 --- a/cmd/edenNetwork.go +++ b/cmd/edenNetwork.go @@ -31,16 +31,21 @@ func newNetworkCmd() *cobra.Command { } func newNetworkLsCmd() *cobra.Command { + var outputFormat types.OutputFormat //networkLsCmd is a command to list deployed network instances var networkLsCmd = &cobra.Command{ Use: "ls", Short: "List networks", Run: func(cmd *cobra.Command, args []string) { - if err := openevec.NetworkLs(); err != nil { + if err := openevec.NetworkLs(outputFormat); err != nil { log.Fatal(err) } }, } + networkLsCmd.Flags().Var( + enumflag.New(&outputFormat, "format", outputFormatIds, enumflag.EnumCaseInsensitive), + "format", + "Format to print logs, supports: lines, json") return networkLsCmd } diff --git a/cmd/edenPod.go b/cmd/edenPod.go index 30eeec26b..8fc9e7281 100644 --- a/cmd/edenPod.go +++ b/cmd/edenPod.go @@ -127,15 +127,20 @@ You can set access VLAN ID (VID) for a particular network in the format ' 1 { appStateObj.InternalIP = []string{} - appStateObj.macs = []string{} + appStateObj.Macs = []string{} for _, el := range im.GetAinfo().Network { if len(el.IPAddrs) != 0 { appStateObj.InternalIP = append(appStateObj.InternalIP, el.IPAddrs[0]) - appStateObj.macs = append(appStateObj.macs, el.MacAddr) + appStateObj.Macs = append(appStateObj.Macs, el.MacAddr) } } } else { if len(im.GetAinfo().Network[0].IPAddrs) != 0 { appStateObj.InternalIP = []string{im.GetAinfo().Network[0].IPAddrs[0]} - appStateObj.macs = []string{im.GetAinfo().Network[0].MacAddr} + appStateObj.Macs = []string{im.GetAinfo().Network[0].MacAddr} } } } else { appStateObj.InternalIP = []string{"-"} - appStateObj.macs = []string{} + appStateObj.Macs = []string{} } //check appStateObj not defined in adam if appStateObj.AdamState != inControllerConfig { @@ -222,7 +237,7 @@ func (ctx *State) processApplicationsByInfo(im *info.ZInfoMsg) { case info.ZInfoTypes_ZiNetworkInstance: //try to find ips from NetworkInstances for _, el := range im.GetNiinfo().IpAssignments { for _, appStateObj := range ctx.applications { - for ind, mac := range appStateObj.macs { + for ind, mac := range appStateObj.Macs { if mac == el.MacAddress { appStateObj.InternalIP[ind] = el.IpAddress[0] } @@ -283,8 +298,7 @@ func (ctx *State) processApplicationsByInfo(im *info.ZInfoMsg) { } } -//PodsList prints applications -func (ctx *State) PodsList() error { +func (ctx *State) printPodListLines() error { w := new(tabwriter.Writer) w.Init(os.Stdout, 0, 8, 1, '\t', 0) if _, err := fmt.Fprintln(w, appStateHeader()); err != nil { @@ -302,3 +316,23 @@ func (ctx *State) PodsList() error { } return w.Flush() } + +func (ctx *State) printPodListJSON() error { + result, err := json.MarshalIndent(ctx.Applications(), "", " ") + if err != nil { + return err + } + fmt.Println(string(result)) + return nil +} + +// PodsList prints applications +func (ctx *State) PodsList(outputFormat types.OutputFormat) error { + switch outputFormat { + case types.OutputFormatLines: + return ctx.printPodListLines() + case types.OutputFormatJSON: + return ctx.printPodListJSON() + } + return fmt.Errorf("unimplemented output format") +} diff --git a/pkg/eve/networks.go b/pkg/eve/networks.go index 419563a5e..68db58e3c 100644 --- a/pkg/eve/networks.go +++ b/pkg/eve/networks.go @@ -1,12 +1,14 @@ package eve import ( + "encoding/json" "fmt" "os" "sort" "text/tabwriter" "github.com/lf-edge/eden/pkg/controller" + "github.com/lf-edge/eden/pkg/controller/types" "github.com/lf-edge/eden/pkg/device" "github.com/lf-edge/eve/api/go/config" "github.com/lf-edge/eve/api/go/info" @@ -17,7 +19,7 @@ import ( type NetInstState struct { Name string UUID string - NetworkType config.ZNetworkInstType + NetworkType string CIDR string Stats string AdamState string @@ -51,7 +53,7 @@ func (ctx *State) initNetworks(ctrl controller.Cloud, dev *device.Ctx) error { AdamState: inControllerConfig, EveState: "UNKNOWN", CIDR: ni.Ip.Subnet, - NetworkType: ni.InstType, + NetworkType: ni.InstType.String(), } ctx.networks[ni.Uuidandversion.Uuid] = netInstStateObj } @@ -69,7 +71,7 @@ func (ctx *State) processNetworksByInfo(im *info.ZInfoMsg) { Stats: "-", AdamState: notInControllerConfig, EveState: "UNKNOWN", - NetworkType: (config.ZNetworkInstType)(int32(im.GetNiinfo().InstType)), + NetworkType: (config.ZNetworkInstType)(int32(im.GetNiinfo().InstType)).String(), } ctx.networks[im.GetNiinfo().GetNetworkID()] = netInstStateObj } @@ -120,8 +122,7 @@ func (ctx *State) processNetworksByMetric(msg *metrics.ZMetricMsg) { } } -// NetList prints networks -func (ctx *State) NetList() error { +func (ctx *State) printNetListLines() error { w := new(tabwriter.Writer) w.Init(os.Stdout, 0, 8, 1, '\t', 0) if _, err := fmt.Fprintln(w, netInstStateHeader()); err != nil { @@ -139,3 +140,23 @@ func (ctx *State) NetList() error { } return w.Flush() } + +func (ctx *State) printNetListJSON() error { + result, err := json.MarshalIndent(ctx.Networks(), "", " ") + if err != nil { + return err + } + fmt.Println(string(result)) + return nil +} + +// NetList prints networks +func (ctx *State) NetList(outputFormat types.OutputFormat) error { + switch outputFormat { + case types.OutputFormatLines: + return ctx.printNetListLines() + case types.OutputFormatJSON: + return ctx.printNetListJSON() + } + return fmt.Errorf("unimplemented output format") +} diff --git a/pkg/eve/volumes.go b/pkg/eve/volumes.go index 1ba28ac0e..571c4aaa6 100644 --- a/pkg/eve/volumes.go +++ b/pkg/eve/volumes.go @@ -1,6 +1,7 @@ package eve import ( + "encoding/json" "fmt" "os" "sort" @@ -9,18 +10,19 @@ import ( "github.com/dustin/go-humanize" "github.com/lf-edge/eden/pkg/controller" + "github.com/lf-edge/eden/pkg/controller/types" "github.com/lf-edge/eden/pkg/device" "github.com/lf-edge/eve/api/go/config" "github.com/lf-edge/eve/api/go/info" "github.com/lf-edge/eve/api/go/metrics" ) -//VolInstState stores state of volumes +// VolInstState stores state of volumes type VolInstState struct { Name string UUID string Image string - VolumeType config.Format + VolumeType string Size string MaxSize string AdamState string @@ -29,7 +31,7 @@ type VolInstState struct { Ref string contentTreeID string MountPoint string - originType config.VolumeContentOriginType + OriginType string deleted bool } @@ -86,7 +88,7 @@ func (ctx *State) initVolumes(ctrl controller.Cloud, dev *device.Ctx) error { Name: vi.GetDisplayName(), UUID: vi.GetUuid(), Image: image, - VolumeType: iFormat, + VolumeType: iFormat.String(), AdamState: inControllerConfig, EveState: "UNKNOWN", Size: "-", @@ -94,7 +96,7 @@ func (ctx *State) initVolumes(ctrl controller.Cloud, dev *device.Ctx) error { MountPoint: strings.Join(mountPoint, ";"), Ref: strings.Join(ref, ";"), contentTreeID: contentTreeID, - originType: vi.GetOrigin().GetType(), + OriginType: vi.GetOrigin().GetType().String(), } ctx.volumes[vi.GetUuid()] = volInstStateObj } @@ -121,8 +123,8 @@ func (ctx *State) processVolumesByInfo(im *info.ZInfoMsg) { } volInstStateObj.deleted = infoObject.DisplayName == "" || infoObject.State == info.ZSwState_INVALID - if volInstStateObj.VolumeType != config.Format_FmtUnknown && - volInstStateObj.VolumeType != config.Format_CONTAINER { + if volInstStateObj.VolumeType != config.Format_FmtUnknown.String() && + volInstStateObj.VolumeType != config.Format_CONTAINER.String() { //we cannot use limits for container or unknown types if infoObject.GetResources() != nil { //MaxSizeBytes to show in MAX_SIZE column @@ -136,7 +138,7 @@ func (ctx *State) processVolumesByInfo(im *info.ZInfoMsg) { } else { volInstStateObj.LastError = "" } - if volInstStateObj.originType == config.VolumeContentOriginType_VCOT_BLANK { + if volInstStateObj.OriginType == config.VolumeContentOriginType_VCOT_BLANK.String() { volInstStateObj.EveState = infoObject.GetState().String() } case info.ZInfoTypes_ZiContentTree: @@ -167,9 +169,7 @@ func (ctx *State) processVolumesByMetric(msg *metrics.ZMetricMsg) { } } } - -//VolumeList prints volumes -func (ctx *State) VolumeList() error { +func (ctx *State) printVolumeListLines() error { w := new(tabwriter.Writer) w.Init(os.Stdout, 0, 8, 1, '\t', 0) if _, err := fmt.Fprintln(w, volInstStateHeader()); err != nil { @@ -187,3 +187,23 @@ func (ctx *State) VolumeList() error { } return w.Flush() } + +func (ctx *State) printVolumeListJSON() error { + result, err := json.MarshalIndent(ctx.Volumes(), "", " ") + if err != nil { + return err + } + fmt.Println(string(result)) + return nil +} + +// VolumeList prints volumes +func (ctx *State) VolumeList(outputFormat types.OutputFormat) error { + switch outputFormat { + case types.OutputFormatLines: + return ctx.printVolumeListLines() + case types.OutputFormatJSON: + return ctx.printVolumeListJSON() + } + return fmt.Errorf("unimplemented output format") +} diff --git a/pkg/openevec/edenNetwork.go b/pkg/openevec/edenNetwork.go index 9cdbe4695..e6e153ab3 100644 --- a/pkg/openevec/edenNetwork.go +++ b/pkg/openevec/edenNetwork.go @@ -12,7 +12,7 @@ import ( log "github.com/sirupsen/logrus" ) -func NetworkLs() error { +func NetworkLs(outputFormat types.OutputFormat) error { changer := &adamChanger{} ctrl, dev, err := changer.getControllerAndDev() if err != nil { @@ -25,7 +25,7 @@ func NetworkLs() error { if err := ctrl.MetricLastCallback(dev.GetID(), nil, state.MetricCallback()); err != nil { return fmt.Errorf("fail in get MetricLastCallback: %w", err) } - if err := state.NetList(); err != nil { + if err := state.NetList(outputFormat); err != nil { return err } return nil diff --git a/pkg/openevec/edenVolume.go b/pkg/openevec/edenVolume.go index 9acf6e964..5da9426bc 100644 --- a/pkg/openevec/edenVolume.go +++ b/pkg/openevec/edenVolume.go @@ -7,6 +7,7 @@ import ( "github.com/docker/docker/pkg/namesgenerator" "github.com/dustin/go-humanize" + "github.com/lf-edge/eden/pkg/controller/types" "github.com/lf-edge/eden/pkg/eve" "github.com/lf-edge/eden/pkg/expect" "github.com/lf-edge/eden/pkg/utils" @@ -16,7 +17,7 @@ import ( "github.com/spf13/viper" ) -func VolumeLs() error { +func VolumeLs(outputFormat types.OutputFormat) error { changer := &adamChanger{} ctrl, dev, err := changer.getControllerAndDev() if err != nil { @@ -29,7 +30,7 @@ func VolumeLs() error { if err := ctrl.InfoLastCallback(dev.GetID(), nil, state.InfoCallback()); err != nil { return fmt.Errorf("fail in get InfoLastCallback: %w", err) } - if err := state.VolumeList(); err != nil { + if err := state.VolumeList(outputFormat); err != nil { return err } return nil diff --git a/pkg/openevec/pod.go b/pkg/openevec/pod.go index 918f9652f..a032a1eb4 100644 --- a/pkg/openevec/pod.go +++ b/pkg/openevec/pod.go @@ -149,7 +149,7 @@ func PodDeploy(appLink string, pc PodConfig, cfg *EdenSetupArgs) error { return nil } -func PodPs(_ *EdenSetupArgs) error { +func PodPs(_ *EdenSetupArgs, outputFormat types.OutputFormat) error { changer := &adamChanger{} ctrl, dev, err := changer.getControllerAndDev() if err != nil { @@ -162,7 +162,7 @@ func PodPs(_ *EdenSetupArgs) error { if err := ctrl.MetricLastCallback(dev.GetID(), nil, state.MetricCallback()); err != nil { return fmt.Errorf("fail in get MetricLastCallback: %w", err) } - if err := state.PodsList(); err != nil { + if err := state.PodsList(outputFormat); err != nil { return err } return nil