Skip to content

Commit

Permalink
use context in registry
Browse files Browse the repository at this point in the history
  • Loading branch information
linyows committed Nov 7, 2024
1 parent af293dd commit ad23965
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 65 deletions.
1 change: 1 addition & 0 deletions artifact/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"log"
"net/url"
"os"
"strings"

"github.com/aws/aws-sdk-go-v2/config"
Expand Down
25 changes: 12 additions & 13 deletions dewy.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,9 @@ func New(c Config) (*Dewy, error) {
}
c.Registry = fmt.Sprintf("%s://%s", su[0], u.String())

r, err := registry.New(c.Registry)
if err != nil {
return nil, err
}

n, err := notify.New(c.Notify)
if err != nil {
return nil, err
}

return &Dewy{
config: c,
cache: kv,
registry: r,
notify: n,
isServerRunning: false,
root: wd,
}, nil
Expand All @@ -94,9 +82,20 @@ func (d *Dewy) Start(i int) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

var err error

d.registry, err = registry.New(ctx, d.config.Registry)
if err != nil {
log.Printf("[ERROR] Registry failure: %#v", err)
}

d.notify, err = notify.New(ctx, d.config.Notify)
if err != nil {
log.Printf("[ERROR] Notify failure: %#v", err)
}

d.notify.Send(ctx, "Automatic shipping started by *Dewy*")

var err error
d.job, err = scheduler.Every(i).Seconds().Run(func() {
e := d.Run()
if e != nil {
Expand Down
4 changes: 3 additions & 1 deletion dewy_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dewy

import (
"context"
"os"
"path/filepath"
"testing"
Expand All @@ -25,7 +26,8 @@ func TestNew(t *testing.T) {
t.Fatal(err)
}
wd, _ := os.Getwd()
r, err := registry.New(reg)
ctx := context.Background()
r, err := registry.New(ctx, reg)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Notify interface {
}

// New returns Notice.
func New(url string) (Notify, error) {
func New(ctx context.Context, url string) (Notify, error) {
splitted := strings.SplitN(url, "://", 2)

switch splitted[0] {
Expand Down
31 changes: 23 additions & 8 deletions registry/ghr.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,33 @@ type GHR struct {
cl *github.Client
}

// New returns GithubRelease.
func NewGHR(owner, repo string) (*GHR, error) {
cl, err := factory.NewGithubClient()
// New returns GHR.
func NewGHR(ctx context.Context, path string) (*GHR, error) {
u, err := url.Parse(path)
if err != nil {
return nil, err
}

return &GHR{
Owner: owner,
Repo: repo,
cl: cl,
}, nil
arr := strings.SplitN(u.Path, "/", 2)
if len(arr) < 2 {
return nil, fmt.Errorf("owner and repository is required")
}

ghr := &GHR{
Owner: arr[0],
Repo: arr[1],
}

if err := decoder.Decode(ghr, u.Query()); err != nil {
return nil, err
}

ghr.cl, err = factory.NewGithubClient()
if err != nil {
return nil, err
}

return ghr, nil
}

// String to string.
Expand Down
1 change: 1 addition & 0 deletions registry/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package registry

import (
"context"
"net/url"

pb "github.com/linyows/dewy/registry/gen/dewy"
"google.golang.org/grpc"
Expand Down
4 changes: 2 additions & 2 deletions registry/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestCurrent(t *testing.T) {
ArtifactUrl: "ghr://linyows/dewy",
})
g := &GRPC{NoTLS: true}
if err := g.Dial(ts.Addr()); err != nil {
if err := g.Dial(ctx, ts.Addr()); err != nil {
t.Fatal(err)
}
req := &CurrentRequest{
Expand Down Expand Up @@ -68,7 +68,7 @@ func TestReport(t *testing.T) {
})
ts.Method("Report").Response(&emptypb.Empty{})
g := &GRPC{NoTLS: true}
if err := g.Dial(ts.Addr()); err != nil {
if err := g.Dial(ctx, ts.Addr()); err != nil {
t.Fatal(err)
}
req := &ReportRequest{
Expand Down
40 changes: 4 additions & 36 deletions registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package registry
import (
"context"
"fmt"
"net/url"
"strings"

"github.com/gorilla/schema"
Expand Down Expand Up @@ -55,49 +54,18 @@ type ReportRequest struct {
Err error
}

func New(strUrl string) (Registry, error) {
func New(ctx context.Context, strUrl string) (Registry, error) {
splitted := strings.SplitN(strUrl, "://", 2)

switch splitted[0] {
case ghrScheme:
u, err := url.Parse(splitted[1])
if err != nil {
return nil, err
}

ownerrepo := strings.SplitN(u.Path, "/", 2)
gr, err := NewGHR(ownerrepo[0], ownerrepo[1])
if err != nil {
return nil, err
}
if err := decoder.Decode(gr, u.Query()); err != nil {
return nil, err
}

return gr, nil
return NewGHR(ctx, splitted[1])

case s3Scheme:
s3, err := NewS3(splitted[1])
if err != nil {
return nil, err
}
return s3, nil
return NewS3(ctx, splitted[1])

case grpcScheme:
u, err := url.Parse(strUrl)
if err != nil {
return nil, err
}

var gr GRPC
if err := decoder.Decode(&gr, u.Query()); err != nil {
return nil, err
}
if err := gr.Dial(u.Host); err != nil {
return nil, err
}

return &gr, nil
return NewGRPC(ctx, splitted[1])
}

return nil, fmt.Errorf("unsupported registry: %s", strUrl)
Expand Down
4 changes: 3 additions & 1 deletion registry/registry_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package registry

import (
"context"
"fmt"
"os"
"testing"
Expand Down Expand Up @@ -75,7 +76,8 @@ func TestNew(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.urlstr, func(t *testing.T) {
got, err := New(tt.urlstr)
ctx := context.Background()
got, err := New(ctx, tt.urlstr)
if (err != nil) != tt.wantErr {
t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
11 changes: 8 additions & 3 deletions registry/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type S3 struct {
}

// NewS3 returns S3.
func NewS3(path string) (*S3, error) {
func NewS3(ctx context.Context, path string) (*S3, error) {
u, err := url.Parse(path)
if err != nil {
return nil, err
Expand Down Expand Up @@ -67,15 +67,20 @@ func NewS3(path string) (*S3, error) {
return nil, fmt.Errorf("s3 bucket is required: %s", "s3://<bucket>/<prefix>")
}

ctx := context.Background()
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(s.Region))
if err != nil {
return nil, err
}

if s.Endpoint != "" {
s.cl = s3.NewFromConfig(cfg, func(o *s3.Options) {
o.EndpointResolver = s3.EndpointResolverFromURL(s.Endpoint)
// path-style: https://s3.region.amazonaws.com/<bucket>/<key>
o.UsePathStyle = true
o.BaseEndpoint = aws.String(s.Endpoint)
})
} else if e := os.Getenv("AWS_ENDPOINT_URL"); e != "" {
s.cl = s3.NewFromConfig(cfg, func(o *s3.Options) {
o.UsePathStyle = true
})
} else {
s.cl = s3.NewFromConfig(cfg)
Expand Down

0 comments on commit ad23965

Please sign in to comment.