diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..d55a4ec --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,179 @@ +run: + deadline: 1m + + # exit code when at least one issue was found, default is 1 + issues-exit-code: 1 + + # include test files or not, default is true + tests: true + + # list of build tags, all linters use it. Default is empty list. + build-tags: + - development + + # which dirs to skip: they won't be analyzed; + # can use regexp here: generated.*, regexp is applied on full path; + # default value is empty list, but next dirs are always skipped independently + # from this option's value: + # vendor$, third_party$, testdata$, examples$, Godeps$, builtin$ + #skip-dirs: + # - foo + # which files to skip: they will be analyzed, but issues from them + # won't be reported. Default value is empty list, but there is + # no need to include all autogenerated files, we confidently recognize + # autogenerated files. If it's not please let us know. + #skip-files: + # - ".*\\.my\\.go$" + # - lib/bad.go + # by default isn't set. If set we pass it to "go list -mod={option}". From "go help modules": + # If invoked with -mod=readonly, the go command is disallowed from the implicit + # automatic updating of go.mod described above. Instead, it fails when any changes + # to go.mod are needed. This setting is most useful to check that go.mod does + # not need updates, such as in a continuous integration and testing system. + # If invoked with -mod=vendor, the go command assumes that the vendor + # directory holds the correct copies of dependencies and ignores + # the dependency descriptions in go.mod. + #modules-download-mode: readonly|release|vendor + +# output configuration options +output: + # colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number" + format: colored-line-number + + # print lines of code with issue, default is true + print-issued-lines: true + + # print linter name in the end of issue text, default is true + print-linter-name: true + +# all available settings of specific linters +linters-settings: + errcheck: + # report about not checking of errors in type assetions: `a := b.(MyStruct)`; + # default is false: such cases aren't reported by default. + check-type-assertions: true + + # report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`; + # default is false: such cases aren't reported by default. + check-blank: false + + # [deprecated] comma-separated list of pairs of the form pkg:regex + # the regex is used to ignore names within pkg. (default "fmt:.*"). + # see https://github.com/kisielk/errcheck#the-deprecated-method for details + #ignore: fmt:.*,io/ioutil:^Read.* + # path to a file containing a list of functions to exclude from checking + # see https://github.com/kisielk/errcheck#excluding-functions for details + #exclude: /path/to/file.txt + funlen: + lines: 80 + statements: 60 + govet: + # report about shadowed variables + check-shadowing: true + golint: + # minimal confidence for issues, default is 0.8 + min-confidence: 0.8 + gofmt: + # simplify code: gofmt with `-s` option, true by default + simplify: true + goimports: + # put imports beginning with prefix after 3rd-party packages; + # it's a comma-separated list of prefixes + local-prefixes: github.com/org/project + gocyclo: + # minimal code complexity to report, 30 by default (but we recommend 10-20) + min-complexity: 20 + maligned: + # print struct with more effective memory layout or not, false by default + suggest-new: true + dupl: + # tokens count to trigger issue, 150 by default + threshold: 100 + goconst: + # minimal length of string constant, 3 by default + min-len: 3 + # minimal occurrences count to trigger, 3 by default + min-occurrences: 3 + depguard: + list-type: blacklist + include-go-root: false + packages: + - github.com/davecgh/go-spew/spew + misspell: + # Correct spellings using locale preferences for US or UK. + # Default is to use a neutral variety of English. + # Setting locale to US will correct the British spelling of 'colour' to 'color'. + locale: US + ignore-words: + - someword + lll: + # max line length, lines longer will be reported. Default is 120. + # '\t' is counted as 1 character by default, and can be changed with the tab-width option + line-length: 120 + # tab width in spaces. Default to 1. + tab-width: 1 + unused: + # treat code as a program (not a library) and report unused exported identifiers; default is false. + # XXX: if you enable this setting, unused will report a lot of false-positives in text editors: + # if it's called for subdir of a project it can't find funcs usages. All text editor integrations + # with golangci-lint call it on a directory with the changed file. + check-exported: false + unparam: + # Inspect exported functions, default is false. Set to true if no external program/library imports your code. + # XXX: if you enable this setting, unparam will report a lot of false-positives in text editors: + # if it's called for subdir of a project it can't find external interfaces. All text editor integrations + # with golangci-lint call it on a directory with the changed file. + check-exported: false + nakedret: + # make an issue if func has more lines of code than this setting and it has naked returns; default is 30 + max-func-lines: 30 + prealloc: + # XXX: we don't recommend using this linter before doing performance profiling. + # For most programs usage of prealloc will be a premature optimization. + + # Report preallocation suggestions only on simple loops that have no returns/breaks/continues/gotos in them. + # True by default. + simple: true + range-loops: true # Report preallocation suggestions on range loops, true by default + for-loops: false # Report preallocation suggestions on for loops, false by default + gocritic: + # Which checks should be disabled; can't be combined with 'enabled-checks'; default is empty + disabled-checks: + - hugeParam + - sloppyReassign + - octalLiteral + - whyNoLint + + # Enable multiple checks by tags, run `GL_DEBUG=gocritic golangci-lint` run to see all tags and checks. + # Empty list by default. See https://github.com/go-critic/go-critic#usage -> section "Tags". + enabled-tags: + - diagnostic + - performance + - style + - opinionated + + settings: # settings passed to gocritic + captLocal: # must be valid enabled check name + paramsOnly: true + rangeValCopy: + sizeThreshold: 128 +issues: + exclude-rules: + - linters: + - gosec + text: G104 # doubles errcheck error handling check + - linters: + - gosec + text: G304 # way too many false positives about "file inclusion" + exclude-use-default: false + +linters: + fast: false + enable-all: true + disable: + - dupl + - gochecknoglobals + - gochecknoinits + - lll + - nakedret + - scopelint # https://github.com/kyoh86/scopelint/issues/4 diff --git a/README.md b/README.md index 58c8da0..5b1e705 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,11 @@ # Databricks + This library is a Go client for the Databricks [REST API V2](https://docs.databricks.com/api/latest/index.html#rest-api-2-0). It's designed to be a minimal client with few dependencies. # Authentication + This library makes little attempts to handle authentication for you. It probably needs some more thought on how it should work. For now you can inject your own `http.Client` to handle authentication for you. You can find more @@ -12,19 +14,12 @@ information of how Databricks handles for details on how to generate tokens. # Hacking + There is some work to still be done in handling date times. Currently the client does the lazy method of just using `int64`s in most cases (as the Databricks API generally uses epoch nanos). # Features -The client library supports injecting your own `http.Client` using the -`ClientHTTPClient` function. There is a special client for using -[`.netrc`](https://www.gnu.org/software/inetutils/manual/html_node/The-_002enetrc-file.html). -The netrc client can be easily used: -```go -client, err := databricks.NewClient( - "", - databricks.ClientHTTPClient(databricks.NetrcHTTPClient), -) -``` +The client library supports injecting your own `http.Client` using the +`ClientHTTPClient` function. diff --git a/doc.go b/doc.go new file mode 100644 index 0000000..7aefa4e --- /dev/null +++ b/doc.go @@ -0,0 +1,2 @@ +// Package databricks implements a client for the DataBricks V2 REST API +package databricks diff --git a/example/main.go b/example/main.go index aca58e1..d04ef18 100644 --- a/example/main.go +++ b/example/main.go @@ -16,7 +16,7 @@ func main() { flag.Parse() client, err := databricks.NewClient( *account, - databricks.ClientHTTPClient(databricks.NetrcHTTPClient), + databricks.ClientHTTPClient(databricks.NewBearerHTTPClient("db_token")), ) if err != nil { log.Fatalln(err) diff --git a/go.mod b/go.mod index bb2172f..664956a 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,3 @@ module github.com/medivo/databricks-go -require ( - github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d - github.com/mitchellh/go-homedir v1.0.0 -) +go 1.12 diff --git a/go.sum b/go.sum index c92b1bd..e69de29 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +0,0 @@ -github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d h1:xDfNPAt8lFiC1UJrqV3uuy861HCTo708pDMbjHHdCas= -github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d/go.mod h1:6QX/PXZ00z/TKoufEY6K/a0k6AhaJrQKdFe6OfVXsa4= -github.com/mitchellh/go-homedir v1.0.0 h1:vKb8ShqSby24Yrqr/yDYkuFz8d0WUjys40rvnGC8aR0= -github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= diff --git a/utils.go b/utils.go index da66ce6..9d55a91 100644 --- a/utils.go +++ b/utils.go @@ -3,29 +3,10 @@ package databricks import ( "fmt" "net/http" - "net/url" - "os" - "runtime" - - "github.com/bgentry/go-netrc/netrc" - "github.com/mitchellh/go-homedir" ) -type netrcRoundTripper struct{} - -// RoundTrip implements the http.RoundTripper interface. -func (r netrcRoundTripper) RoundTrip( - req *http.Request, -) (*http.Response, error) { - if err := addAuthFromNetrc(req.URL); err != nil { - return nil, err - } - return http.DefaultClient.Do(req) -} - -// NetrcHTTPClient adds auth from NETRC. -var NetrcHTTPClient = &http.Client{ - Transport: netrcRoundTripper{}, +type bearerRoundTripper struct { + token string } // NewBearerHTTPClient uses a token as an authorization bearer. @@ -37,70 +18,8 @@ func NewBearerHTTPClient(token string) *http.Client { return &client } -type bearerRoundTripper struct { - token string -} - // RoundTrip implements the http.RoundTripper interface. -func (r bearerRoundTripper) RoundTrip( - req *http.Request, -) (*http.Response, error) { +func (r bearerRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", r.token)) return http.DefaultClient.Do(req) } - -// addAuthFromNetrc adds auth information to the URL from the user's -// netrc file if it can be found. This will only add the auth info -// if the URL doesn't already have auth info specified and the -// the username is blank. -func addAuthFromNetrc(u *url.URL) error { - // If the URL already has auth information, do nothing - if u.User != nil && u.User.Username() != "" { - return nil - } - - // Get the netrc file path - path := os.Getenv("NETRC") - if path == "" { - filename := ".netrc" - if runtime.GOOS == "windows" { - filename = "_netrc" - } - - var err error - path, err = homedir.Expand("~/" + filename) - if err != nil { - return err - } - } - - // If the file is not a file, then do nothing - if fi, err := os.Stat(path); err != nil { - // File doesn't exist, do nothing - if os.IsNotExist(err) { - return nil - } - - // Some other error! - return err - } else if fi.IsDir() { - // File is directory, ignore - return nil - } - - // Load up the netrc file - net, err := netrc.ParseFile(path) - if err != nil { - return fmt.Errorf("Error parsing netrc file at %q: %s", path, err) - } - - machine := net.FindMachine(u.Host) - if machine == nil { - // Machine not found, no problem - return nil - } - - // Set the user info - u.User = url.UserPassword(machine.Login, machine.Password) - return nil -}