Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GCP cfg.StorageProvider #53

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
30 changes: 29 additions & 1 deletion cfg/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ will panic if the key does not exist:
* [func (ep EnvProvider) Provide() (map[string]string, error)](#EnvProvider.Provide)
* [type FileProvider](#FileProvider)
* [func (fp FileProvider) Provide() (map[string]string, error)](#FileProvider.Provide)
* [type StorageProvider](#StorageProvider)
* [func (sp StorageProvider) Provide() (map[string]string, error)](#StorageProvider.Provide)
* [type MapProvider](#MapProvider)
* [func (mp MapProvider) Provide() (map[string]string, error)](#MapProvider.Provide)
* [type Provider](#Provider)
Expand All @@ -89,7 +91,7 @@ will panic if the key does not exist:
* [New](#example_New)

#### <a name="pkg-files">Package files</a>
[cfg.go](/src/github.com/ardanlabs/kit/cfg/cfg.go) [cfg_default.go](/src/github.com/ardanlabs/kit/cfg/cfg_default.go) [doc.go](/src/github.com/ardanlabs/kit/cfg/doc.go) [env_provider.go](/src/github.com/ardanlabs/kit/cfg/env_provider.go) [file_provider.go](/src/github.com/ardanlabs/kit/cfg/file_provider.go) [map_provider.go](/src/github.com/ardanlabs/kit/cfg/map_provider.go)
[cfg.go](/src/github.com/ardanlabs/kit/cfg/cfg.go) [cfg_default.go](/src/github.com/ardanlabs/kit/cfg/cfg_default.go) [doc.go](/src/github.com/ardanlabs/kit/cfg/doc.go) [env_provider.go](/src/github.com/ardanlabs/kit/cfg/env_provider.go) [file_provider.go](/src/github.com/ardanlabs/kit/cfg/file_provider.go) [storage_provider.go](/src/target/storage_provider.go) [map_provider.go](/src/github.com/ardanlabs/kit/cfg/map_provider.go)



Expand Down Expand Up @@ -546,6 +548,32 @@ func (fp FileProvider) Provide() (map[string]string, error)
Provide implements the Provider interface.


## <a name="StorageProvider">type</a> [StorageProvider](/cfg/storage_provider.go?#L13)
``` go
type StorageProvider struct {
Bucketname string
Filename string
}
```
StorageProvider describes GCP Storage based loader which loads the configuration
from a bucket and file listed.









### <a name="StorageProvider.Provide">func</a> (StorageProvider) [Provide](/cfg/storage_provider.go?#L19)
``` go
func (sp StorageProvider) Provide() (map[string]string, error)
```
Provide implements the Provider interface.





## <a name="MapProvider">type</a> [MapProvider](/src/target/map_provider.go?s=118:168#L1)
Expand Down
73 changes: 73 additions & 0 deletions cfg/storage_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package cfg

import (
"bufio"
"context"
"strings"

"cloud.google.com/go/storage"
)

// StorageProvider describes GCP Storage based loader which loads the configuration
// from a bucket and file listed.
type StorageProvider struct {
Bucketname string
Filename string
}

// Provide implements the Provider interface.
func (sp StorageProvider) Provide() (map[string]string, error) {
ctx := context.Background()

var config = make(map[string]string)

// Creating Storage client
// The client will use your default application credentials.
storageClient, err := storage.NewClient(ctx)
if err != nil {
return nil, err
}

// Reading object from storage
file, err := storageClient.Bucket(sp.Bucketname).Object(sp.Filename).NewReader(ctx)
if err != nil {
return nil, err
}
defer file.Close()

scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)

for scanner.Scan() {
line := scanner.Text()

if len(line) < 3 {
// the line doesn't have enough data
continue
}

if line[0] == '#' {
// the line starts with a comment character
continue
}

// find the first equals sign
index := strings.Index(line, "=")

// if we couldn't find one
if index <= 0 {
// the line is invalid
continue
}

if index == len(line)-1 {
// the line is invalid
continue
}

// add the item to the config
config[line[:index]] = line[index+1:]
}

return config, nil
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unhandled error from scanner

Suggested change
return config, nil
return config, scanner.Err()

}