github.com/hxx258456/fabric-ca-gm@v0.0.3-0.20221111064038-a268ad7e3a37/internal/pkg/util/args.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package util 8 9 import ( 10 "fmt" 11 "os" 12 ) 13 14 const ( 15 defaultServerProtocol = "http" 16 defaultServerAddr = "localhost" 17 defaultServerPort = "7054" 18 ) 19 20 // GetServerURL returns the server's URL 21 func GetServerURL() string { 22 return fmt.Sprintf("%s://%s:%s", getServerProtocol(), getServerAddr(), GetServerPort()) 23 } 24 25 // GetServerPort returns the server's listening port 26 func GetServerPort() string { 27 port := getCommandLineOptValue("-port") 28 if port != "" { 29 return port 30 } 31 return defaultServerPort 32 } 33 34 // getCommandLineOptValue searches the command line arguments for the 35 // specified option and returns the following value if found; otherwise 36 // it returns "". 37 // For example, if command line is: 38 // fabric-ca client enroll -config myconfig.json 39 // getCommandLineOptValue("-config") returns "myconfig.json" 40 func getCommandLineOptValue(optName string) string { 41 for i := 0; i < len(os.Args)-1; i++ { 42 if os.Args[i] == optName { 43 val := os.Args[i+1] 44 return val 45 } 46 } 47 return "" 48 } 49 50 // getServerProtocol returns the server's protocol 51 func getServerProtocol() string { 52 protocol := getCommandLineOptValue("-protocol") 53 if protocol != "" { 54 return protocol 55 } 56 return defaultServerProtocol 57 } 58 59 // getServerAddr returns the server's address 60 func getServerAddr() string { 61 addr := getCommandLineOptValue("-address") 62 if addr != "" { 63 return addr 64 } 65 return defaultServerAddr 66 } 67 68 // setDefaultServerPort overrides the default CFSSL server port 69 // by adding the "-port" option to the command line if it was not 70 // already present. 71 func setDefaultServerPort() { 72 if len(os.Args) > 2 && getCommandLineOptValue("-port") == "" { 73 os.Args = append(os.Args, "-port", defaultServerPort) 74 } 75 }