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