From d893f6373ce33ec2bef04cecb6041cb274b1ff12 Mon Sep 17 00:00:00 2001 From: Don Date: Fri, 11 Oct 2019 11:37:45 -0700 Subject: [PATCH] Start fleshing out network helpers --- pkg/urfave/network.go | 77 +++++++++++++++++++++++++++++++++++++++++++ pkg/urfave/types.go | 39 ---------------------- 2 files changed, 77 insertions(+), 39 deletions(-) create mode 100644 pkg/urfave/network.go delete mode 100644 pkg/urfave/types.go diff --git a/pkg/urfave/network.go b/pkg/urfave/network.go new file mode 100644 index 0000000..abcaba0 --- /dev/null +++ b/pkg/urfave/network.go @@ -0,0 +1,77 @@ +// Copyright (c) 2019, the Drone Plugins project authors. +// Please see the AUTHORS file for details. All rights reserved. +// Use of this source code is governed by an Apache 2.0 license that can be +// found in the LICENSE file. + +package urfave + +import ( + "context" + "net" + "net/http" + "time" + + "github.com/urfave/cli" +) + +//--------------------------------------------------------------------- +// Transport Flags +//--------------------------------------------------------------------- + +// Network contains options for connecting to the network. +type Network struct { + // Context for making network requests. + // + // If `trace` logging is requested the context will use `httptrace` to + // capture all network requests. + Context context.Context + + // Client for making network requests. + Client *http.Client +} + +const networkSSLVerifyFlag = "transport.ssl-verify" + +// networkFlags has the cli.Flags for the Transport. +func networkFlags() []cli.Flag { + return []cli.Flag{ + cli.BoolFlag{ + Name: networkSSLVerifyFlag, + Usage: "transport ssl verify", + EnvVar: "PLUGIN_SSL_VERIFY", + }, + } +} + +// NetworkFromContext creates a Transport from the cli.Context. +func NetworkFromContext(ctx *cli.Context) Network { + // Create the client + client := &http.Client{ + Timeout: 10 * time.Second, + Transport: &http.Transport{ + Proxy: http.ProxyFromEnvironment, + DialContext: (&net.Dialer{ + Timeout: 30 * time.Second, + KeepAlive: 30 * time.Second, + DualStack: true, + }).DialContext, + ForceAttemptHTTP2: true, + MaxIdleConns: 100, + IdleConnTimeout: 90 * time.Second, + TLSHandshakeTimeout: 10 * time.Second, + ExpectContinueTimeout: 1 * time.Second, + }, + } + + // Create the context + context := context.Background() + + if ctx.String(logLevelFlag) == "trace" { + context = traceHTTP(context) + } + + return Network{ + Client: client, + Context: context, + } +} diff --git a/pkg/urfave/types.go b/pkg/urfave/types.go deleted file mode 100644 index bf964c9..0000000 --- a/pkg/urfave/types.go +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright (c) 2019, the Drone Plugins project authors. -// Please see the AUTHORS file for details. All rights reserved. -// Use of this source code is governed by an Apache 2.0 license that can be -// found in the LICENSE file. - -package urfave - -import "github.com/urfave/cli" - -//--------------------------------------------------------------------- -// Transport Flags -//--------------------------------------------------------------------- - -// Transport contains options for the underlying http client. -type Transport struct { - // SSLVerify certificate information. - SSLVerify bool -} - -const transportSSLVerifyFlag = "transport.ssl-verify" - -// TransportFlags has the cli.Flags for the Transport. -func TransportFlags() []cli.Flag { - return []cli.Flag{ - cli.BoolFlag{ - Name: transportSSLVerifyFlag, - Usage: "transport ssl verify", - EnvVar: "PLUGIN_SSL_VERIFY", - Hidden: true, - }, - } -} - -// TransportFromContext creates a Transport from the cli.Context. -func TransportFromContext(ctx cli.Context) Transport { - return Transport{ - SSLVerify: ctx.Bool(transportSSLVerifyFlag), - } -}