Skip to content

Commit

Permalink
Fixed ini file parser
Browse files Browse the repository at this point in the history
  • Loading branch information
David Koblas committed Apr 22, 2019
1 parent 858d0fb commit 683849a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
29 changes: 19 additions & 10 deletions config.go
Expand Up @@ -2,6 +2,7 @@ package main

import (
// "fmt"
"fmt"
"path"
"reflect"
"strings"
Expand Down Expand Up @@ -37,16 +38,16 @@ type Config struct {
// - Config File
// - Default Values
func NewConfig(c *cli.Context) *Config {
cfgPath := "/.s3cfg"

if c.GlobalIsSet("config") {
cfgPath = c.GlobalString("config")
} else if c.IsSet("config") {
cfgPath = c.String("config")
var cfgPath string

if obj := c.GlobalStringSlice("config"); len(obj) > 1 {
cfgPath = obj[1]
} else if obj := c.StringSlice("config"); len(obj) > 1 {
cfgPath = obj[1]
} else if value := GetEnv("HOME"); value != nil {
cfgPath = path.Join(*value, ".s3cfg")
} else {
if value := GetEnv("HOME"); value != nil {
cfgPath = path.Join(*value, ".s3cfg")
}
cfgPath = ".s3cfg"
}

config := loadConfigFile(cfgPath)
Expand All @@ -64,13 +65,21 @@ func NewConfig(c *cli.Context) *Config {
func loadConfigFile(path string) *Config {
config := &Config{CheckMD5: false}

// fmt.Println("Read config ", path)
fmt.Println("Read config ", path)

cfg, err := ini.Load(path)
if err != nil {
return config
}

// s3cmd ini files are not Python configfiles --
//
// The INI file parser will use the %(bucket) and do a
// lookup on the key, if not found it will panic
// this puts a key in that causes the recursive lookup to limit
// out but allows things to work.
cfg.Section("").NewKey("bucket", "%(bucket)s")

if err := cfg.Section("default").MapTo(config); err != nil {
return config
}
Expand Down
9 changes: 7 additions & 2 deletions session.go
Expand Up @@ -24,13 +24,18 @@ func buildSessionConfig(config *Config) aws.Config {
return sessionConfig
}

func buildEndpointResolver(hostname string) endpoints.Resolver{
func buildEndpointResolver(hostname string) endpoints.Resolver {
defaultResolver := endpoints.DefaultResolver()

fixedHost := hostname
if !strings.HasPrefix(hostname, "http") {
fixedHost = "https://" + hostname
}

return endpoints.ResolverFunc(func(service, region string, optFns ...func(*endpoints.Options)) (endpoints.ResolvedEndpoint, error) {
if service == endpoints.S3ServiceID {
return endpoints.ResolvedEndpoint{
URL: hostname,
URL: fixedHost,
}, nil
}

Expand Down

0 comments on commit 683849a

Please sign in to comment.