Skip to content

Commit 843691b

Browse files
weieigaoboyang9527
authored andcommitted
pluginContext's APIEndpoint() return CF API endpoint for old config file (#115)
1 parent 05d6e44 commit 843691b

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

plugin/plugin_context.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"os"
66
"path/filepath"
7+
"strconv"
8+
"strings"
79

810
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/bluemix/authentication"
911
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/bluemix/configuration/core_config"
@@ -48,6 +50,44 @@ func createPluginContext(pluginPath string, coreConfig core_config.ReadWriter) *
4850
}
4951
}
5052

53+
func (c *pluginContext) APIEndpoint() string {
54+
if compareVersion(c.SDKVersion(), "0.1.1") < 0 {
55+
return c.ReadWriter.CFConfig().APIEndpoint()
56+
}
57+
return c.ReadWriter.APIEndpoint()
58+
}
59+
60+
func compareVersion(v1, v2 string) int {
61+
s1 := strings.Split(v1, ".")
62+
s2 := strings.Split(v2, ".")
63+
64+
n := len(s1)
65+
if len(s2) > n {
66+
n = len(s2)
67+
}
68+
69+
for i := 0; i < n; i++ {
70+
var p1, p2 int
71+
if len(s1) > i {
72+
p1, _ = strconv.Atoi(s1[i])
73+
}
74+
if len(s2) > i {
75+
p2, _ = strconv.Atoi(s2[i])
76+
}
77+
if p1 > p2 {
78+
return 1
79+
}
80+
if p1 < p2 {
81+
return -1
82+
}
83+
}
84+
return 0
85+
}
86+
87+
func (c *pluginContext) HasAPIEndpoint() bool {
88+
return c.APIEndpoint() != ""
89+
}
90+
5191
func (c *pluginContext) PluginDirectory() string {
5292
return c.pluginPath
5393
}

0 commit comments

Comments
 (0)