Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions ds3/buildclient/buildClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"github.com/SpectraLogic/ds3_go_sdk/ds3/networking"
"os"
"strings"
)

const EndpointEnv = "DS3_ENDPOINT"
Expand All @@ -16,7 +17,7 @@ const ProxyEnv = "DS3_PROXY"

// Creates a client from cli arguments
func FromArgs(args *commands.Arguments) (*ds3.Client, error) {
return createClient(args.Endpoint, args.AccessKey, args.SecretKey, args.Proxy)
return createClient(args)
}

// Creates a client from environment variables
Expand All @@ -32,31 +33,46 @@ func FromEnv() (*ds3.Client, error) {
case endpoint == "": return nil, errors.New("Must specify an endpoint.")
case accessKey == "": return nil, errors.New("Must specify an access key.")
case secretKey == "": return nil, errors.New("Must specify an secret key.")
default: return createClient(endpoint, accessKey, secretKey, proxy)
}

args := commands.Arguments{
Endpoint: endpoint,
AccessKey: accessKey,
SecretKey: secretKey,
Proxy: proxy,
}

return createClient(&args)
}

// Creates a client
func createClient(endpoint, accessKey, secretKey, proxy string) (*ds3.Client, error) {
func createClient(args *commands.Arguments) (*ds3.Client, error) {

// Parse endpoint.
endpoint := args.Endpoint
if !strings.HasPrefix(endpoint, "http://") || !strings.HasPrefix(endpoint, "https://") {
endpoint = "https://" + endpoint
}

endpointUrl, endpointErr := url.Parse(endpoint)
if endpointErr != nil {
return nil, errors.New("The endpoint format was invalid.")
}

// Parse proxy.
var proxyUrlPtr *url.URL
if len(proxy) != 0 {
if len(args.Proxy) != 0 {
var proxyErr error
proxyUrlPtr, proxyErr = url.Parse(proxy)
proxyUrlPtr, proxyErr = url.Parse(args.Proxy)
if proxyErr != nil {
return nil, errors.New("The proxy format was invalid.")
}
}

// Create the client.
client := ds3.
NewClientBuilder(endpointUrl, &networking.Credentials{AccessId: accessKey, Key: secretKey}).
NewClientBuilder(endpointUrl, &networking.Credentials{AccessId: args.AccessKey, Key: args.SecretKey}).
WithIgnoreServerCertificate(args.IgnoreServerCertificate).
WithProxy(proxyUrlPtr).
BuildClient()
return client, nil
Expand Down
3 changes: 3 additions & 0 deletions ds3_cli/commands/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
type Arguments struct {
Endpoint, Proxy string
AccessKey, SecretKey string
IgnoreServerCertificate bool
Command string
Bucket string
Key string
Expand All @@ -21,6 +22,7 @@ type Arguments struct {
func ParseArgs() (*Arguments, error) {
// Parse command line arguments.
endpointParam := flag.String("endpoint", "", "Specifies the url to the DS3 server.")
ignoreServerCertParam := flag.Bool("ignore_server_cert", false, "Specifies whether to ignore the server's certificate.")
accessKeyParam := flag.String("access_key", "", "Specifies the access_key for the DS3 user.")
secretKeyParam := flag.String("secret_key", "", "Specifies the secret_key for the DS3 user.")
proxyParam := flag.String("proxy", "", "Specifies the HTTP proxy to route through.")
Expand All @@ -36,6 +38,7 @@ func ParseArgs() (*Arguments, error) {
// Build the arguments object.
args := Arguments{
Endpoint: paramOrEnv(*endpointParam, "DS3_ENDPOINT"),
IgnoreServerCertificate: *ignoreServerCertParam,
AccessKey: paramOrEnv(*accessKeyParam, "DS3_ACCESS_KEY"),
SecretKey: paramOrEnv(*secretKeyParam, "DS3_SECRET_KEY"),
Proxy: paramOrEnv(*proxyParam, "DS3_PROXY"),
Expand Down