mdb: refactor to use new mctx stuff

This commit is contained in:
Brian Picciano 2019-02-02 21:56:32 -05:00
parent f783b62f6f
commit 608eb90274

View File

@ -3,18 +3,13 @@
package mdb package mdb
import ( import (
"context"
"github.com/mediocregopher/mediocre-go-lib/mcfg" "github.com/mediocregopher/mediocre-go-lib/mcfg"
"github.com/mediocregopher/mediocre-go-lib/mctx"
"github.com/mediocregopher/mediocre-go-lib/mlog" "github.com/mediocregopher/mediocre-go-lib/mlog"
"github.com/mediocregopher/mediocre-go-lib/mrun"
"google.golang.org/api/option" "google.golang.org/api/option"
) )
// DefaultGCEProject can be set before any of the Cfg* functions are called for
// GCE services, and will be used as the default value for the "project"
// configuration parameter for each service.
var DefaultGCEProject string
// GCE wraps configuration parameters commonly used for interacting with GCE // GCE wraps configuration parameters commonly used for interacting with GCE
// services. // services.
type GCE struct { type GCE struct {
@ -22,14 +17,24 @@ type GCE struct {
CredFile string CredFile string
} }
// CfgGCE configures and returns a GCE instance which will be usable once Run is // MGCE returns a GCE instance which will be initialized and configured when the
// called on the passed in Cfg instance. // start event is triggered on ctx (see mrun.Start). defaultProject is used as
func CfgGCE(cfg *mcfg.Cfg) *GCE { // the default value for the mcfg parameter this function creates.
proj := cfg.ParamString("project", DefaultGCEProject, "name of GCE project") func MGCE(ctx mctx.Context, defaultProject string) *GCE {
credFile := cfg.ParamString("cred-file", "", "path to GCE credientials json file, if any") ctx = mctx.ChildOf(ctx, "gce")
credFile := mcfg.String(ctx, "cred-file", "", "Path to GCE credientials JSON file, if any")
var project *string
const projectUsage = "Name of GCE project to use"
if defaultProject == "" {
project = mcfg.RequiredString(ctx, "project", projectUsage)
} else {
project = mcfg.String(ctx, "project", defaultProject, projectUsage)
}
var gce GCE var gce GCE
cfg.Start.Then(func(ctx context.Context) error { mrun.OnStart(ctx, func(mctx.Context) error {
gce.Project = *proj gce.Project = *project
gce.CredFile = *credFile gce.CredFile = *credFile
return nil return nil
}) })
@ -46,9 +51,9 @@ func (gce *GCE) ClientOptions() []option.ClientOption {
return opts return opts
} }
// KV implements the mlog.KVer interface // KV implements the mlog.KVer interface.
func (gce *GCE) KV() mlog.KV { func (gce *GCE) KV() map[string]interface{} {
return mlog.KV{ return mlog.KV{
"project": gce.Project, "gceProject": gce.Project,
} }
} }