mirror of
https://github.com/lxc/incus.git
synced 2026-02-05 09:46:19 +01:00
incus/list: Add option for raw units in CSV output
Closes #2611 Signed-off-by: Mujib Ahasan <ahasanmujib8@gmail.com>
This commit is contained in:
committed by
Stéphane Graber
parent
53b02a8cac
commit
93242595f8
@@ -39,6 +39,7 @@ type cmdList struct {
|
||||
flagFast bool
|
||||
flagFormat string
|
||||
flagAllProjects bool
|
||||
rawFormat bool
|
||||
|
||||
shorthandFilters map[string]func(*api.Instance, *api.InstanceState, string) bool
|
||||
}
|
||||
@@ -409,6 +410,15 @@ func (c *cmdList) Run(cmd *cobra.Command, args []string) error {
|
||||
return errors.New(i18n.G("Can't specify --project with --all-projects"))
|
||||
}
|
||||
|
||||
// Check for raw unit formatting.
|
||||
formatFields := strings.SplitN(c.flagFormat, ",", 2)
|
||||
if len(formatFields) == 2 {
|
||||
formatOptions := strings.Split(formatFields[1], ",")
|
||||
if slices.Contains(formatOptions, "raw") {
|
||||
c.rawFormat = true
|
||||
}
|
||||
}
|
||||
|
||||
// Parse the remote
|
||||
var remote string
|
||||
var name string
|
||||
@@ -761,7 +771,11 @@ func (c *cmdList) projectColumnData(cInfo api.InstanceFull) string {
|
||||
|
||||
func (c *cmdList) memoryUsageColumnData(cInfo api.InstanceFull) string {
|
||||
if cInfo.IsActive() && cInfo.State != nil && cInfo.State.Memory.Usage > 0 {
|
||||
return units.GetByteSizeStringIEC(cInfo.State.Memory.Usage, 2)
|
||||
if !c.rawFormat {
|
||||
return units.GetByteSizeStringIEC(cInfo.State.Memory.Usage, 2)
|
||||
}
|
||||
|
||||
return strconv.FormatInt(cInfo.State.Memory.Usage, 10)
|
||||
}
|
||||
|
||||
return ""
|
||||
@@ -798,7 +812,11 @@ func (c *cmdList) diskUsageColumnData(cInfo api.InstanceFull) string {
|
||||
rootDisk, _, _ := instance.GetRootDiskDevice(cInfo.ExpandedDevices)
|
||||
|
||||
if cInfo.State != nil && cInfo.State.Disk != nil && cInfo.State.Disk[rootDisk].Usage > 0 {
|
||||
return units.GetByteSizeStringIEC(cInfo.State.Disk[rootDisk].Usage, 2)
|
||||
if !c.rawFormat {
|
||||
return units.GetByteSizeStringIEC(cInfo.State.Disk[rootDisk].Usage, 2)
|
||||
}
|
||||
|
||||
return strconv.FormatInt(cInfo.State.Disk[rootDisk].Usage, 10)
|
||||
}
|
||||
|
||||
return ""
|
||||
|
||||
@@ -181,7 +181,7 @@ func ValidateFlagFormatForListOutput(value string) error {
|
||||
options = strings.Split(fields[1], ",")
|
||||
for _, option := range options {
|
||||
switch option {
|
||||
case "noheader", "header", "":
|
||||
case "noheader", "header", "raw", "":
|
||||
default:
|
||||
return fmt.Errorf(`Invalid modifier %q on flag "--format" (%q)`, option, value)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user