Copyright 2020 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
Package cmdconfig contains functions for configuring commands.
package cmdconfig

import (
	
	
	
	

	
	
	_  // for pgx driver
	
	
	
	
	
	
	
	
)
Logger configures a middleware.Logger.
func ( context.Context,  *config.Config,  string) middleware.Logger {
	if .OnGCP() {
		,  := log.UseStackdriver(, , )
		if  != nil {
			log.Fatal(, )
		}
		return 
	}
	return middleware.LocalLogger{}
}
ReportingClient configures an Error Reporting client.
func ( context.Context,  *config.Config) *errorreporting.Client {
	if !.OnGCP() || .DisableErrorReporting {
		return nil
	}
	,  := errorreporting.NewClient(, .ProjectID, errorreporting.Config{
		ServiceName: .ServiceID,
		OnError: func( error) {
			log.Errorf(, "Error reporting failed: %v", )
		},
	})
	if  != nil {
		log.Fatal(, )
	}
	derrors.SetReportingClient()
	return 
}
Experimenter configures a middleware.Experimenter.
func ( context.Context,  *config.Config,  middleware.ExperimentGetter,  *errorreporting.Client) *middleware.Experimenter {
	,  := middleware.NewExperimenter(, 1*time.Minute, , )
	if  != nil {
		log.Fatal(, )
	}
	return 
}
ExperimentGetter returns an ExperimentGetter using the config.
func ( context.Context,  *config.Config) middleware.ExperimentGetter {
	if .DynamicConfigLocation == "" {
		log.Warningf(, "experiments are not configured")
		return func(context.Context) ([]*internal.Experiment, error) { return nil, nil }
	}
	log.Infof(, "using dynamic config from %s for experiments", .DynamicConfigLocation)
	return func( context.Context) ([]*internal.Experiment, error) {
		,  := dynconfig.Read(, .DynamicConfigLocation)
		if  != nil {
			return nil, 
		}

		var  []string
		for ,  := range .Experiments {
			 = append(, fmt.Sprintf("%s:%d", .Name, .Rollout))
			if ,  := internal.Experiments[.Name];  {
				if .Description == "" {
					.Description = 
				}
			} else {
				log.Errorf(, "unknown experiment %q", .Name)
			}
		}
		log.Infof(, "read experiments %s", strings.Join(, ", "))
		return .Experiments, nil
	}
}
OpenDB opens the postgres database specified by the config. It first tries the main connection info (DBConnInfo), and if that fails, it uses backup connection info it if exists (DBSecondaryConnInfo).
func ( context.Context,  *config.Config,  bool) ( *postgres.DB,  error) {
	defer derrors.Wrap(&, "cmdconfig.OpenDB(ctx, cfg)")
Wrap the postgres driver with our own wrapper, which adds OpenCensus instrumentation.
	,  := database.RegisterOCWrapper(.DBDriver, ocsql.WithAllTraceOptions())
	if  != nil {
		return nil, fmt.Errorf("unable to register the ocsql driver: %v", )
	}
	log.Infof(, "opening database on host %s", .DBHost)
	,  := database.Open(, .DBConnInfo(), .InstanceID)
	if  == nil {
		log.Infof(, "connected to primary host: %s", .DBHost)
	} else {
		 := .DBSecondaryConnInfo()
		if  == "" {
			log.Infof(, "no secondary DB host")
			return nil, 
		}
		log.Errorf(, "database.Open for primary host %s failed with %v; trying secondary host %s ",
			.DBHost, , .DBSecondaryHost)
		,  = database.Open(, , .InstanceID)
		if  != nil {
			return nil, 
		}
		log.Infof(, "connected to secondary host %s", .DBSecondaryHost)
	}
	log.Infof(, "database open finished")
	if  {
		return postgres.NewBypassingLicenseCheck(), nil
	}
	return postgres.New(), nil