code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/session_list.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package cmd 17 18 import ( 19 "context" 20 "fmt" 21 "io" 22 23 "code.vegaprotocol.io/vega/cmd/vegawallet/commands/cli" 24 "code.vegaprotocol.io/vega/cmd/vegawallet/commands/flags" 25 "code.vegaprotocol.io/vega/cmd/vegawallet/commands/printer" 26 "code.vegaprotocol.io/vega/paths" 27 "code.vegaprotocol.io/vega/wallet/service/v2/connections" 28 sessionStoreV1 "code.vegaprotocol.io/vega/wallet/service/v2/connections/store/session/v1" 29 30 "github.com/spf13/cobra" 31 ) 32 33 var ( 34 listSessionsLong = cli.LongDesc(` 35 List all the tracked sessions 36 `) 37 38 listSessionsExample = cli.Examples(` 39 # List the tracked sessions 40 {{.Software}} session list 41 `) 42 ) 43 44 type ListSessionsHandler func() ([]connections.Session, error) 45 46 func NewCmdListSessions(w io.Writer, rf *RootFlags) *cobra.Command { 47 h := func() ([]connections.Session, error) { 48 vegaPaths := paths.New(rf.Home) 49 50 sessionStore, err := sessionStoreV1.InitialiseStore(vegaPaths) 51 if err != nil { 52 return nil, fmt.Errorf("couldn't load the session store: %w", err) 53 } 54 55 return sessionStore.ListSessions(context.Background()) 56 } 57 58 return BuildCmdListSessions(w, h, rf) 59 } 60 61 func BuildCmdListSessions(w io.Writer, handler ListSessionsHandler, rf *RootFlags) *cobra.Command { 62 cmd := &cobra.Command{ 63 Use: "list", 64 Short: "List all the tracked sessions", 65 Long: listSessionsLong, 66 Example: listSessionsExample, 67 RunE: func(_ *cobra.Command, _ []string) error { 68 res, err := handler() 69 if err != nil { 70 return err 71 } 72 73 switch rf.Output { 74 case flags.InteractiveOutput: 75 printListSessions(w, res) 76 case flags.JSONOutput: 77 return printer.FprintJSON(w, res) 78 } 79 return nil 80 }, 81 } 82 83 return cmd 84 } 85 86 func printListSessions(w io.Writer, sessions []connections.Session) { 87 p := printer.NewInteractivePrinter(w) 88 89 str := p.String() 90 defer p.Print(str) 91 92 if len(sessions) == 0 { 93 str.InfoText("No session found.").NextLine() 94 return 95 } 96 97 for i, session := range sessions { 98 str.Text("- ").WarningText(session.Token.String()).NextLine() 99 str.Pad().Text("Hostname: ").WarningText(session.Hostname).NextLine() 100 str.Pad().Text("Wallet: ").WarningText(session.Wallet) 101 102 if i == len(sessions)-1 { 103 str.NextLine() 104 } else { 105 str.NextSection() 106 } 107 } 108 }