github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/supervisor/client/client.go (about) 1 /* 2 * Copyright (C) 2020 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 client 19 20 import ( 21 "bufio" 22 "errors" 23 "fmt" 24 "strings" 25 26 "github.com/rs/zerolog/log" 27 ) 28 29 // Command executes supervisor command. 30 func Command(args ...string) (result string, err error) { 31 cmdLine := strings.Join(args, " ") 32 log.Trace().Msgf("Supervisor command invoked: %q", cmdLine) 33 conn, err := connect() 34 if err != nil { 35 return "", err 36 } 37 defer conn.Close() 38 39 _, err = fmt.Fprintln(conn, cmdLine) 40 if err != nil { 41 return "", err 42 } 43 44 scanner := bufio.NewScanner(conn) 45 scanner.Scan() 46 line := string(scanner.Bytes()) 47 parts := strings.SplitN(line, ": ", 2) 48 status := parts[0] 49 if status == "ok" { 50 if len(parts) > 1 { 51 result = parts[1] 52 } 53 return result, nil 54 } 55 56 message := strings.TrimPrefix(line, "error: ") 57 return "", errors.New(message) 58 }