@@ -12,6 +12,7 @@ import (
1212 "net/url"
1313 "os"
1414 "path"
15+ "strconv"
1516 "strings"
1617 "sync"
1718
@@ -25,6 +26,33 @@ import (
2526 "gitlab.com/postgres-ai/database-lab/v3/pkg/models"
2627)
2728
29+ // parseProtectedFlag parses the --protected flag value.
30+ // Returns (isProtected, durationMinutes, error).
31+ // durationMinutes is nil if default duration should be used.
32+ func parseProtectedFlag (cliCtx * cli.Context ) (bool , * uint , error ) {
33+ if ! cliCtx .IsSet ("protected" ) {
34+ return false , nil , nil
35+ }
36+
37+ value := cliCtx .String ("protected" )
38+
39+ switch strings .ToLower (value ) {
40+ case "" , "true" :
41+ return true , nil , nil
42+ case "false" :
43+ return false , nil , nil
44+ default :
45+ duration , err := strconv .ParseUint (value , 10 , 32 )
46+ if err != nil {
47+ return false , nil , errors .Errorf ("invalid --protected value: %q (use 'true', 'false', or minutes)" , value )
48+ }
49+
50+ d := uint (duration )
51+
52+ return true , & d , nil
53+ }
54+ }
55+
2856// list runs a request to list clones of an instance.
2957func list (cliCtx * cli.Context ) error {
3058 dblabClient , err := commands .ClientByCLIContext (cliCtx )
@@ -96,9 +124,15 @@ func create(cliCtx *cli.Context) error {
96124 return err
97125 }
98126
127+ isProtected , protectionDuration , err := parseProtectedFlag (cliCtx )
128+ if err != nil {
129+ return err
130+ }
131+
99132 cloneRequest := types.CloneCreateRequest {
100- ID : cliCtx .String ("id" ),
101- Protected : cliCtx .Bool ("protected" ),
133+ ID : cliCtx .String ("id" ),
134+ Protected : isProtected ,
135+ ProtectionDurationMinutes : protectionDuration ,
102136 DB : & types.DatabaseRequest {
103137 Username : cliCtx .String ("username" ),
104138 Password : cliCtx .String ("password" ),
@@ -184,8 +218,14 @@ func update(cliCtx *cli.Context) error {
184218 return err
185219 }
186220
221+ isProtected , protectionDuration , err := parseProtectedFlag (cliCtx )
222+ if err != nil {
223+ return err
224+ }
225+
187226 updateRequest := types.CloneUpdateRequest {
188- Protected : cliCtx .Bool ("protected" ),
227+ Protected : isProtected ,
228+ ProtectionDurationMinutes : protectionDuration ,
189229 }
190230
191231 cloneID := cliCtx .Args ().First ()
0 commit comments