github.com/IBM-Blockchain/fabric-operator@v1.0.4/integration/helper/session.go (about) 1 /* 2 * Copyright contributors to the Hyperledger Fabric Operator project 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at: 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 package helper 20 21 import ( 22 "fmt" 23 "os/exec" 24 "path/filepath" 25 "strings" 26 27 "github.com/onsi/ginkgo/v2" 28 "github.com/onsi/gomega/gexec" 29 ) 30 31 var ( 32 colorIndex uint 33 ) 34 35 func AbsPath(wd string, script string) string { 36 return filepath.Join(wd, script) 37 } 38 39 func GetCommand(command string, args ...string) *exec.Cmd { 40 for _, arg := range args { 41 command = command + " " + arg 42 } 43 // Ignoring this gosec issue as this is integration test code 44 return exec.Command("bash", "-c", command) // #nosec 45 } 46 47 // StartSession executes a command session. This should be used to launch 48 // command line tools that are expected to run to completion. 49 func StartSession(cmd *exec.Cmd, name string) (*gexec.Session, error) { 50 ansiColorCode := nextColor() 51 fmt.Fprintf( 52 ginkgo.GinkgoWriter, 53 "\x1b[33m[d]\x1b[%s[%s]\x1b[0m starting %s %s with env var: %s\n", 54 ansiColorCode, 55 name, 56 filepath.Base(cmd.Args[0]), 57 strings.Join(cmd.Args[1:], " "), 58 cmd.Env, 59 ) 60 return gexec.Start( 61 cmd, 62 gexec.NewPrefixedWriter( 63 fmt.Sprintf("\x1b[32m[o]\x1b[%s[%s]\x1b[0m ", ansiColorCode, name), 64 ginkgo.GinkgoWriter, 65 ), 66 gexec.NewPrefixedWriter( 67 fmt.Sprintf("\x1b[91m[e]\x1b[%s[%s]\x1b[0m ", ansiColorCode, name), 68 ginkgo.GinkgoWriter, 69 ), 70 ) 71 } 72 73 func nextColor() string { 74 color := colorIndex%14 + 31 75 if color > 37 { 76 color = color + 90 - 37 77 } 78 79 colorIndex++ 80 return fmt.Sprintf("%dm", color) 81 }