@@ -30,6 +30,8 @@ const (
3030 instanceNameFlagName = "instance-name"
3131 destInstanceIDFlagName = "dest-instance-id"
3232 destInstanceNameFlagName = "dest-instance-name"
33+ InstanceIDListFlagName = "instance-id-list"
34+ instanceNameListFlagName = "instance-name-list"
3335)
3436
3537// ParseInstance parses the service instance id or service instance name,
@@ -44,6 +46,18 @@ func ParseInstance(required bool) func(*cli.Context) error {
4446 }
4547}
4648
49+ // ParseInstanceList parses the service instance id slice or service instance name slice,
50+ // and converts the present one to the missing one.
51+ // See flags.InstanceSliceFlags.
52+ func ParseInstanceList (required bool ) func (* cli.Context ) error {
53+ return func (ctx * cli.Context ) error {
54+ if err := ParseService (required )(ctx ); err != nil {
55+ return err
56+ }
57+ return parseInstanceList (required , InstanceIDListFlagName , instanceNameListFlagName , serviceIDFlagName )(ctx )
58+ }
59+ }
60+
4761// ParseInstanceRelation parses the source and destination service instance id or service instance name,
4862// and converts the present one to the missing one respectively.
4963// See flags.InstanceRelationFlags.
@@ -72,26 +86,85 @@ func parseInstance(required bool, idFlagName, nameFlagName, serviceIDFlagName st
7286 return nil
7387 }
7488
75- if id != "" {
76- parts := strings .Split (id , "_" )
77- if len (parts ) != 2 {
78- return fmt .Errorf ("invalid instance id, cannot be splitted into 2 parts. %v" , id )
89+ id , name , err := encode (serviceID , nameFlagName , id , name )
90+ if err != nil {
91+ return err
92+ }
93+
94+ if err := ctx .Set (idFlagName , id ); err != nil {
95+ return err
96+ }
97+ return ctx .Set (nameFlagName , name )
98+ }
99+ }
100+
101+ func parseInstanceList (required bool , idListFlagName , nameListFlagName , serviceIDFlagName string ) func (* cli.Context ) error {
102+ return func (ctx * cli.Context ) error {
103+ idsArg := ctx .String (idListFlagName )
104+ namesArgs := ctx .String (nameListFlagName )
105+ serviceID := ctx .String (serviceIDFlagName )
106+
107+ if idsArg == "" && namesArgs == "" {
108+ if required {
109+ return fmt .Errorf (`either flags "--%s" or "--%s" must be given` , idListFlagName , nameListFlagName )
110+ }
111+ return nil
112+ }
113+
114+ ids := strings .Split (idsArg , "," )
115+ names := strings .Split (namesArgs , "," )
116+ var sliceSize int
117+ if l := len (ids ); idsArg != "" && l != 0 {
118+ sliceSize = l
119+ } else {
120+ sliceSize = len (names )
121+ }
122+ instanceIDSlice := make ([]string , sliceSize )
123+ instanceNameSlice := make ([]string , sliceSize )
124+ for i := 0 ; i < sliceSize ; i ++ {
125+ id := ""
126+ name := ""
127+ if len (ids ) > i {
128+ id = ids [i ]
129+ }
130+ if len (names ) > i {
131+ name = names [i ]
79132 }
80- s , err := base64 .StdEncoding .DecodeString (parts [1 ])
133+
134+ id , name , err := encode (serviceID , nameListFlagName , id , name )
81135 if err != nil {
82136 return err
83137 }
84- name = string (s )
85- } else if name != "" {
86- if serviceID == "" {
87- return fmt .Errorf (`"--%s" is specified but its related service name or id is not given` , nameFlagName )
88- }
89- id = serviceID + "_" + b64enc (name )
138+
139+ instanceIDSlice [i ] = id
140+ instanceNameSlice [i ] = name
90141 }
91142
92- if err := ctx .Set (idFlagName , id ); err != nil {
143+ instanceIDSliceString := strings .Join (instanceIDSlice , "," )
144+ instanceNameSliceString := strings .Join (instanceNameSlice , "," )
145+ if err := ctx .Set (idListFlagName , instanceIDSliceString ); err != nil {
93146 return err
94147 }
95- return ctx .Set (nameFlagName , name )
148+ return ctx .Set (nameListFlagName , instanceNameSliceString )
149+ }
150+ }
151+
152+ func encode (serviceID , nameFlagName , id , name string ) (encodedID , encodedName string , err error ) {
153+ if id != "" {
154+ parts := strings .Split (id , "_" )
155+ if len (parts ) != 2 {
156+ return "" , "" , fmt .Errorf ("invalid instance id, cannot be splitted into 2 parts. %v" , id )
157+ }
158+ s , err := base64 .StdEncoding .DecodeString (parts [1 ])
159+ if err != nil {
160+ return "" , "" , err
161+ }
162+ name = string (s )
163+ } else if name != "" {
164+ if serviceID == "" {
165+ return "" , "" , fmt .Errorf (`"--%s" is specified but its related service name or id is not given` , nameFlagName )
166+ }
167+ id = serviceID + "_" + b64enc (name )
96168 }
169+ return id , name , nil
97170}
0 commit comments