Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Fixes #260: Adds URL checking
Browse files Browse the repository at this point in the history
  • Loading branch information
tiffanyfay committed Mar 2, 2016
1 parent 6135777 commit feb4c53
Show file tree
Hide file tree
Showing 7 changed files with 634 additions and 565 deletions.
5 changes: 5 additions & 0 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion cmd/snapctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package main

import (
"flag"
"fmt"
"os"
"sort"
"time"
Expand All @@ -33,6 +34,7 @@ var (
gitversion string
pClient *client.Client
timeFormat = time.RFC1123
err error
)

func main() {
Expand Down Expand Up @@ -90,7 +92,11 @@ func init() {
}
}
}
pClient = client.New(url, ver, secure)
pClient, err = client.New(url, ver, secure)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
resp := pClient.ListAgreements()
if resp.Err == nil {
commands = append(commands, tribeCommands...)
Expand Down
18 changes: 15 additions & 3 deletions mgmt/rest/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import (
"path/filepath"
"strings"

"github.com/asaskevich/govalidator"

"github.com/intelsdi-x/snap/mgmt/rest/rbody"
)

Expand Down Expand Up @@ -72,9 +74,20 @@ type Client struct {
prefix string
}

// Checks validity of URL
func parseURL(url string) error {
if !govalidator.IsURL(url) || !strings.HasPrefix(url, "http") {
return fmt.Errorf("URL %s is not in the format of http(s)://<ip>:<port>", url)
}
return nil
}

// New returns a pointer to a snap api client
// if ver is an empty string, v1 is used by default
func New(url, ver string, insecure bool) *Client {
func New(url, ver string, insecure bool) (*Client, error) {
if err := parseURL(url); err != nil {
return nil, err
}
if ver == "" {
ver = "v1"
}
Expand All @@ -91,8 +104,7 @@ func New(url, ver string, insecure bool) *Client {
},
}
c.prefix = url + "/" + ver
// TODO (danielscottt): assert that path is valid and target is available
return c
return c, nil
}

// String returns the string representation of the content type given a content number.
Expand Down
Loading

0 comments on commit feb4c53

Please sign in to comment.