github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/cmd/commands/cli/command_service.go (about) 1 /* 2 * Copyright (C) 2021 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package cli 19 20 import ( 21 "fmt" 22 23 "github.com/mysteriumnetwork/node/cmd/commands/cli/clio" 24 "github.com/mysteriumnetwork/node/datasize" 25 "github.com/mysteriumnetwork/node/money" 26 "github.com/mysteriumnetwork/node/tequilapi/contract" 27 ) 28 29 func (c *cliApp) service(args []string) (err error) { 30 if len(args) == 0 { 31 fmt.Println(serviceHelp) 32 return errWrongArgumentCount 33 } 34 35 action := args[0] 36 switch action { 37 case "start": 38 if len(args) < 3 { 39 fmt.Println(serviceHelp) 40 return errWrongArgumentCount 41 } 42 return c.serviceStart(args[1], args[2], args[3:]...) 43 case "stop": 44 if len(args) < 2 { 45 fmt.Println(serviceHelp) 46 return errWrongArgumentCount 47 } 48 return c.serviceStop(args[1]) 49 case "status": 50 if len(args) < 2 { 51 fmt.Println(serviceHelp) 52 return errWrongArgumentCount 53 } 54 return c.serviceGet(args[1]) 55 case "list": 56 return c.serviceList() 57 case "sessions": 58 return c.serviceSessions() 59 default: 60 fmt.Println(serviceHelp) 61 return errUnknownSubCommand(args[0]) 62 } 63 } 64 65 func (c *cliApp) serviceStart(providerID, serviceType string, args ...string) (err error) { 66 serviceOpts, err := parseStartFlags(serviceType, args...) 67 if err != nil { 68 return fmt.Errorf("failed to parse service options: %w", err) 69 } 70 71 service, err := c.tequilapi.ServiceStart(contract.ServiceStartRequest{ 72 ProviderID: providerID, 73 Type: serviceType, 74 AccessPolicies: &contract.ServiceAccessPolicies{IDs: serviceOpts.AccessPolicyList}, 75 Options: serviceOpts.TypeOptions, 76 }) 77 if err != nil { 78 return fmt.Errorf("failed to start service: %w", err) 79 } 80 81 clio.Status(service.Status, 82 "ID: "+service.ID, 83 "ProviderID: "+service.Proposal.ProviderID, 84 "Type: "+service.Proposal.ServiceType) 85 return nil 86 } 87 88 func (c *cliApp) serviceStop(id string) (err error) { 89 if err := c.tequilapi.ServiceStop(id); err != nil { 90 return fmt.Errorf("failed to stop service: %w", err) 91 } 92 93 clio.Status("Stopping", "ID: "+id) 94 return nil 95 } 96 97 func (c *cliApp) serviceList() (err error) { 98 services, err := c.tequilapi.Services() 99 if err != nil { 100 return fmt.Errorf("failed to get a list of services: %w", err) 101 } 102 103 for _, service := range services { 104 clio.Status(service.Status, 105 "ID: "+service.ID, 106 "ProviderID: "+service.Proposal.ProviderID, 107 "Type: "+service.Proposal.ServiceType) 108 } 109 return nil 110 } 111 112 func (c *cliApp) serviceSessions() (err error) { 113 sessions, err := c.tequilapi.Sessions() 114 if err != nil { 115 return fmt.Errorf("failed to get a list of sessions: %w", err) 116 } 117 118 clio.Status("Current sessions", len(sessions.Items)) 119 for _, session := range sessions.Items { 120 clio.Status( 121 "ID: "+session.ID, 122 "ConsumerID: "+session.ConsumerID, 123 fmt.Sprintf("Data: %s/%s", datasize.FromBytes(session.BytesReceived).String(), datasize.FromBytes(session.BytesSent).String()), 124 fmt.Sprintf("Tokens: %s", money.New(session.Tokens)), 125 ) 126 } 127 return nil 128 } 129 130 func (c *cliApp) serviceGet(id string) (err error) { 131 service, err := c.tequilapi.Service(id) 132 if err != nil { 133 return fmt.Errorf("failed to get service info: %w", err) 134 } 135 136 clio.Status(service.Status, 137 "ID: "+service.ID, 138 "ProviderID: "+service.Proposal.ProviderID, 139 "Type: "+service.Proposal.ServiceType) 140 return nil 141 }