This repository has been archived by the owner on Dec 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 773
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #590 from lowzj/0.3-node
feature: parse the port of supernode from --node flag of dfget
- Loading branch information
Showing
10 changed files
with
162 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright The Dragonfly Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package util | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// ExtractHost extracts host ip from the giving string. | ||
func ExtractHost(hostAndPort string) string { | ||
fields := strings.Split(strings.TrimSpace(hostAndPort), ":") | ||
return fields[0] | ||
} | ||
|
||
// GetIPAndPortFromNode return ip and port by parsing the node value. | ||
// It will return defaultPort as the value of port | ||
// when the node is a string without port or with an illegal port. | ||
func GetIPAndPortFromNode(node string, defaultPort int) (string, int) { | ||
if IsEmptyStr(node) { | ||
return "", defaultPort | ||
} | ||
|
||
nodeFields := strings.Split(node, ":") | ||
switch len(nodeFields) { | ||
case 1: | ||
return nodeFields[0], defaultPort | ||
case 2: | ||
port, err := strconv.Atoi(nodeFields[1]) | ||
if err != nil { | ||
return nodeFields[0], defaultPort | ||
} | ||
return nodeFields[0], port | ||
default: | ||
return "", defaultPort | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright The Dragonfly Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package util | ||
|
||
import ( | ||
"github.com/go-check/check" | ||
) | ||
|
||
type UtilSuite struct{} | ||
|
||
func init() { | ||
check.Suite(&UtilSuite{}) | ||
} | ||
|
||
func (suite *UtilSuite) TestExtractHost(c *check.C) { | ||
host := ExtractHost("1:0") | ||
c.Assert(host, check.Equals, "1") | ||
} | ||
|
||
func (suite *UtilSuite) TestGetIPAndPortFromNode(c *check.C) { | ||
var cases = []struct { | ||
node string | ||
defaultPort int | ||
expectedIP string | ||
expectedPort int | ||
}{ | ||
{"127.0.0.1", 8002, "127.0.0.1", 8002}, | ||
{"127.0.0.1:8001", 8002, "127.0.0.1", 8001}, | ||
{"127.0.0.1:abcd", 8002, "127.0.0.1", 8002}, | ||
} | ||
|
||
for _, v := range cases { | ||
ip, port := GetIPAndPortFromNode(v.node, v.defaultPort) | ||
c.Check(ip, check.Equals, v.expectedIP) | ||
c.Check(port, check.Equals, v.expectedPort) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters