Skip to content

Commit

Permalink
feat!: remove omit scheme support and enhance error message
Browse files Browse the repository at this point in the history
  • Loading branch information
macrat committed Apr 10, 2021
1 parent 2abefd4 commit dea4240
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
20 changes: 15 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,17 @@ func Usage() {
fmt.Fprintf(out, "\n")
fmt.Fprintf(out, "EXAMPLES:\n")
fmt.Fprintf(out, " Send ping to example.com in default interval(5m):\n")
fmt.Fprintf(out, " $ %s example.com\n", os.Args[0])
fmt.Fprintf(out, " $ %s ping:example.com\n", os.Args[0])
fmt.Fprintf(out, "\n")
fmt.Fprintf(out, " Send ping to example.com every minutes:\n")
fmt.Fprintf(out, " $ %s 1m example.com\n", os.Args[0])
fmt.Fprintf(out, " $ %s 1m ping:example.com\n", os.Args[0])
fmt.Fprintf(out, "\n")
fmt.Fprintf(out, " Access to http://example.com every half hours:\n")
fmt.Fprintf(out, " $ %s 30m http://example.com\n", os.Args[0])
fmt.Fprintf(out, "\n")
fmt.Fprintf(out, " Check a.local(ping) and b.local(http) every minutes,\n")
fmt.Fprintf(out, " and check c.local every 15 minutes:\n")
fmt.Fprintf(out, " $ %s 1m a.local http://b.local 15m ping:c.local\n", os.Args[0])
fmt.Fprintf(out, " and execute ./check.sh command every 15 minutes:\n")
fmt.Fprintf(out, " $ %s 1m ping:a.local http://b.local 15m exec:./check.sh\n", os.Args[0])
fmt.Fprintf(out, "\n")
fmt.Fprintf(out, " Listen on http://0.0.0.0:8080 (and connect to example.com:3306 for check):\n")
fmt.Fprintf(out, " $ %s -l 8080 1m tcp:example.com:3306\n", os.Args[0])
Expand All @@ -95,6 +95,14 @@ func ParseArgs(args []string) ([]Task, []error) {

p, err := probe.Get(a)
if err != nil {
switch err {
case probe.UnsupportedSchemeError:
err = fmt.Errorf("%s: This scheme is not supported.", a)
case probe.MissingSchemeError:
err = fmt.Errorf("%s: Not valid as schedule or target URI. Please specify scheme if this is target. (e.g. ping:%s or http://%s)", a, a, a)
default:
err = fmt.Errorf("%s: Not valid as schedule or target URI.", a)
}
errors = append(errors, err)
continue
}
Expand Down Expand Up @@ -173,9 +181,11 @@ func main() {

tasks, errors := ParseArgs(flag.Args())
if errors != nil {
fmt.Fprintln(os.Stderr, "Invalid argument:")
for _, err := range errors {
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(os.Stderr, "", err)
}
fmt.Fprintf(os.Stderr, "\nPlease see `%s -h` for more information.\n", os.Args[0])
os.Exit(2)
}
if len(tasks) == 0 {
Expand Down
4 changes: 2 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ func TestParseArgs(t *testing.T) {
Want []WantTask
}{
{
Args: []string{"hoge", "2m", "http://example.com"},
Args: []string{"ping:hoge", "2m", "http://example.com"},
Want: []WantTask{
{"5m0s", "ping:hoge"},
{"2m0s", "http://example.com"},
},
},
{
Args: []string{"hoge", "fuga", "2m", "1h", "http://example.com"},
Args: []string{"ping:hoge", "ping://fuga", "2m", "1h", "http://example.com"},
Want: []WantTask{
{"5m0s", "ping:hoge"},
{"5m0s", "ping:fuga"},
Expand Down
17 changes: 10 additions & 7 deletions probe/probe.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package probe

import (
"fmt"
"errors"
"net/url"

"github.com/macrat/ayd/store"
)

var (
InvalidURIError = errors.New("invalid URI")
MissingSchemeError = errors.New("missing scheme")
UnsupportedSchemeError = errors.New("unsupported scheme")
)

type Probe interface {
Target() *url.URL
Check() store.Record
Expand All @@ -32,19 +38,16 @@ func GetByURL(u *url.URL) Probe {
func Get(rawURL string) (Probe, error) {
u, err := url.Parse(rawURL)
if err != nil {
return nil, fmt.Errorf("invalid target: %s", rawURL)
return nil, InvalidURIError
}

if u.Scheme == "" {
u, err = url.Parse("ping:" + rawURL)
if err != nil {
return nil, fmt.Errorf("invalid target: %s", rawURL)
}
return nil, MissingSchemeError
}

p := GetByURL(u)
if p == nil {
return nil, fmt.Errorf("unsupported scheme: %#v", u.Scheme)
return nil, UnsupportedSchemeError
}

return p, nil
Expand Down
2 changes: 0 additions & 2 deletions probe/probe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ func TestTargetURLNormalize(t *testing.T) {
Input string
Want url.URL
}{
{"example.com", url.URL{Scheme: "ping", Opaque: "example.com"}},

{"ping:example.com", url.URL{Scheme: "ping", Opaque: "example.com"}},
{"ping://example.com:123/foo/bar?hoge=fuga#piyo", url.URL{Scheme: "ping", Opaque: "example.com"}},

Expand Down

0 comments on commit dea4240

Please sign in to comment.