-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
154 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"net/url" | ||
) | ||
|
||
// AddNode adds a new node to the configuration | ||
func (c Configuration) AddNode(name string, nodeURL string) error { | ||
if _, ok := c.Nodes[name]; ok { | ||
return errors.New("node" + name + " already exists. Remove it first.") | ||
} | ||
|
||
u, err := url.ParseRequestURI(nodeURL) | ||
if err != nil { | ||
return errors.New(nodeURL + " is not a valid URL. A valid URL looks like: http://hostname:port/?token=optional") | ||
} | ||
|
||
c.Nodes[name] = Node{Url: u.Scheme + "://" + u.Host, Token: u.Query().Get("token")} | ||
c.Save() | ||
|
||
return nil | ||
} | ||
|
||
// RemoveNode removes a node from the configuration | ||
func (c Configuration) RemoveNode(name string) bool { | ||
_, ok := c.Nodes[name] | ||
if ok { | ||
delete(c.Nodes, name) | ||
c.Save() | ||
} | ||
return ok | ||
} |
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,49 @@ | ||
package config | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestNodes(t *testing.T) { | ||
os.Remove("test.json") | ||
c := NewConfiguration("test.json") | ||
|
||
c.AddNode("default", "http://localhost") | ||
if value, ok := c.Nodes["default"]; ok { | ||
if value.Token != "" { | ||
t.Error("Token should not have been set") | ||
} | ||
} else { | ||
t.Error("Default has not been added") | ||
} | ||
|
||
if c.AddNode("default", "http://test123") == nil { | ||
t.Error("Should not be able to add an existing node") | ||
} | ||
|
||
c.AddNode("test1", "https://localhost:8080/?token=123") | ||
value, _ := c.Nodes["test1"] | ||
if value.Token != "123" { | ||
t.Error("Token should be 123") | ||
} | ||
if value.Url != "https://localhost:8080" { | ||
t.Error("URL is not properly set") | ||
} | ||
|
||
if c.AddNode("test2", "invalid-url") == nil { | ||
t.Error("We shouldn't be able to add invalid URLs") | ||
} | ||
|
||
if !c.RemoveNode("test1") { | ||
t.Error("Function should have returned true") | ||
} | ||
|
||
if _, ok := c.Nodes["test1"]; ok { | ||
t.Error("test1 should have been removed") | ||
} | ||
|
||
if c.RemoveNode("test1") { | ||
t.Error("Function should have returned false (already deleted)") | ||
} | ||
} |
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