// Copyright 2016-2018, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package config

import (
	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

// Config is a struct that permits access to config as a "bag" with a package name.  This avoids needing to access
// config with the fully qualified name all of the time (e.g., a bag whose namespace is "p" automatically translates
// attempted reads of keys "k" into "p:k").  This is optional but can save on some boilerplate when accessing config.
type Config struct {
	ctx       *pulumi.Context
	namespace string
}

// New creates a new config bag with the given context and namespace.
func New(ctx *pulumi.Context, namespace string) *Config {
	if namespace == "" {
		namespace = ctx.Project()
	}

	return &Config{ctx: ctx, namespace: namespace}
}

// fullKey turns a simple configuration key into a fully resolved one, by prepending the bag's name.
func (c *Config) fullKey(key string) string {
	return c.namespace + ":" + key
}

// Get loads an optional configuration value by its key, or returns "" if it doesn't exist.
func (c *Config) Get(key string) string {
	return Get(c.ctx, c.fullKey(key))
}

// GetObject loads an optional configuration value into the specified output by its key,
// or returns an error if unable to do so.
func (c *Config) GetObject(key string, output interface{}) error {
	return GetObject(c.ctx, c.fullKey(key), output)
}

{{range .Builtins}}
{{if .GenerateConfig}}
// Get{{.Name}} loads an optional {{.Type}} configuration value by its key, or returns {{.DefaultConfig}} if it doesn't exist.
func (c *Config) Get{{.Name}}(key string) {{.Type}} {
	return Get{{.Name}}(c.ctx, c.fullKey(key))
}

{{end}}
{{end}}
// Require loads a configuration value by its key, or panics if it doesn't exist.
func (c *Config) Require(key string) string {
	return Require(c.ctx, c.fullKey(key))
}

// RequireObject loads a required configuration value into the specified output by its key,
// or panics if unable to do so.
func (c *Config) RequireObject(key string, output interface{}) {
	RequireObject(c.ctx, c.fullKey(key), output)
}

{{range .Builtins}}
{{if .GenerateConfig}}
// Require{{.Name}} loads a {{.Type}} configuration value by its key, or panics if it doesn't exist.
func (c *Config) Require{{.Name}}(key string) {{.Type}} {
	return Require{{.Name}}(c.ctx, c.fullKey(key))
}

{{end}}
{{end}}
// Try loads a configuration value by its key, returning a non-nil error if it doesn't exist.
func (c *Config) Try(key string) (string, error) {
	return Try(c.ctx, c.fullKey(key))
}

// TryObject loads an optional configuration value into the specified output by its key,
// or returns an error if unable to do so.
func (c *Config) TryObject(key string, output interface{}) error {
	return TryObject(c.ctx, c.fullKey(key), output)
}

{{range .Builtins}}
{{if .GenerateConfig}}
// Try{{.Name}} loads an optional {{.Type}} configuration value by its key, or returns an error if it doesn't exist.
func (c *Config) Try{{.Name}}(key string) ({{.Type}}, error) {
	return Try{{.Name}}(c.ctx, c.fullKey(key))
}

{{end}}
{{end}}
// GetSecret loads an optional configuration value by its key 
// or "" if it doesn't exist, and returns it wrapped in a secret Output.
func (c *Config) GetSecret(key string) pulumi.StringOutput {
	return GetSecret(c.ctx, c.fullKey(key))
}

// GetSecretObject loads an optional configuration value into the specified output by its key,
// returning it wrapped in a secret Output or an error if unable to do so.
func (c *Config) GetSecretObject(key string, output interface{}) (pulumi.Output, error) {
	return GetSecretObject(c.ctx, c.fullKey(key), output)
}

{{range .Builtins}}
{{if .GenerateConfig}}
// GetSecret{{.Name}} loads an optional {{.Type}} configuration value by its key 
// or {{.DefaultConfig}} if it doesn't exist, and returns it wrapped in a secret Output.
func (c *Config) GetSecret{{.Name}}(key string) pulumi.{{.Name}}Output {
	return GetSecret{{.Name}}(c.ctx, c.fullKey(key))
}

{{end}}
{{end}}
// RequireSecret loads a configuration value by its key
// and returns it wrapped in a secret output, or panics if it doesn't exist.
func (c *Config) RequireSecret(key string) pulumi.StringOutput {
	return RequireSecret(c.ctx, c.fullKey(key))
}

// RequireSecretObject loads a required configuration value into the specified output by its key
// and returns it wrapped in a secret Output, or panics if unable to do so.
func (c *Config) RequireSecretObject(key string, output interface{}) pulumi.Output {
	return RequireSecretObject(c.ctx, c.fullKey(key), output)
}

{{range .Builtins}}
{{if .GenerateConfig}}
// RequireSecret{{.Name}} loads a {{.Type}} configuration value by its key
// and returns is wrapped in a secret Output, or panics if it doesn't exist.
func (c *Config) RequireSecret{{.Name}}(key string) pulumi.{{.Name}}Output {
	return RequireSecret{{.Name}}(c.ctx, c.fullKey(key))
}

{{end}}
{{end}}
// TrySecret loads a configuration value by its key, returning a non-nil error if it doesn't exist.
func (c *Config) TrySecret(key string) (pulumi.StringOutput, error) {
	return TrySecret(c.ctx, c.fullKey(key))
}

// TrySecretObject loads a configuration value by its key, returning a non-nil error if it doesn't exist.
func (c *Config) TrySecretObject(key string, output interface{}) (pulumi.Output, error) {
	return TrySecretObject(c.ctx, c.fullKey(key), output)
}

{{range .Builtins}}
{{if .GenerateConfig}}
// TrySecret{{.Name}} loads an optional {{.Type}} configuration value by its key into a secret Output, 
// or returns an error if it doesn't exist.
func (c *Config) TrySecret{{.Name}}(key string) (pulumi.{{.Name}}Output, error) {
	return TrySecret{{.Name}}(c.ctx, c.fullKey(key))
}

{{end}}
{{end}}
