Skip to content

Commit

Permalink
config file path
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryAtUber committed Jun 1, 2020
1 parent 6e71ef5 commit 086a4b9
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 19 deletions.
4 changes: 2 additions & 2 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
v1.1.7 - Athenareader output style and format added (May 31, 2020)
v1.1.8 - Athenareader output style and format added (May 31, 2020)

- prettify athenareader output
- One bug fix (https://github.com/uber/athenadriver/issues/12)
Expand All @@ -8,4 +8,4 @@ v1.1.6 - Pseudo commands, bug fix and more document and sample code (May 25, 202
- Introduce pseudo commands: get_query_id, get_query_id_status, stop_query_id, get_driver_version (doc: https://github.com/uber/athenadriver#pseudo-commands, Sample code: https://github.com/uber/athenadriver/tree/master/examples)
- Enable AWS profile manual setup for authentication (Sample code: https://github.com/uber/athenadriver/blob/master/examples/auth.go)
- Query Athena with athenadriver in AWS Lambda (https://github.com/uber/athenadriver/tree/master/examples/lambda/Go)
- One bug fix (https://github.com/uber/athenadriver/commit/8618706818a8db7abc8f1bd344ac0eca50d38959)
- One bug fix (https://github.com/uber/athenadriver/commit/8618706818a8db7abc8f1bd344ac0eca50d38959)
2 changes: 1 addition & 1 deletion athenareader/athenareader.config
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ athenareader:
moneywise: false

input:
bucket: "s3://henrywutest/"
bucket: "s3://athena-query-result-bucket/"
region: "us-east-1"
database: sampledb
admin: false
88 changes: 82 additions & 6 deletions athenareader/configfx/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import (
drv "github.com/uber/athenadriver/go"
"go.uber.org/config"
"go.uber.org/fx"
"io"
"io/ioutil"
"net/http"
"os"
)

Expand Down Expand Up @@ -138,15 +140,36 @@ func new(p Params) (Result, error) {
err error
)

if _, err = os.Stat("~/athenareader.config"); err == nil {
provider, err = config.NewYAML(config.File("~/athenareader.config"))
if _, err = os.Stat(HomeDir() + "/athenareader.config"); err == nil {
provider, err = config.NewYAML(config.File(HomeDir() + "/athenareader.config"))
} else if _, err = os.Stat("athenareader.config"); err == nil {
provider, err = config.NewYAML(config.File("athenareader.config"))
} else {
d, _ := os.Getwd()
println("could not find athenareader.config in home directory or current directory " + d)
p.Shutdowner.Shutdown()
os.Exit(2)
goPath := os.Getenv("GOPATH")
if goPath == "" {
goPath = HomeDir() + "/go"
if _, err = os.Stat(goPath); err != nil {
d, _ := os.Getwd()
println("could not find athenareader.config in home directory or current directory " + d)
p.Shutdowner.Shutdown()
os.Exit(2)
}
}
path := goPath + "/src/github.com/uber/athenadriver/athenareader/athenareader.config"
if _, err = os.Stat(path); err == nil {
Copy(path, HomeDir()+"/athenareader.config")
provider, err = config.NewYAML(config.File(path))
} else {
err = downloadFile(HomeDir()+"/athenareader.config", "https://raw.githubusercontent.com/uber/athenadriver/master/athenareader/athenareader.config")
if err != nil {
d, _ := os.Getwd()
println("could not find athenareader.config in home directory or current directory " + d)
p.Shutdowner.Shutdown()
os.Exit(2)
} else {
provider, err = config.NewYAML(config.File(HomeDir() + "/athenareader.config"))
}
}
}

if err != nil {
Expand All @@ -170,6 +193,7 @@ func new(p Params) (Result, error) {
}
if isFlagPassed("b") {
mc.IC.Bucket = *bucket
mc.DrvConfig.SetOutputBucket(mc.IC.Bucket)
}
if isFlagPassed("d") {
mc.IC.Database = *database
Expand Down Expand Up @@ -203,3 +227,55 @@ func new(p Params) (Result, error) {
MC: mc,
}, nil
}

func Copy(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()

out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()

_, err = io.Copy(out, in)
if err != nil {
return err
}
return out.Close()
}

func HomeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // windows
}

func downloadFile(filepath string, url string) (err error) {
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Check server response
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bad status: %s", resp.Status)
}
// Writer the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}
2 changes: 1 addition & 1 deletion athenareader/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/uber/athenadriver/athenareader
go 1.13

require (
github.com/uber/athenadriver v1.1.7
github.com/uber/athenadriver v1.1.8
go.uber.org/config v1.4.0
go.uber.org/fx v1.12.0
)
20 changes: 12 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ go 1.13

require (
github.com/DATA-DOG/go-sqlmock v1.4.1
github.com/aws/aws-sdk-go v1.29.16
github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535 // indirect
github.com/aws/aws-sdk-go v1.31.7
github.com/cactus/go-statsd-client/statsd v0.0.0-20191106001114-12b4e2b38748
github.com/go-openapi/strfmt v0.19.5 // indirect
github.com/go-openapi/errors v0.19.4 // indirect
github.com/jedib0t/go-pretty v4.3.0+incompatible
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/stretchr/testify v1.4.0
github.com/uber-go/tally v3.3.15+incompatible
github.com/uber/athenadriver/athenareader v0.0.0-20200531155210-80e6133fe73c // indirect
github.com/mitchellh/mapstructure v1.3.1 // indirect
github.com/stretchr/testify v1.5.1
github.com/uber-go/tally v3.3.16+incompatible
github.com/uber/athenadriver/athenareader v0.0.0-20200601071720-4f4514022f39 // indirect
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2
go.mongodb.org/mongo-driver v1.3.3 // indirect
go.uber.org/config v1.4.0
go.uber.org/fx v1.12.0
go.uber.org/zap v1.14.0
golang.org/x/tools v0.0.0-20200304024140-c4206d458c3f
go.uber.org/zap v1.15.0
golang.org/x/sys v0.0.0-20200523222454-059865788121 // indirect
golang.org/x/tools v0.0.0-20200530233709-52effbd89c51
gopkg.in/yaml.v2 v2.3.0 // indirect
)
Loading

0 comments on commit 086a4b9

Please sign in to comment.