-
Notifications
You must be signed in to change notification settings - Fork 2
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
Dima Kozlov
committed
Oct 25, 2020
1 parent
e503690
commit d990ae9
Showing
12 changed files
with
176 additions
and
24 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 |
---|---|---|
@@ -1,7 +1,2 @@ | ||
// Package git defines interface for git service | ||
// Package git implements glmt.Git | ||
package git | ||
|
||
type Git interface { | ||
Remote() (string, error) | ||
CurrentBranch() (string, error) | ||
} |
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 |
---|---|---|
@@ -1,5 +1,4 @@ | ||
// Package impl implements git service | ||
package impl | ||
package git | ||
|
||
import ( | ||
"fmt" | ||
|
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,40 @@ | ||
package glmt | ||
|
||
import "errors" | ||
|
||
func NewNestedError(wrap, cause error) error { | ||
if wrap == nil || cause == nil { | ||
panic("wrap or cause can not be nil") | ||
} | ||
|
||
return NestedError{ | ||
wrap: wrap, | ||
cause: cause, | ||
} | ||
} | ||
|
||
// NestedError is usefull when you want to wrap unknown error | ||
// with predefined error | ||
type NestedError struct { | ||
wrap error | ||
cause error | ||
} | ||
|
||
func (ne NestedError) Error() string { | ||
return ne.wrap.Error() + ": " + ne.cause.Error() | ||
} | ||
|
||
func (ne NestedError) Is(err error) bool { | ||
switch err.(type) { | ||
case NestedError: | ||
return true | ||
} | ||
|
||
return errors.Is(ne.wrap, err) || | ||
errors.Is(ne.cause, err) | ||
} | ||
|
||
func (ne NestedError) As(as interface{}) bool { | ||
return errors.As(ne.wrap, as) || | ||
errors.As(ne.cause, as) | ||
} |
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,44 @@ | ||
package glmt | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"net" | ||
"testing" | ||
) | ||
|
||
func TestNestedErrorIs(t *testing.T) { | ||
n := NewNestedError(ErrNotification, io.EOF) | ||
|
||
if !errors.Is(n, NestedError{}) { | ||
t.Fatal("error is NestedError") | ||
} | ||
|
||
if !errors.Is(n, ErrNotification) { | ||
t.Fatal("error is not ErrNotification") | ||
} | ||
|
||
if !errors.Is(n, io.EOF) { | ||
t.Fatal("error is not io.EOF") | ||
} | ||
} | ||
|
||
func TestNestedErrorAs(t *testing.T) { | ||
n := NewNestedError(&net.ParseError{}, &net.OpError{}) | ||
|
||
// NestedError is transparent | ||
var ne NestedError | ||
if !errors.As(n, &ne) { | ||
t.Fatal("error is NestedError") | ||
} | ||
|
||
pe := &net.ParseError{} | ||
if !errors.As(n, &pe) { | ||
t.Fatal("error is not ErrNotification") | ||
} | ||
|
||
ope := &net.OpError{} | ||
if !errors.As(n, &ope) { | ||
t.Fatal("error is not io.EOF") | ||
} | ||
} |
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,6 @@ | ||
package glmt | ||
|
||
type Git interface { | ||
Remote() (string, error) | ||
CurrentBranch() (string, error) | ||
} |
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,7 @@ | ||
package glmt | ||
|
||
import "context" | ||
|
||
type Notifier interface { | ||
Send(ctx context.Context, message string) error | ||
} |
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,2 @@ | ||
// Package notifier implements glmt.Notifier | ||
package notifier |
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,24 @@ | ||
package notifier | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/slack-go/slack" | ||
) | ||
|
||
func NewSlackWebHookNotifier(url string) *SlackWebHookNotifier { | ||
return &SlackWebHookNotifier{ | ||
url: url, | ||
} | ||
} | ||
|
||
type SlackWebHookNotifier struct { | ||
url string | ||
} | ||
|
||
func (sn *SlackWebHookNotifier) Send(ctx context.Context, message string) error { | ||
msg := &slack.WebhookMessage{ | ||
Text: message, | ||
} | ||
return slack.PostWebhookContext(ctx, sn.url, msg) | ||
} |