From 482ebb7d7796ba93756d11e524bfcfa819788d99 Mon Sep 17 00:00:00 2001 From: Askar Sagyndyk Date: Fri, 1 Mar 2019 10:52:22 -0500 Subject: [PATCH 1/2] Added GCP StorageProvider --- cfg/readme.md | 30 ++++++++++++++++- cfg/storage_provider.go | 73 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 cfg/storage_provider.go diff --git a/cfg/readme.md b/cfg/readme.md index e5af50f..62cf64b 100644 --- a/cfg/readme.md +++ b/cfg/readme.md @@ -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) @@ -89,7 +91,7 @@ will panic if the key does not exist: * [New](#example_New) #### Package files -[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) @@ -546,6 +548,32 @@ func (fp FileProvider) Provide() (map[string]string, error) Provide implements the Provider interface. +## type [StorageProvider](/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. + + + + + + + + + +### func (StorageProvider) [Provide](/storage_provider.go?#L19) +``` go +func (sp StorageProvider) Provide() (map[string]string, error) +``` +Provide implements the Provider interface. + + + ## type [MapProvider](/src/target/map_provider.go?s=118:168#L1) diff --git a/cfg/storage_provider.go b/cfg/storage_provider.go new file mode 100644 index 0000000..7c50465 --- /dev/null +++ b/cfg/storage_provider.go @@ -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 +} From b62eb3df1c00853d3ee1bc8e19301f25cec15370 Mon Sep 17 00:00:00 2001 From: Askar Sagyndyk Date: Fri, 1 Mar 2019 10:55:03 -0500 Subject: [PATCH 2/2] corrected links reference for StorageProvider --- cfg/readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cfg/readme.md b/cfg/readme.md index 62cf64b..15c29dd 100644 --- a/cfg/readme.md +++ b/cfg/readme.md @@ -548,7 +548,7 @@ func (fp FileProvider) Provide() (map[string]string, error) Provide implements the Provider interface. -## type [StorageProvider](/storage_provider.go?#L13) +## type [StorageProvider](/cfg/storage_provider.go?#L13) ``` go type StorageProvider struct { Bucketname string @@ -566,7 +566,7 @@ from a bucket and file listed. -### func (StorageProvider) [Provide](/storage_provider.go?#L19) +### func (StorageProvider) [Provide](/cfg/storage_provider.go?#L19) ``` go func (sp StorageProvider) Provide() (map[string]string, error) ```