Skip to content

Commit

Permalink
Minor refactor to support flyctl interfaces (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
benbjohnson authored May 20, 2024
1 parent 39edf76 commit 448d1e5
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 49 deletions.
5 changes: 4 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type InstrumentationService interface {
type Client struct {
httpClient *http.Client
client *graphql.Client
GenqClient genq.Client
genqClient genq.Client
tokens *tokens.Tokens
logger Logger
}
Expand All @@ -77,6 +77,9 @@ func (c *Client) Authenticated() bool {
return c.tokens.GraphQL() != ""
}

func (c *Client) GenqClient() genq.Client { return c.genqClient }
func (c *Client) SetGenqClient(client genq.Client) { c.genqClient = client }

// NewClient - creates a new Client, takes an access token
func NewClient(accessToken, name, version string, logger Logger) *Client {
return NewClientFromOptions(ClientOptions{
Expand Down
11 changes: 0 additions & 11 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,10 @@ import "context"
type contextKey string

const (
contextKeyClient = contextKey("client")
contextKeyAuthorization = contextKey("authorization")
contextKeyRequestStart = contextKey("RequestStart")
)

// NewContextWithClient derives a Context that carries c from ctx.
func NewContextWithClient(ctx context.Context, c *Client) context.Context {
return context.WithValue(ctx, contextKeyClient, c)
}

// FromContext returns the Client ctx carries. Panics if ctx carries no Client.
func ClientFromContext(ctx context.Context) *Client {
return ctx.Value(contextKeyClient).(*Client)
}

// WithAuthorizationHeader returns a context that instructs the client to use
// the specified Authorization header value.
func WithAuthorizationHeader(ctx context.Context, hdr string) context.Context {
Expand Down
18 changes: 4 additions & 14 deletions flaps/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,10 @@ package flaps

import "context"

type contextKey struct{}
type machineIDCtxKey struct{}
type actionCtxKey struct{}

// NewContext derives a Context that carries c from ctx.
func NewContext(ctx context.Context, c *Client) context.Context {
return context.WithValue(ctx, contextKey{}, c)
}

// FromContext returns the Client ctx carries. It panics in case ctx carries
// no Client.
func FromContext(ctx context.Context) *Client {
return ctx.Value(contextKey{}).(*Client)
}
type (
machineIDCtxKey struct{}
actionCtxKey struct{}
)

func contextWithMachineID(ctx context.Context, id string) context.Context {
return context.WithValue(ctx, machineIDCtxKey{}, id)
Expand Down
25 changes: 2 additions & 23 deletions flaps/flaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type NewClientOpts struct {

// optional, used to connect to machines API
DialContext func(ctx context.Context, network, address string) (net.Conn, error)
OrgSlug string // required if DialContext set

// URL used when connecting via usermode wireguard.
BaseURL *url.URL
Expand All @@ -68,14 +69,9 @@ func NewWithOptions(ctx context.Context, opts NewClientOpts) (*Client, error) {
}

if opts.DialContext != nil {
orgSlug, err := resolveOrgSlugForApp(ctx, opts.AppCompact, opts.AppName)
if err != nil {
return nil, fmt.Errorf("failed to resolve org for app '%s': %w", opts.AppName, err)
}

return newWithUsermodeWireguard(ctx, wireguardConnectionParams{
appName: opts.AppName,
orgSlug: orgSlug,
orgSlug: opts.OrgSlug,
dialContext: opts.DialContext,
baseURL: opts.BaseURL,
userAgent: opts.UserAgent,
Expand Down Expand Up @@ -112,23 +108,6 @@ func NewWithOptions(ctx context.Context, opts NewClientOpts) (*Client, error) {
}, nil
}

func resolveOrgSlugForApp(ctx context.Context, app *fly.AppCompact, appName string) (string, error) {
app, err := resolveApp(ctx, app, appName)
if err != nil {
return "", err
}
return app.Organization.Slug, nil
}

func resolveApp(ctx context.Context, app *fly.AppCompact, appName string) (*fly.AppCompact, error) {
var err error
if app == nil {
apiClient := fly.ClientFromContext(ctx)
app, err = apiClient.GetAppCompact(ctx, appName)
}
return app, err
}

type wireguardConnectionParams struct {
appName string
orgSlug string
Expand Down

0 comments on commit 448d1e5

Please sign in to comment.