@@ -336,10 +336,6 @@ func (sc *subcommandContext) tunnelActive(name string) (*tunnelstore.Tunnel, boo
336336 filter := tunnelstore .NewFilter ()
337337 filter .NoDeleted ()
338338 filter .ByName (name )
339- if maxFetch := sc .c .Uint ("max-fetch-size" ); maxFetch > 0 {
340- filter .MaxFetchSize (maxFetch )
341- }
342-
343339 tunnels , err := sc .list (filter )
344340 if err != nil {
345341 return nil , false , err
@@ -377,56 +373,42 @@ func (sc *subcommandContext) findID(input string) (uuid.UUID, error) {
377373}
378374
379375// findIDs is just like mapping `findID` over a slice, but it only uses
380- // one Tunnelstore API call.
376+ // one Tunnelstore API call per non-UUID input provided .
381377func (sc * subcommandContext ) findIDs (inputs []string ) ([]uuid.UUID , error ) {
378+ uuids , names := splitUuids (inputs )
382379
383- // Shortcut without Tunnelstore call if we find that all inputs are already UUIDs.
384- uuids , err := convertNamesToUuids (inputs , make (map [string ]uuid.UUID ))
385- if err == nil {
386- return uuids , nil
387- }
380+ for _ , name := range names {
381+ filter := tunnelstore .NewFilter ()
382+ filter .NoDeleted ()
383+ filter .ByName (name )
388384
389- // First, look up all tunnels the user has
390- filter := tunnelstore .NewFilter ()
391- filter .NoDeleted ()
392- if maxFetch := sc .c .Uint ("max-fetch-size" ); maxFetch > 0 {
393- filter .MaxFetchSize (maxFetch )
394- }
385+ tunnels , err := sc .list (filter )
386+ if err != nil {
387+ return nil , err
388+ }
395389
396- tunnels , err := sc .list (filter )
397- if err != nil {
398- return nil , err
399- }
400- // Do the pure list-processing in its own function, so that it can be
401- // unit tested easily.
402- return findIDs (tunnels , inputs )
403- }
390+ if len (tunnels ) != 1 {
391+ return nil , fmt .Errorf ("there should only be 1 non-deleted Tunnel named %s" , name )
392+ }
404393
405- func findIDs (tunnels []* tunnelstore.Tunnel , inputs []string ) ([]uuid.UUID , error ) {
406- // Put them into a dictionary for faster lookups
407- nameToID := make (map [string ]uuid.UUID , len (tunnels ))
408- for _ , tunnel := range tunnels {
409- nameToID [tunnel .Name ] = tunnel .ID
394+ uuids = append (uuids , tunnels [0 ].ID )
410395 }
411396
412- return convertNamesToUuids ( inputs , nameToID )
397+ return uuids , nil
413398}
414399
415- func convertNamesToUuids (inputs []string , nameToID map [ string ]uuid. UUID ) ([]uuid.UUID , error ) {
416- tunnelIDs := make ([]uuid.UUID , len ( inputs ) )
417- var badInputs []string
418- for i , input := range inputs {
419- if id , err := uuid . Parse ( input ); err == nil {
420- tunnelIDs [ i ] = id
421- } else if id , ok := nameToID [ input ]; ok {
422- tunnelIDs [ i ] = id
400+ func splitUuids (inputs []string ) ([]uuid.UUID , [] string ) {
401+ uuids := make ([]uuid.UUID , 0 )
402+ names := make ( []string , 0 )
403+
404+ for _ , input := range inputs {
405+ id , err := uuid . Parse ( input )
406+ if err != nil {
407+ names = append ( names , input )
423408 } else {
424- badInputs = append (badInputs , input )
409+ uuids = append (uuids , id )
425410 }
426411 }
427- if len (badInputs ) > 0 {
428- msg := "Please specify either the ID or name of a tunnel. The following inputs were neither: %s"
429- return nil , fmt .Errorf (msg , strings .Join (badInputs , ", " ))
430- }
431- return tunnelIDs , nil
412+
413+ return uuids , names
432414}
0 commit comments