Skip to content

Commit

Permalink
implement -a flag on skywire-cli visor tp rm -a (--all) (#1726)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrpalide authored Feb 5, 2024
1 parent f319585 commit fa090e6
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 17 deletions.
29 changes: 12 additions & 17 deletions cmd/skywire-cli/commands/visor/transports.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,25 +203,20 @@ var rmTpCmd = &cobra.Command{
Long: "\n Remove transport(s) by id",
DisableFlagsInUseLine: true,
Run: func(cmd *cobra.Command, args []string) {
//TODO
//if removeAll {
// var pks cipher.PubKeys
// internal.Catch(cmd.Flags(), pks.Set(strings.Join(filterPubKeys, ",")))
// tID, err := clirpc.Client(cmd.Flags()).Transports(filterTypes, pks, showLogs)
// internal.Catch(cmd.Flags(), err)
// internal.Catch(cmd.Flags(), clirpc.Client(cmd.Flags()).RemoveTransport(tID))
//} else {
if args[0] != "" {
tpID = args[0]
}
tID := internal.ParseUUID(cmd.Flags(), "transport-id", tpID)
rpcClient, err := clirpc.Client(cmd.Flags())
if err != nil {
os.Exit(1)
if removeAll {
internal.Catch(cmd.Flags(), rpcClient.RemoveAllTransports())
internal.PrintOutput(cmd.Flags(), "OK", "OK\n")
} else if tpID != "" {
tID := internal.ParseUUID(cmd.Flags(), "transport-id", tpID)
if err != nil {
os.Exit(1)
}
internal.Catch(cmd.Flags(), rpcClient.RemoveTransport(tID))
internal.PrintOutput(cmd.Flags(), "OK", "OK\n")
} else {
internal.PrintOutput(cmd.Flags(), "", cmd.Help())
}
internal.Catch(cmd.Flags(), rpcClient.RemoveTransport(tID))
internal.PrintOutput(cmd.Flags(), "OK", "OK\n")
//}
},
}

Expand Down
16 changes: 16 additions & 0 deletions pkg/transport/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,22 @@ func (tm *Manager) DeleteTransport(id uuid.UUID) {
}
}

// DeleteAllTransports deregisters all Transports in transport discovery and deletes them locally.
func (tm *Manager) DeleteAllTransports() {
tm.mx.Lock()
defer tm.mx.Unlock()

if tm.isClosing() {
return
}

// Deregister transports before closing the underlying transport.
for _, tp := range tm.tps {
tp.close()
delete(tm.tps, tp.Entry.ID)
}
}

// ReadPacket reads data packets from routes.
func (tm *Manager) ReadPacket() (routing.Packet, error) {
p, ok := <-tm.readCh
Expand Down
7 changes: 7 additions & 0 deletions pkg/visor/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ type API interface {
Transport(tid uuid.UUID) (*TransportSummary, error)
AddTransport(remote cipher.PubKey, tpType string, timeout time.Duration) (*TransportSummary, error)
RemoveTransport(tid uuid.UUID) error
RemoveAllTransports() error
SetPublicAutoconnect(pAc bool) error
GetPersistentTransports() ([]transport.PersistentTransports, error)
SetPersistentTransports([]transport.PersistentTransports) error
Expand Down Expand Up @@ -1192,6 +1193,12 @@ func (v *Visor) RemoveTransport(tid uuid.UUID) error {
return nil
}

// RemoveAllTransports implements API
func (v *Visor) RemoveAllTransports() error {
v.tpM.DeleteAllTransports()
return nil
}

// DiscoverTransportsByPK implements API.
func (v *Visor) DiscoverTransportsByPK(pk cipher.PubKey) ([]*transport.Entry, error) {
tpD := v.tpDiscClient()
Expand Down
7 changes: 7 additions & 0 deletions pkg/visor/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,13 @@ func (r *RPC) RemoveTransport(tid *uuid.UUID, _ *struct{}) (err error) {
return r.visor.RemoveTransport(*tid)
}

// RemoveAllTransports removes all Transports from the visor.
func (r *RPC) RemoveAllTransports(_ *struct{}, _ *struct{}) (err error) {
defer rpcutil.LogCall(r.log, "RemoveAllTransports", nil)(nil, &err)

return r.visor.RemoveAllTransports()
}

/*
<<< AVAILABLE TRANSPORTS >>>
*/
Expand Down
13 changes: 13 additions & 0 deletions pkg/visor/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,11 @@ func (rc *rpcClient) RemoveTransport(tid uuid.UUID) error {
return rc.Call("RemoveTransport", &tid, &struct{}{})
}

// RemoveAllTransports calls RemoveAllTransports.
func (rc *rpcClient) RemoveAllTransports() error {
return rc.Call("RemoveAllTransports", &struct{}{}, &struct{}{})
}

func (rc *rpcClient) DiscoverTransportsByPK(pk cipher.PubKey) ([]*transport.Entry, error) {
entries := make([]*transport.Entry, 0)
err := rc.Call("DiscoverTransportsByPK", &pk, &entries)
Expand Down Expand Up @@ -1158,6 +1163,14 @@ func (mc *mockRPCClient) RemoveTransport(tid uuid.UUID) error {
})
}

// RemoveAllTransports implements API.
func (mc *mockRPCClient) RemoveAllTransports() error {
return mc.do(true, func() error {
mc.o.Transports = []*TransportSummary{}
return nil
})
}

func (mc *mockRPCClient) DiscoverTransportsByPK(cipher.PubKey) ([]*transport.Entry, error) {
return nil, ErrNotImplemented
}
Expand Down

0 comments on commit fa090e6

Please sign in to comment.