@@ -3,9 +3,11 @@ package cmd
33import (
44 "context"
55 "fmt"
6+ "math"
67 "os"
78 "strings"
89
10+ "github.com/c2h5oh/datasize"
911 "github.com/kernel/hypeman-go"
1012 "github.com/kernel/hypeman-go/option"
1113 "github.com/tidwall/gjson"
@@ -39,10 +41,15 @@ Examples:
3941var resourcesReclaimMemoryCmd = cli.Command {
4042 Name : "reclaim-memory" ,
4143 Usage : "Request guest memory reclaim from reclaim-eligible instances" ,
44+ Description : `Request guest memory reclaim across eligible instances.
45+
46+ Examples:
47+ hypeman resources reclaim-memory --reclaim-bytes 512MB --dry-run
48+ hypeman resources reclaim-memory --reclaim-bytes 1073741824 --hold-for 10m --reason "pack host before launch"` ,
4249 Flags : []cli.Flag {
43- & cli.Int64Flag {
50+ & cli.StringFlag {
4451 Name : "reclaim-bytes" ,
45- Usage : " Total bytes of guest memory to reclaim across eligible VMs" ,
52+ Usage : ` Total guest memory to reclaim (e.g., "512MB", "2GB", or "1048576" for raw bytes)` ,
4653 Required : true ,
4754 },
4855 & cli.BoolFlag {
@@ -93,9 +100,9 @@ func handleResources(ctx context.Context, cmd *cli.Command) error {
93100func handleResourcesReclaimMemory (ctx context.Context , cmd * cli.Command ) error {
94101 client := hypeman .NewClient (getDefaultRequestOptions (cmd )... )
95102
96- reclaimBytes := cmd .Int64 ("reclaim-bytes" )
97- if reclaimBytes <= 0 {
98- return fmt . Errorf ( "reclaim-bytes must be greater than 0" )
103+ reclaimBytes , err := parseReclaimBytes ( cmd .String ("reclaim-bytes" ) )
104+ if err != nil {
105+ return err
99106 }
100107
101108 request := hypeman.MemoryReclaimRequestParam {
@@ -122,7 +129,7 @@ func handleResourcesReclaimMemory(ctx context.Context, cmd *cli.Command) error {
122129
123130 var res []byte
124131 opts = append (opts , option .WithResponseBodyInto (& res ))
125- _ , err : = client .Resources .ReclaimMemory (ctx , params , opts ... )
132+ _ , err = client .Resources .ReclaimMemory (ctx , params , opts ... )
126133 if err != nil {
127134 return err
128135 }
@@ -143,6 +150,25 @@ func handleResourcesReclaimMemory(ctx context.Context, cmd *cli.Command) error {
143150 return ShowJSON (os .Stdout , "resources reclaim-memory" , obj , format , transform )
144151}
145152
153+ func parseReclaimBytes (raw string ) (int64 , error ) {
154+ if raw == "" {
155+ return 0 , fmt .Errorf ("reclaim-bytes is required" )
156+ }
157+
158+ var size datasize.ByteSize
159+ if err := size .UnmarshalText ([]byte (raw )); err != nil {
160+ return 0 , fmt .Errorf ("invalid reclaim-bytes %q: %w" , raw , err )
161+ }
162+ if size == 0 {
163+ return 0 , fmt .Errorf ("reclaim-bytes must be greater than 0" )
164+ }
165+ if size .Bytes () > math .MaxInt64 {
166+ return 0 , fmt .Errorf ("reclaim-bytes %q exceeds the maximum supported size" , raw )
167+ }
168+
169+ return int64 (size .Bytes ()), nil
170+ }
171+
146172func showResourcesTable (data []byte ) error {
147173 obj := gjson .ParseBytes (data )
148174
0 commit comments