<commit_before>package command

import (
	"bytes"
	"context"
	"fmt"
	"io"
	"os"
	"path/filepath"
	"sort"
	"strings"
	texttemplate "text/template"

	"github.com/hashicorp/hcl/v2/hclwrite"
	hcl2shim "github.com/hashicorp/packer/hcl2template/shim"
	"github.com/hashicorp/packer/template"
	"github.com/posener/complete"
	"github.com/zclconf/go-cty/cty"
)

type HCL2UpgradeCommand struct {
	Meta
}

func (c *HCL2UpgradeCommand) Run(args []string) int {
	ctx, cleanup := handleTermInterrupt(c.Ui)
	defer cleanup()

	cfg, ret := c.ParseArgs(args)
	if ret != 0 {
		return ret
	}

	return c.RunContext(ctx, cfg)
}

func (c *HCL2UpgradeCommand) ParseArgs(args []string) (*HCL2UpgradeArgs, int) {
	var cfg HCL2UpgradeArgs
	flags := c.Meta.FlagSet("hcl2_upgrade", FlagSetNone)
	flags.Usage = func() { c.Ui.Say(c.Help()) }
	cfg.AddFlagSets(flags)
	if err := flags.Parse(args); err != nil {
		return &cfg, 1
	}
	args = flags.Args()
	if len(args) != 1 {
		flags.Usage()
		return &cfg, 1
	}
	cfg.Path = args[0]
	if cfg.OutputFile == "" {
		cfg.OutputFile = cfg.Path + ".pkr.hcl"
	}
	return &cfg, 0
}

const (
	hcl2UpgradeFileHeader = `# This file was autogenerate by the BETA 'packer hcl2_upgrade' command. We
# recommend double checking that everything is correct before going forward. We
# also recommend treating this file as disposable. The HCL2 blocks in this
# file can be moved to other files. For example, the variable blocks could be
# moved to their own 'variables.pkr.hcl' file, etc. Those files need to be
# suffixed with '.pkr.hcl' to be visible to Packer. To use multiple files at
# once they also need to be in the same folder. 'packer inspect folder/'
# will describe to you what is in that folder.

# All generated input variables will be of string type as this how Packer JSON
# views them; you can later on change their type. Read the variables type
# constraints documentation
# https://www.packer.io/docs/from-1.5/variables#type-constraints for more info.
`

	sourcesHeader = `
# source blocks are generated from your builders; a source can be referenced in
# build blocks. A build block runs provisioner and post-processors onto a
# source. Read the documentation for source blocks here:
# https://www.packer.io/docs/from-1.5/blocks/source`

	buildHeader = `
# a build block invokes sources and runs provisionning steps on them. The
# documentation for build blocks can be found here:
# https://www.packer.io/docs/from-1.5/blocks/build
build {
`
)

func (c *HCL2UpgradeCommand) RunContext(buildCtx context.Context, cla *HCL2UpgradeArgs) int {

	out := &bytes.Buffer{}
	var output io.Writer
	if err := os.MkdirAll(filepath.Dir(cla.OutputFile), 0); err != nil {
		c.Ui.Error(fmt.Sprintf("Failed to create output directory: %v", err))
		return 1
	}
	if f, err := os.Create(cla.OutputFile); err == nil {
		output = f
		defer f.Close()
	} else {
		c.Ui.Error(fmt.Sprintf("Failed to create output file: %v", err))
		return 1
	}

	if _, err := output.Write([]byte(hcl2UpgradeFileHeader)); err != nil {
		c.Ui.Error(fmt.Sprintf("Failed to write to file: %v", err))
		return 1
	}

	hdl, ret := c.GetConfigFromJSON(&cla.MetaArgs)
	if ret != 0 {
		return ret
	}

	core := hdl.(*CoreWrapper).Core
	if err := core.Initialize(); err != nil {
		c.Ui.Error(fmt.Sprintf("Initialization error, continuing: %v", err))
	}
	tpl := core.Template

	// Output variables section

	variables := []*template.Variable{}
	{
		// sort variables to avoid map's randomness

		for _, variable := range tpl.Variables {
			variables = append(variables, variable)
		}
		sort.Slice(variables, func(i, j int) bool {
			return variables[i].Key < variables[j].Key
		})
	}

	for _, variable := range variables {
		variablesContent := hclwrite.NewEmptyFile()
		variablesBody := variablesContent.Body()

		variableBody := variablesBody.AppendNewBlock("variable", []string{variable.Key}).Body()
		variableBody.SetAttributeRaw("type", hclwrite.Tokens{&hclwrite.Token{Bytes: []byte("string")}})

		if variable.Default != "" || !variable.Required {
			variableBody.SetAttributeValue("default", hcl2shim.HCL2ValueFromConfigValue(variable.Default))
		}
		if isSensitiveVariable(variable.Key, tpl.SensitiveVariables) {
			variableBody.SetAttributeValue("sensitive", cty.BoolVal(true))
		}
		variablesBody.AppendNewline()
		out.Write(transposeTemplatingCalls(variablesContent.Bytes()))
	}

	fmt.Fprintln(out, `# "timestamp" template function replacement`)
	fmt.Fprintln(out, `locals { timestamp = regex_replace(timestamp(), "[- TZ:]", "") }`)

	// Output sources section

	builders := []*template.Builder{}
	{
		// sort builders to avoid map's randomnes
		for _, builder := range tpl.Builders {
			builders = append(builders, builder)
		}
		sort.Slice(builders, func(i, j int) bool {
			return builders[i].Type+builders[i].Name < builders[j].Type+builders[j].Name
		})
	}

	out.Write([]byte(sourcesHeader))

	for i, builderCfg := range builders {
		sourcesContent := hclwrite.NewEmptyFile()
		body := sourcesContent.Body()

		body.AppendNewline()
		if !c.Meta.CoreConfig.Components.BuilderStore.Has(builderCfg.Type) {
			c.Ui.Error(fmt.Sprintf("unknown builder type: %q\n", builderCfg.Type))
			return 1
		}
		if builderCfg.Name == "" || builderCfg.Name == builderCfg.Type {
			builderCfg.Name = fmt.Sprintf("autogenerated_%d", i+1)
		}
		sourceBody := body.AppendNewBlock("source", []string{builderCfg.Type, builderCfg.Name}).Body()

		jsonBodyToHCL2Body(sourceBody, builderCfg.Config)

		_, _ = out.Write(transposeTemplatingCalls(sourcesContent.Bytes()))
	}

	// Output build section
	out.Write([]byte(buildHeader))

	buildContent := hclwrite.NewEmptyFile()
	buildBody := buildContent.Body()
	if tpl.Description != "" {
		buildBody.SetAttributeValue("description", cty.StringVal(tpl.Description))
		buildBody.AppendNewline()
	}

	sourceNames := []string{}
	for _, builder := range builders {
		sourceNames = append(sourceNames, fmt.Sprintf("source.%s.%s", builder.Type, builder.Name))
	}
	buildBody.SetAttributeValue("sources", hcl2shim.HCL2ValueFromConfigValue(sourceNames))
	buildBody.AppendNewline()
	_, _ = buildContent.WriteTo(out)

	for _, provisioner := range tpl.Provisioners {
		provisionerContent := hclwrite.NewEmptyFile()
		body := provisionerContent.Body()

		buildBody.AppendNewline()
		block := body.AppendNewBlock("provisioner", []string{provisioner.Type})
		cfg := provisioner.Config
		if len(provisioner.Except) > 0 {
			cfg["except"] = provisioner.Except
		}
		if len(provisioner.Only) > 0 {
			cfg["only"] = provisioner.Only
		}
		if provisioner.MaxRetries != "" {
			cfg["max_retries"] = provisioner.MaxRetries
		}
		if provisioner.Timeout > 0 {
			cfg["timeout"] = provisioner.Timeout.String()
		}
		jsonBodyToHCL2Body(block.Body(), cfg)

		out.Write(transposeTemplatingCalls(provisionerContent.Bytes()))
	}
	for _, pps := range tpl.PostProcessors {
		postProcessorContent := hclwrite.NewEmptyFile()
		body := postProcessorContent.Body()

		switch len(pps) {
		case 0:
			continue
		case 1:
		default:
			body = body.AppendNewBlock("post-processors", nil).Body()
		}
		for _, pp := range pps {
			ppBody := body.AppendNewBlock("post-processor", []string{pp.Type}).Body()
			if pp.KeepInputArtifact != nil {
				ppBody.SetAttributeValue("keep_input_artifact", cty.BoolVal(*pp.KeepInputArtifact))
			}
			cfg := pp.Config
			if len(pp.Except) > 0 {
				cfg["except"] = pp.Except
			}
			if len(pp.Only) > 0 {
				cfg["only"] = pp.Only
			}
			if pp.Name != "" && pp.Name != pp.Type {
				cfg["name"] = pp.Name
			}
			jsonBodyToHCL2Body(ppBody, cfg)
		}

		_, _ = out.Write(transposeTemplatingCalls(postProcessorContent.Bytes()))
	}

	_, _ = out.Write([]byte("}\n"))

	_, _ = output.Write(hclwrite.Format(out.Bytes()))

	c.Ui.Say(fmt.Sprintf("Successfully created %s ", cla.OutputFile))

	return 0
}

// transposeTemplatingCalls executes parts of blocks as go template files and replaces
// their result with their hcl2 variant. If something goes wrong the template
// containing the go template string is returned.
func transposeTemplatingCalls(s []byte) []byte {
	fallbackReturn := func(err error) []byte {
		return append([]byte(fmt.Sprintf("\n#could not parse template for following block: %q\n", err)), s...)
	}
	funcMap := texttemplate.FuncMap{
		"timestamp": func() string {
			return "${local.timestamp}"
		},
		"isotime": func() string {
			return "${local.timestamp}"
		},
		"user": func(in string) string {
			return fmt.Sprintf("${var.%s}", in)
		},
		"env": func(in string) string {
			return fmt.Sprintf("${var.%s}", in)
		},
		"build": func(a string) string {
			return fmt.Sprintf("${build.%s}", a)
		},
	}

	tpl, err := texttemplate.New("generated").
		Funcs(funcMap).
		Parse(string(s))

	if err != nil {
		return fallbackReturn(err)
	}

	str := &bytes.Buffer{}
	v := struct {
		HTTPIP   string
		HTTPPort string
	}{
		HTTPIP:   "{{ .HTTPIP }}",
		HTTPPort: "{{ .HTTPPort }}",
	}
	if err := tpl.Execute(str, v); err != nil {
		return fallbackReturn(err)
	}

	return str.Bytes()
}

func jsonBodyToHCL2Body(out *hclwrite.Body, kvs map[string]interface{}) {
	ks := []string{}
	for k := range kvs {
		ks = append(ks, k)
	}
	sort.Strings(ks)

	for _, k := range ks {
		value := kvs[k]

		switch value := value.(type) {
		case map[string]interface{}:
			var first interface{}
			for _, elem := range value {
				first = elem
			}

			switch first.(type) {
			case string, int, float64:
				out.SetAttributeValue(k, hcl2shim.HCL2ValueFromConfigValue(value))
			default:
				nestedBlockBody := out.AppendNewBlock(k, nil).Body()
				jsonBodyToHCL2Body(nestedBlockBody, value)
			}
		case []interface{}:
			if len(value) == 0 {
				continue
			}
			switch value[0].(type) {
			case map[string]interface{}:
				for i := range value {
					value := value[i].(map[string]interface{})
					nestedBlockBody := out.AppendNewBlock(k, nil).Body()
					jsonBodyToHCL2Body(nestedBlockBody, value)
				}
				continue
			default:
			}
			out.SetAttributeValue(k, hcl2shim.HCL2ValueFromConfigValue(value))
		default:
			out.SetAttributeValue(k, hcl2shim.HCL2ValueFromConfigValue(value))
		}
	}
}

func isSensitiveVariable(key string, vars []*template.Variable) bool {
	for _, v := range vars {
		if v.Key == key {
			return true
		}
	}
	return false
}

func (*HCL2UpgradeCommand) Help() string {
	helpText := `
Usage: packer hcl2_upgrade -output-file=JSON_TEMPLATE.pkr.hcl JSON_TEMPLATE...

  Will transform your JSON template to a HCL2 configuration.
`

	return strings.TrimSpace(helpText)
}

func (*HCL2UpgradeCommand) Synopsis() string {
	return "build image(s) from template"
}

func (*HCL2UpgradeCommand) AutocompleteArgs() complete.Predictor {
	return complete.PredictNothing
}

func (*HCL2UpgradeCommand) AutocompleteFlags() complete.Flags {
	return complete.Flags{}
}
<commit_msg>command/hcl2_upgrade: Update description text for command<commit_after>package command

import (
	"bytes"
	"context"
	"fmt"
	"io"
	"os"
	"path/filepath"
	"sort"
	"strings"
	texttemplate "text/template"

	"github.com/hashicorp/hcl/v2/hclwrite"
	hcl2shim "github.com/hashicorp/packer/hcl2template/shim"
	"github.com/hashicorp/packer/template"
	"github.com/posener/complete"
	"github.com/zclconf/go-cty/cty"
)

type HCL2UpgradeCommand struct {
	Meta
}

func (c *HCL2UpgradeCommand) Run(args []string) int {
	ctx, cleanup := handleTermInterrupt(c.Ui)
	defer cleanup()

	cfg, ret := c.ParseArgs(args)
	if ret != 0 {
		return ret
	}

	return c.RunContext(ctx, cfg)
}

func (c *HCL2UpgradeCommand) ParseArgs(args []string) (*HCL2UpgradeArgs, int) {
	var cfg HCL2UpgradeArgs
	flags := c.Meta.FlagSet("hcl2_upgrade", FlagSetNone)
	flags.Usage = func() { c.Ui.Say(c.Help()) }
	cfg.AddFlagSets(flags)
	if err := flags.Parse(args); err != nil {
		return &cfg, 1
	}
	args = flags.Args()
	if len(args) != 1 {
		flags.Usage()
		return &cfg, 1
	}
	cfg.Path = args[0]
	if cfg.OutputFile == "" {
		cfg.OutputFile = cfg.Path + ".pkr.hcl"
	}
	return &cfg, 0
}

const (
	hcl2UpgradeFileHeader = `# This file was autogenerate by the BETA 'packer hcl2_upgrade' command. We
# recommend double checking that everything is correct before going forward. We
# also recommend treating this file as disposable. The HCL2 blocks in this
# file can be moved to other files. For example, the variable blocks could be
# moved to their own 'variables.pkr.hcl' file, etc. Those files need to be
# suffixed with '.pkr.hcl' to be visible to Packer. To use multiple files at
# once they also need to be in the same folder. 'packer inspect folder/'
# will describe to you what is in that folder.

# All generated input variables will be of string type as this how Packer JSON
# views them; you can later on change their type. Read the variables type
# constraints documentation
# https://www.packer.io/docs/from-1.5/variables#type-constraints for more info.
`

	sourcesHeader = `
# source blocks are generated from your builders; a source can be referenced in
# build blocks. A build block runs provisioner and post-processors onto a
# source. Read the documentation for source blocks here:
# https://www.packer.io/docs/from-1.5/blocks/source`

	buildHeader = `
# a build block invokes sources and runs provisionning steps on them. The
# documentation for build blocks can be found here:
# https://www.packer.io/docs/from-1.5/blocks/build
build {
`
)

func (c *HCL2UpgradeCommand) RunContext(buildCtx context.Context, cla *HCL2UpgradeArgs) int {

	out := &bytes.Buffer{}
	var output io.Writer
	if err := os.MkdirAll(filepath.Dir(cla.OutputFile), 0); err != nil {
		c.Ui.Error(fmt.Sprintf("Failed to create output directory: %v", err))
		return 1
	}
	if f, err := os.Create(cla.OutputFile); err == nil {
		output = f
		defer f.Close()
	} else {
		c.Ui.Error(fmt.Sprintf("Failed to create output file: %v", err))
		return 1
	}

	if _, err := output.Write([]byte(hcl2UpgradeFileHeader)); err != nil {
		c.Ui.Error(fmt.Sprintf("Failed to write to file: %v", err))
		return 1
	}

	hdl, ret := c.GetConfigFromJSON(&cla.MetaArgs)
	if ret != 0 {
		return ret
	}

	core := hdl.(*CoreWrapper).Core
	if err := core.Initialize(); err != nil {
		c.Ui.Error(fmt.Sprintf("Initialization error, continuing: %v", err))
	}
	tpl := core.Template

	// Output variables section

	variables := []*template.Variable{}
	{
		// sort variables to avoid map's randomness

		for _, variable := range tpl.Variables {
			variables = append(variables, variable)
		}
		sort.Slice(variables, func(i, j int) bool {
			return variables[i].Key < variables[j].Key
		})
	}

	for _, variable := range variables {
		variablesContent := hclwrite.NewEmptyFile()
		variablesBody := variablesContent.Body()

		variableBody := variablesBody.AppendNewBlock("variable", []string{variable.Key}).Body()
		variableBody.SetAttributeRaw("type", hclwrite.Tokens{&hclwrite.Token{Bytes: []byte("string")}})

		if variable.Default != "" || !variable.Required {
			variableBody.SetAttributeValue("default", hcl2shim.HCL2ValueFromConfigValue(variable.Default))
		}
		if isSensitiveVariable(variable.Key, tpl.SensitiveVariables) {
			variableBody.SetAttributeValue("sensitive", cty.BoolVal(true))
		}
		variablesBody.AppendNewline()
		out.Write(transposeTemplatingCalls(variablesContent.Bytes()))
	}

	fmt.Fprintln(out, `# "timestamp" template function replacement`)
	fmt.Fprintln(out, `locals { timestamp = regex_replace(timestamp(), "[- TZ:]", "") }`)

	// Output sources section

	builders := []*template.Builder{}
	{
		// sort builders to avoid map's randomnes
		for _, builder := range tpl.Builders {
			builders = append(builders, builder)
		}
		sort.Slice(builders, func(i, j int) bool {
			return builders[i].Type+builders[i].Name < builders[j].Type+builders[j].Name
		})
	}

	out.Write([]byte(sourcesHeader))

	for i, builderCfg := range builders {
		sourcesContent := hclwrite.NewEmptyFile()
		body := sourcesContent.Body()

		body.AppendNewline()
		if !c.Meta.CoreConfig.Components.BuilderStore.Has(builderCfg.Type) {
			c.Ui.Error(fmt.Sprintf("unknown builder type: %q\n", builderCfg.Type))
			return 1
		}
		if builderCfg.Name == "" || builderCfg.Name == builderCfg.Type {
			builderCfg.Name = fmt.Sprintf("autogenerated_%d", i+1)
		}
		sourceBody := body.AppendNewBlock("source", []string{builderCfg.Type, builderCfg.Name}).Body()

		jsonBodyToHCL2Body(sourceBody, builderCfg.Config)

		_, _ = out.Write(transposeTemplatingCalls(sourcesContent.Bytes()))
	}

	// Output build section
	out.Write([]byte(buildHeader))

	buildContent := hclwrite.NewEmptyFile()
	buildBody := buildContent.Body()
	if tpl.Description != "" {
		buildBody.SetAttributeValue("description", cty.StringVal(tpl.Description))
		buildBody.AppendNewline()
	}

	sourceNames := []string{}
	for _, builder := range builders {
		sourceNames = append(sourceNames, fmt.Sprintf("source.%s.%s", builder.Type, builder.Name))
	}
	buildBody.SetAttributeValue("sources", hcl2shim.HCL2ValueFromConfigValue(sourceNames))
	buildBody.AppendNewline()
	_, _ = buildContent.WriteTo(out)

	for _, provisioner := range tpl.Provisioners {
		provisionerContent := hclwrite.NewEmptyFile()
		body := provisionerContent.Body()

		buildBody.AppendNewline()
		block := body.AppendNewBlock("provisioner", []string{provisioner.Type})
		cfg := provisioner.Config
		if len(provisioner.Except) > 0 {
			cfg["except"] = provisioner.Except
		}
		if len(provisioner.Only) > 0 {
			cfg["only"] = provisioner.Only
		}
		if provisioner.MaxRetries != "" {
			cfg["max_retries"] = provisioner.MaxRetries
		}
		if provisioner.Timeout > 0 {
			cfg["timeout"] = provisioner.Timeout.String()
		}
		jsonBodyToHCL2Body(block.Body(), cfg)

		out.Write(transposeTemplatingCalls(provisionerContent.Bytes()))
	}
	for _, pps := range tpl.PostProcessors {
		postProcessorContent := hclwrite.NewEmptyFile()
		body := postProcessorContent.Body()

		switch len(pps) {
		case 0:
			continue
		case 1:
		default:
			body = body.AppendNewBlock("post-processors", nil).Body()
		}
		for _, pp := range pps {
			ppBody := body.AppendNewBlock("post-processor", []string{pp.Type}).Body()
			if pp.KeepInputArtifact != nil {
				ppBody.SetAttributeValue("keep_input_artifact", cty.BoolVal(*pp.KeepInputArtifact))
			}
			cfg := pp.Config
			if len(pp.Except) > 0 {
				cfg["except"] = pp.Except
			}
			if len(pp.Only) > 0 {
				cfg["only"] = pp.Only
			}
			if pp.Name != "" && pp.Name != pp.Type {
				cfg["name"] = pp.Name
			}
			jsonBodyToHCL2Body(ppBody, cfg)
		}

		_, _ = out.Write(transposeTemplatingCalls(postProcessorContent.Bytes()))
	}

	_, _ = out.Write([]byte("}\n"))

	_, _ = output.Write(hclwrite.Format(out.Bytes()))

	c.Ui.Say(fmt.Sprintf("Successfully created %s ", cla.OutputFile))

	return 0
}

// transposeTemplatingCalls executes parts of blocks as go template files and replaces
// their result with their hcl2 variant. If something goes wrong the template
// containing the go template string is returned.
func transposeTemplatingCalls(s []byte) []byte {
	fallbackReturn := func(err error) []byte {
		return append([]byte(fmt.Sprintf("\n#could not parse template for following block: %q\n", err)), s...)
	}
	funcMap := texttemplate.FuncMap{
		"timestamp": func() string {
			return "${local.timestamp}"
		},
		"isotime": func() string {
			return "${local.timestamp}"
		},
		"user": func(in string) string {
			return fmt.Sprintf("${var.%s}", in)
		},
		"env": func(in string) string {
			return fmt.Sprintf("${var.%s}", in)
		},
		"build": func(a string) string {
			return fmt.Sprintf("${build.%s}", a)
		},
	}

	tpl, err := texttemplate.New("generated").
		Funcs(funcMap).
		Parse(string(s))

	if err != nil {
		return fallbackReturn(err)
	}

	str := &bytes.Buffer{}
	v := struct {
		HTTPIP   string
		HTTPPort string
	}{
		HTTPIP:   "{{ .HTTPIP }}",
		HTTPPort: "{{ .HTTPPort }}",
	}
	if err := tpl.Execute(str, v); err != nil {
		return fallbackReturn(err)
	}

	return str.Bytes()
}

func jsonBodyToHCL2Body(out *hclwrite.Body, kvs map[string]interface{}) {
	ks := []string{}
	for k := range kvs {
		ks = append(ks, k)
	}
	sort.Strings(ks)

	for _, k := range ks {
		value := kvs[k]

		switch value := value.(type) {
		case map[string]interface{}:
			var first interface{}
			for _, elem := range value {
				first = elem
			}

			switch first.(type) {
			case string, int, float64:
				out.SetAttributeValue(k, hcl2shim.HCL2ValueFromConfigValue(value))
			default:
				nestedBlockBody := out.AppendNewBlock(k, nil).Body()
				jsonBodyToHCL2Body(nestedBlockBody, value)
			}
		case []interface{}:
			if len(value) == 0 {
				continue
			}
			switch value[0].(type) {
			case map[string]interface{}:
				for i := range value {
					value := value[i].(map[string]interface{})
					nestedBlockBody := out.AppendNewBlock(k, nil).Body()
					jsonBodyToHCL2Body(nestedBlockBody, value)
				}
				continue
			default:
			}
			out.SetAttributeValue(k, hcl2shim.HCL2ValueFromConfigValue(value))
		default:
			out.SetAttributeValue(k, hcl2shim.HCL2ValueFromConfigValue(value))
		}
	}
}

func isSensitiveVariable(key string, vars []*template.Variable) bool {
	for _, v := range vars {
		if v.Key == key {
			return true
		}
	}
	return false
}

func (*HCL2UpgradeCommand) Help() string {
	helpText := `
Usage: packer hcl2_upgrade -output-file=JSON_TEMPLATE.pkr.hcl JSON_TEMPLATE...

  Will transform your JSON template to a HCL2 configuration.
`

	return strings.TrimSpace(helpText)
}

func (*HCL2UpgradeCommand) Synopsis() string {
	return "transform a JSON template into a HCL2 configuration"
}

func (*HCL2UpgradeCommand) AutocompleteArgs() complete.Predictor {
	return complete.PredictNothing
}

func (*HCL2UpgradeCommand) AutocompleteFlags() complete.Flags {
	return complete.Flags{}
}
<|endoftext|>


<commit_before>package postgresql

import (
	"fmt"
	"strings"
	"time"

	"github.com/hashicorp/vault/logical"
	"github.com/hashicorp/vault/logical/framework"
	"github.com/pquerna/totp"
)

// Update with TOTP values
func pathRoleCreate(b *backend) *framework.Path {
	return &framework.Path{
		Pattern: "creds/" + framework.GenericNameRegex("name"),
		Fields: map[string]*framework.FieldSchema{
			"name": &framework.FieldSchema{
				Type:        framework.TypeString,
				Description: "Name of the role.",
			},
		},

		Callbacks: map[logical.Operation]framework.OperationFunc{
			logical.ReadOperation: b.pathRoleCreateRead,
		},

		HelpSynopsis:    pathRoleCreateReadHelpSyn,
		HelpDescription: pathRoleCreateReadHelpDesc,
	}
}

func (b *backend) pathRoleCreateRead(
	req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
	b.logger.Trace("totp/pathRoleCreateRead: enter")
	defer b.logger.Trace("totp/pathRoleCreateRead: exit")

	name := data.Get("name").(string)

	// Get the role
	b.logger.Trace("totp/pathRoleCreateRead: getting role")
	role, err := b.Role(req.Storage, name)
	if err != nil {
		return nil, err
	}
	if role == nil {
		return logical.ErrorResponse(fmt.Sprintf("unknown role: %s", name)), nil
	}

	// Generate TOTP token
	/*
		//Generate key using totp library
		totpKey, err := totp.GenerateCodeCustom(role.key, time.Now().UTC(), ValdidateOpts{
			Period: role.period,
			Skew: 1,
			Digits: otp.DigitsSix
			Algorithm: otp.AlgorithmSHA1
		});

		if err != nil {
			return nil, err
		}
	*/

	// Return the secret
	b.logger.Trace("totp/pathRoleCreateRead: generating secret")

	/*
			return &logical.Response{
			Data: map[string]interface{}{
				"token":            totpKey,
			},
		}, nil
	*/
	return resp, nil
}

// Update help strings
const pathRoleCreateReadHelpSyn = `
Request database credentials for a certain role.
`

const pathRoleCreateReadHelpDesc = `
This path reads database credentials for a certain role. The
database credentials will be generated on demand and will be automatically
revoked when the lease is up.
`
<commit_msg>Updated TOTP path_role_create.go's structure and help strings<commit_after>package totp

import (
	"fmt"
	"time"

	"github.com/hashicorp/vault/logical"
	"github.com/hashicorp/vault/logical/framework"
	"github.com/pquerna/otp/totp"
)

func pathRoleCreate(b *backend) *framework.Path {
	return &framework.Path{
		Pattern: "creds/" + framework.GenericNameRegex("name"),
		Fields: map[string]*framework.FieldSchema{
			"name": &framework.FieldSchema{
				Type:        framework.TypeString,
				Description: "Name of the role.",
			},
		},

		Callbacks: map[logical.Operation]framework.OperationFunc{
			logical.ReadOperation: b.pathRoleCreateRead,
		},

		HelpSynopsis:    pathRoleCreateReadHelpSyn,
		HelpDescription: pathRoleCreateReadHelpDesc,
	}
}

func (b *backend) pathRoleCreateRead(
	req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
	b.logger.Trace("totp/pathRoleCreateRead: enter")
	defer b.logger.Trace("totp/pathRoleCreateRead: exit")

	name := data.Get("name").(string)

	// Get the role
	b.logger.Trace("totp/pathRoleCreateRead: getting role")
	role, err := b.Role(req.Storage, name)
	if err != nil {
		return nil, err
	}
	if role == nil {
		return logical.ErrorResponse(fmt.Sprintf("unknown role: %s", name)), nil
	}

	// Generate password using totp library
	totpToken, err := totp.GenerateCodeCustom(role.Key, time.Now().UTC(), ValdidateOpts{
		Period:    role.Period,
		Digits:    role.Digits,
		Algorithm: role.Algorithm,
	})

	if err != nil {
		return nil, err
	}

	// Return the secret
	b.logger.Trace("totp/pathRoleCreateRead: generating secret")

	resp := &logical.Response{
		Data: map[string]interface{}{
			"token": totpToken,
		},
	}, nil

	return resp, nil
}

const pathRoleCreateReadHelpSyn = `
Request time-based one-time use password for a certain role.
`
const pathRoleCreateReadHelpDesc = `
This path generates a time-based one-time use password for a certain role.
`
<|endoftext|>


<commit_before>package http

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"

	"github.com/micro/go-micro/v2/client/selector"
	"github.com/micro/go-micro/v2/registry"
)

// Write sets the status and body on a http ResponseWriter
func Write(w http.ResponseWriter, contentType string, status int, body string) {
	w.Header().Set("Content-Length", fmt.Sprintf("%v", len(body)))
	w.Header().Set("Content-Type", contentType)
	w.WriteHeader(status)
	fmt.Fprintf(w, `%v`, body)
}

// WriteBadRequestError sets a 400 status code
func WriteBadRequestError(w http.ResponseWriter, err error) {
	rawBody, err := json.Marshal(map[string]string{
		"error": err.Error(),
	})
	if err != nil {
		WriteInternalServerError(w, err)
		return
	}
	Write(w, "application/json", 400, string(rawBody))
}

// WriteInternalServerError sets a 500 status code
func WriteInternalServerError(w http.ResponseWriter, err error) {
	rawBody, err := json.Marshal(map[string]string{
		"error": err.Error(),
	})
	if err != nil {
		log.Println(err)
		return
	}
	Write(w, "application/json", 500, string(rawBody))
}

func NewRoundTripper(opts ...Option) http.RoundTripper {
	options := Options{
		Registry: registry.DefaultRegistry,
	}
	for _, o := range opts {
		o(&options)
	}

	return &roundTripper{
		rt:   http.DefaultTransport,
		st:   selector.Random,
		opts: options,
	}
}
<commit_msg>Auth util func RequestToContext (#1386)<commit_after>package http

import (
	"context"
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"strings"

	"github.com/micro/go-micro/v2/client/selector"
	"github.com/micro/go-micro/v2/metadata"
	"github.com/micro/go-micro/v2/registry"
)

// Write sets the status and body on a http ResponseWriter
func Write(w http.ResponseWriter, contentType string, status int, body string) {
	w.Header().Set("Content-Length", fmt.Sprintf("%v", len(body)))
	w.Header().Set("Content-Type", contentType)
	w.WriteHeader(status)
	fmt.Fprintf(w, `%v`, body)
}

// WriteBadRequestError sets a 400 status code
func WriteBadRequestError(w http.ResponseWriter, err error) {
	rawBody, err := json.Marshal(map[string]string{
		"error": err.Error(),
	})
	if err != nil {
		WriteInternalServerError(w, err)
		return
	}
	Write(w, "application/json", 400, string(rawBody))
}

// WriteInternalServerError sets a 500 status code
func WriteInternalServerError(w http.ResponseWriter, err error) {
	rawBody, err := json.Marshal(map[string]string{
		"error": err.Error(),
	})
	if err != nil {
		log.Println(err)
		return
	}
	Write(w, "application/json", 500, string(rawBody))
}

func NewRoundTripper(opts ...Option) http.RoundTripper {
	options := Options{
		Registry: registry.DefaultRegistry,
	}
	for _, o := range opts {
		o(&options)
	}

	return &roundTripper{
		rt:   http.DefaultTransport,
		st:   selector.Random,
		opts: options,
	}
}

// RequestToContext puts the `Authorization` header bearer token into context
// so calls to services will be authorized.
func RequestToContext(r *http.Request) context.Context {
	ctx := context.Background()
	md := make(metadata.Metadata)
	for k, v := range r.Header {
		md[k] = strings.Join(v, ",")
	}
	return metadata.NewContext(ctx, md)
}
<|endoftext|>


<commit_before>package alphabet

const (
	A = [][]int{[]int{0, 1, 1, 0}, []int{1, 0, 0, 1}, []int{1, 0, 0, 1}, []int{1, 1, 1, 1}, []int{1, 0, 0, 1}, []int{1, 0, 0, 1}}
	B = [][]int{[]int{1, 1, 1, 0}, []int{1, 0, 0, 1}, []int{1, 1, 1, 0}, []int{1, 0, 0, 1}, []int{1, 0, 0, 1}, []int{1, 1, 1, 0}}
	C = [][]int{[]int{0, 1, 1, 0}, []int{1, 0, 0, 1}, []int{1, 0, 0, 0}, []int{1, 0, 0, 0}, []int{1, 0, 0, 1}, []int{0, 1, 1, 0}}
	D = [][]int{[]int{1, 1, 1, 0}, []int{1, 0, 0, 1}, []int{1, 0, 0, 1}, []int{1, 0, 0, 1}, []int{1, 0, 0, 1}, []int{1, 1, 1, 0}}
	E = [][]int{[]int{1, 1, 1, 1}, []int{1, 0, 0, 0}, []int{1, 1, 1, 0}, []int{1, 0, 0, 0}, []int{1, 0, 0, 0}, []int{1, 1, 1, 1}}
	F = [][]int{[]int{1, 1, 1, 1}, []int{1, 0, 0, 0}, []int{1, 1, 1, 0}, []int{1, 0, 0, 0}, []int{1, 0, 0, 0}, []int{1, 0, 0, 0}}
	G = [][]int{[]int{1, 1, 1, 1}, []int{1, 0, 0, 1}, []int{1, 0, 0, 0}, []int{1, 0, 1, 1}, []int{1, 1, 1, 1}}
	H = [][]int{[]int{1, 0, 0, 1}, []int{1, 0, 0, 1}, []int{1, 1, 1, 1}, []int{1, 0, 0, 1}, []int{1, 0, 0, 1}, []int{1, 0, 0, 1}}
	I = [][]int{[]int{1}, []int{1}, []int{1}, []int{1}, []int{1}, []int{1}}
	J = [][]int{[]int{0, 0, 0, 1}, []int{0, 0, 0, 1}, []int{0, 0, 0, 1}, []int{0, 0, 0, 1}, []int{1, 0, 0, 1}, []int{0, 1, 1, 0}}
	K = [][]int{[]int{1, 0, 0, 1}, []int{1, 0, 1, 1}, []int{1, 1, 0, 0}, []int{1, 0, 1, 1}, []int{1, 0, 0, 1}, []int{1, 0, 0, 1}}
	L = [][]int{[]int{1, 0, 0, 0}, []int{1, 0, 0, 0}, []int{1, 0, 0, 0}, []int{1, 0, 0, 0}, []int{1, 0, 0, 0}, []int{1, 1, 1, 1}}
	M = [][]int{[]int{1, 0, 0, 0, 1}, []int{1, 1, 0, 1, 1}, []int{1, 0, 1, 0, 1}, []int{1, 0, 1, 0, 1}, []int{1, 0, 0, 0, 1}, []int{1, 0, 0, 0, 1}}
	N = [][]int{[]int{1, 1, 0, 0, 1}, []int{1, 1, 0, 0, 1}, []int{1, 0, 1, 0, 1}, []int{1, 0, 1, 0, 1}, []int{1, 0, 0, 1, 1}, []int{1, 0, 0, 1, 1}}
	O = [][]int{[]int{1, 1, 1, 1}, []int{1, 0, 0, 1}, []int{1, 0, 0, 1}, []int{1, 0, 0, 1}, []int{1, 0, 0, 1}, []int{1, 1, 1, 1}}
	P = [][]int{[]int{1, 1, 1, 1}, []int{1, 0, 0, 1}, []int{1, 1, 1, 1}, []int{1, 0, 0, 0}, []int{1, 0, 0, 0}, []int{1, 0, 0, 0}}
	Q = [][]int{[]int{0, 1, 1, 1, 0}, []int{1, 0, 0, 0, 1}, []int{1, 0, 0, 0, 1}, []int{1, 0, 1, 0, 1}, []int{1, 0, 0, 1, 1}, []int{0, 1, 1, 1, 0}}
	R = [][]int{[]int{1, 1, 1, 0}, []int{1, 0, 0, 1}, []int{1, 1, 1, 0}, []int{1, 1, 0, 0}, []int{1, 0, 1, 0}, []int{1, 0, 0, 1}}
	S = [][]int{[]int{1, 1, 1, 1}, []int{1, 0, 0, 0}, []int{1, 1, 1, 1}, []int{0, 0, 0, 1}, []int{0, 0, 0, 1}, []int{1, 1, 1, 1}}
	T = [][]int{[]int{1, 1, 1, 1, 1}, []int{0, 0, 1, 0, 0}, []int{0, 0, 1, 0, 0}, []int{0, 0, 1, 0, 0}, []int{0, 0, 1, 0, 0}, []int{0, 0, 1, 0, 0}}
	U = [][]int{[]int{1, 0, 0, 1}, []int{1, 0, 0, 1}, []int{1, 0, 0, 1}, []int{1, 0, 0, 1}, []int{1, 0, 0, 1}, []int{0, 1, 1, 0}}
	V = [][]int{[]int{1, 0, 0, 0, 1}, []int{1, 0, 0, 0, 1}, []int{1, 0, 0, 0, 1}, []int{0, 1, 0, 1, 0}, []int{0, 1, 0, 1, 0}, []int{0, 0, 1, 0, 0}}
	W = [][]int{[]int{1, 0, 0, 0, 1}, []int{1, 0, 0, 0, 1}, []int{1, 0, 1, 0, 1}, []int{1, 0, 1, 0, 1}, []int{1, 1, 1, 1, 1}, []int{0, 1, 0, 1, 0}}
	X = [][]int{[]int{1, 0, 0, 0, 1}, []int{0, 1, 0, 1, 0}, []int{0, 0, 1, 0, 0}, []int{0, 0, 1, 0, 0}, []int{0, 1, 0, 1, 0}, []int{1, 0, 0, 0, 1}}
	Y = [][]int{[]int{1, 0, 0, 0, 1}, []int{0, 1, 0, 1, 0}, []int{0, 0, 1, 0, 0}, []int{0, 0, 1, 0, 0}, []int{0, 0, 1, 0, 0}, []int{0, 0, 1, 0, 0}}
	Z = [][]int{[]int{1, 1, 1, 1}, []int{0, 0, 0, 1}, []int{0, 0, 1, 0}, []int{0, 1, 0, 0}, []int{1, 0, 0, 0}, []int{1, 1, 1, 1}}

	SPACE = [][]int{[]int{0}, []int{0}, []int{0}, []int{0}, []int{0}, []int{0}}
)
<commit_msg>Implement translation function.<commit_after>package utils

import (
	"errors"
)

var A = [][]int{[]int{0, 1, 1, 0}, []int{1, 0, 0, 1}, []int{1, 0, 0, 1}, []int{1, 1, 1, 1}, []int{1, 0, 0, 1}, []int{1, 0, 0, 1}}
var B = [][]int{[]int{1, 1, 1, 0}, []int{1, 0, 0, 1}, []int{1, 1, 1, 0}, []int{1, 0, 0, 1}, []int{1, 0, 0, 1}, []int{1, 1, 1, 0}}
var C = [][]int{[]int{0, 1, 1, 0}, []int{1, 0, 0, 1}, []int{1, 0, 0, 0}, []int{1, 0, 0, 0}, []int{1, 0, 0, 1}, []int{0, 1, 1, 0}}
var D = [][]int{[]int{1, 1, 1, 0}, []int{1, 0, 0, 1}, []int{1, 0, 0, 1}, []int{1, 0, 0, 1}, []int{1, 0, 0, 1}, []int{1, 1, 1, 0}}
var E = [][]int{[]int{1, 1, 1, 1}, []int{1, 0, 0, 0}, []int{1, 1, 1, 0}, []int{1, 0, 0, 0}, []int{1, 0, 0, 0}, []int{1, 1, 1, 1}}
var F = [][]int{[]int{1, 1, 1, 1}, []int{1, 0, 0, 0}, []int{1, 1, 1, 0}, []int{1, 0, 0, 0}, []int{1, 0, 0, 0}, []int{1, 0, 0, 0}}
var G = [][]int{[]int{1, 1, 1, 1}, []int{1, 0, 0, 1}, []int{1, 0, 0, 0}, []int{1, 0, 1, 1}, []int{1, 1, 1, 1}}
var H = [][]int{[]int{1, 0, 0, 1}, []int{1, 0, 0, 1}, []int{1, 1, 1, 1}, []int{1, 0, 0, 1}, []int{1, 0, 0, 1}, []int{1, 0, 0, 1}}
var I = [][]int{[]int{1}, []int{1}, []int{1}, []int{1}, []int{1}, []int{1}}
var J = [][]int{[]int{0, 0, 0, 1}, []int{0, 0, 0, 1}, []int{0, 0, 0, 1}, []int{0, 0, 0, 1}, []int{1, 0, 0, 1}, []int{0, 1, 1, 0}}
var K = [][]int{[]int{1, 0, 0, 1}, []int{1, 0, 1, 1}, []int{1, 1, 0, 0}, []int{1, 0, 1, 1}, []int{1, 0, 0, 1}, []int{1, 0, 0, 1}}
var L = [][]int{[]int{1, 0, 0, 0}, []int{1, 0, 0, 0}, []int{1, 0, 0, 0}, []int{1, 0, 0, 0}, []int{1, 0, 0, 0}, []int{1, 1, 1, 1}}
var M = [][]int{[]int{1, 0, 0, 0, 1}, []int{1, 1, 0, 1, 1}, []int{1, 0, 1, 0, 1}, []int{1, 0, 1, 0, 1}, []int{1, 0, 0, 0, 1}, []int{1, 0, 0, 0, 1}}
var N = [][]int{[]int{1, 1, 0, 0, 1}, []int{1, 1, 0, 0, 1}, []int{1, 0, 1, 0, 1}, []int{1, 0, 1, 0, 1}, []int{1, 0, 0, 1, 1}, []int{1, 0, 0, 1, 1}}
var O = [][]int{[]int{1, 1, 1, 1}, []int{1, 0, 0, 1}, []int{1, 0, 0, 1}, []int{1, 0, 0, 1}, []int{1, 0, 0, 1}, []int{1, 1, 1, 1}}
var P = [][]int{[]int{1, 1, 1, 1}, []int{1, 0, 0, 1}, []int{1, 1, 1, 1}, []int{1, 0, 0, 0}, []int{1, 0, 0, 0}, []int{1, 0, 0, 0}}
var Q = [][]int{[]int{0, 1, 1, 1, 0}, []int{1, 0, 0, 0, 1}, []int{1, 0, 0, 0, 1}, []int{1, 0, 1, 0, 1}, []int{1, 0, 0, 1, 1}, []int{0, 1, 1, 1, 0}}
var R = [][]int{[]int{1, 1, 1, 0}, []int{1, 0, 0, 1}, []int{1, 1, 1, 0}, []int{1, 1, 0, 0}, []int{1, 0, 1, 0}, []int{1, 0, 0, 1}}
var S = [][]int{[]int{1, 1, 1, 1}, []int{1, 0, 0, 0}, []int{1, 1, 1, 1}, []int{0, 0, 0, 1}, []int{0, 0, 0, 1}, []int{1, 1, 1, 1}}
var T = [][]int{[]int{1, 1, 1, 1, 1}, []int{0, 0, 1, 0, 0}, []int{0, 0, 1, 0, 0}, []int{0, 0, 1, 0, 0}, []int{0, 0, 1, 0, 0}, []int{0, 0, 1, 0, 0}}
var U = [][]int{[]int{1, 0, 0, 1}, []int{1, 0, 0, 1}, []int{1, 0, 0, 1}, []int{1, 0, 0, 1}, []int{1, 0, 0, 1}, []int{0, 1, 1, 0}}
var V = [][]int{[]int{1, 0, 0, 0, 1}, []int{1, 0, 0, 0, 1}, []int{1, 0, 0, 0, 1}, []int{0, 1, 0, 1, 0}, []int{0, 1, 0, 1, 0}, []int{0, 0, 1, 0, 0}}
var W = [][]int{[]int{1, 0, 0, 0, 1}, []int{1, 0, 0, 0, 1}, []int{1, 0, 1, 0, 1}, []int{1, 0, 1, 0, 1}, []int{1, 1, 1, 1, 1}, []int{0, 1, 0, 1, 0}}
var X = [][]int{[]int{1, 0, 0, 0, 1}, []int{0, 1, 0, 1, 0}, []int{0, 0, 1, 0, 0}, []int{0, 0, 1, 0, 0}, []int{0, 1, 0, 1, 0}, []int{1, 0, 0, 0, 1}}
var Y = [][]int{[]int{1, 0, 0, 0, 1}, []int{0, 1, 0, 1, 0}, []int{0, 0, 1, 0, 0}, []int{0, 0, 1, 0, 0}, []int{0, 0, 1, 0, 0}, []int{0, 0, 1, 0, 0}}
var Z = [][]int{[]int{1, 1, 1, 1}, []int{0, 0, 0, 1}, []int{0, 0, 1, 0}, []int{0, 1, 0, 0}, []int{1, 0, 0, 0}, []int{1, 1, 1, 1}}

var SPACE = [][]int{[]int{0}, []int{0}, []int{0}, []int{0}, []int{0}, []int{0}}

// TranslateLetter returns the bitmap like [][]int of the given letter.
func TranslateLetter(letter string) ([][]int, error) {
	var err error
	switch {
	case letter == "a":
		return A, err
	case letter == "b":
		return B, err
	case letter == "c":
		return C, err
	case letter == "d":
		return D, err
	case letter == "e":
		return E, err
	case letter == "f":
		return F, err
	case letter == "g":
		return G, err
	case letter == "h":
		return H, err
	case letter == "i":
		return I, err
	case letter == "j":
		return J, err
	case letter == "k":
		return K, err
	case letter == "l":
		return L, err
	case letter == "m":
		return M, err
	case letter == "n":
		return N, err
	case letter == "o":
		return O, err
	case letter == "p":
		return P, err
	case letter == "q":
		return Q, err
	case letter == "r":
		return R, err
	case letter == "s":
		return S, err
	case letter == "t":
		return T, err
	case letter == "u":
		return U, err
	case letter == "v":
		return V, err
	case letter == "w":
		return W, err
	case letter == "x":
		return X, err
	case letter == "y":
		return Y, err
	case letter == "z":
		return Z, err
	case letter == " ":
		return SPACE, err
	default:
		return nil, errors.New("Letter must be lowercase: [a-z]{1}")
	}
}
<|endoftext|>


<commit_before>package logger

import (
	"fmt"
	"log"
	"os"
	"testing"
)

var file *os.File
var logger *log.Logger

func init() {
	var err error
	file, err = createLogFile("./testLog/", "hello.txt")
	if err != nil {
		panic(err)
	}
	logger = log.New(os.Stderr,
		"Test Log :: ",
		log.Ldate|log.Ltime|log.Lshortfile)
}

// checking if file created is exist
func TestCreateLogFile(t *testing.T) {
	//checking file location
	_, err := os.Stat("./testLog/hello.txt")
	if err != nil {
		t.Errorf("Expecting the file exist got = %v\n", err)
	}
}

func TestWriteLog(t *testing.T) {
	filePath := "./testLog/"
	fileName := "hello.txt"
	inputData := "data log here"

	// set input error
	logger.SetOutput(file)
	logger.Println(inputData)

	//checking file location
	_, err := os.Stat(filePath + fileName)
	if err != nil {
		t.Error(err)
	}

	//opening file
	f, err := os.OpenFile(filePath+fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, os.ModePerm)
	if err != nil {
		t.Error(err)
	}

	//get the data from the file
	result := make([]byte, 100)
	_, err = f.Read(result)
	if err != nil {
		t.Error(err)
	}

	if len(result) == 0 {
		t.Error("Log Data is Empty")
	}

	//convert byte to string
	stringResult := string(result)
	fmt.Println("Result Read files = ", stringResult)
}
<commit_msg>call logger inside Test<commit_after>package logger

import (
	"fmt"
	"log"
	"os"
	"testing"
)

var file *os.File

func init() {
	var err error
	file, err = createLogFile("./testLog/", "hello.txt")
	if err != nil {
		panic(err)
	}
}

// checking if file created is exist
func TestCreateLogFile(t *testing.T) {
	//checking file location
	_, err := os.Stat("./testLog/hello.txt")
	if err != nil {
		t.Errorf("Expecting the file exist got = %v\n", err)
	}
}

func TestWriteLog(t *testing.T) {
	filePath := "./testLog/"
	fileName := "hello.txt"
	inputData := "data log here"

	// set input error
	logger := log.New(os.Stderr,
		"Test Log :: ",
		log.Ldate|log.Ltime|log.Lshortfile)
	logger.SetOutput(file)
	logger.Println(inputData)

	//checking file location
	_, err := os.Stat(filePath + fileName)
	if err != nil {
		t.Errorf("Error file is not exist = %v\n", err)
	}

	//opening file
	f, err := os.OpenFile(filePath+fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, os.ModePerm)
	if err != nil {
		t.Errorf("Error opening file = %v\n", err)
	}

	//get the data from the file
	result := make([]byte, 100)
	_, err = f.Read(result)
	if err != nil {
		t.Errorf("Cannot read file = %v\n", err)
	}

	if len(result) == 0 {
		t.Error("Log Data is Empty")
	}

	//convert byte to string
	stringResult := string(result)
	fmt.Println("Result Read files = ", stringResult)
}
<|endoftext|>


<commit_before>package colonycore

import (
	"github.com/eaciit/orm/v1"
)

type DataGrabber struct {
	orm.ModelBase
	ID string `json:"_id",bson:"_id"`

	DataSourceOrigin      string
	DataSourceDestination string

	IsFromWizard          bool
	ConnectionOrigin      string
	ConnectionDestination string
	TableOrigin           string
	TableDestination      string

	UseInterval         bool
	IntervalType        string
	GrabInterval        int32
	TimeoutInterval     int32
	Maps                []*Map
	RunAt               []string
	PreTransferCommand  string
	PostTransferCommand string
}

type Map struct {
	Destination     string
	DestinationType string
	Source          string
	SourceType      string
}

func (c *DataGrabber) TableName() string {
	return "datagrabbers"
}

func (c *DataGrabber) RecordID() interface{} {
	return c.ID
}
<commit_msg>no message<commit_after>package colonycore

import (
	"github.com/eaciit/orm/v1"
)

type DataGrabber struct {
	orm.ModelBase
	ID string `json:"_id",bson:"_id"`

	DataSourceOrigin      string
	DataSourceDestination string

	IsFromWizard          bool
	ConnectionOrigin      string
	ConnectionDestination string
	TableOrigin           string
	TableDestination      string

	UseInterval         bool
	IntervalType        string
	GrabInterval        int32
	TimeoutInterval     int32
	Maps                []*Map
	RunAt               []string
	PreTransferCommand  string
	PostTransferCommand string
}

type Map struct {
	Destination     string
	DestinationType string
	Source          string
	SourceType      string
}

func (c *DataGrabber) TableName() string {
	return "datagrabbers"
}

func (c *DataGrabber) RecordID() interface{} {
	return c.ID
}

type WizardTransformation struct {
	TableSource      string
	TableDestination string
}
type Wizard struct {
	ConnectionSource      string
	ConnectionDestination string
	Transformations       []*WizardTransformation
}
<|endoftext|>


<commit_before>package srcgraph

import (
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"os/exec"

	"strings"

	"github.com/aybabtme/color/brush"
	"github.com/sourcegraph/go-vcs"
	"github.com/sourcegraph/makex"
	"sourcegraph.com/sourcegraph/srcgraph/build"
	"sourcegraph.com/sourcegraph/srcgraph/buildstore"
	"sourcegraph.com/sourcegraph/srcgraph/task2"
)

func makefile(args []string) {
	make_(append(args, "-mf"))
}

func make_(args []string) {
	params := mustParseMakeParams(args)
	if err := params.verify(); err != nil {
		log.Fatal(err)
	}
	repoConfig := params.RepositoryConfig.GetRepositoryConfig(task2.DefaultContext)

	repoStore, err := buildstore.NewRepositoryStore(params.Repository.RootDir)
	if err != nil {
		log.Fatal(err)
	}

	// Get build directory (${REPO}/.sourcegraph-data/...)
	var buildDir string
	if params.Test {
		var err error
		buildDir, err = ioutil.TempDir("", fmt.Sprintf("sourcegraph-data.%s.%s-", strings.Replace(string(repoConfig.URI), "/", "-", -1),
			params.Repository.CommitID))
		if err != nil {
			log.Fatal(err)
		}
		if params.TestKeep {
			defer log.Printf("Test build directory: %s", buildDir)
		} else {
			defer os.RemoveAll(buildDir)
		}
	} else {
		buildDir, err = buildstore.BuildDir(repoStore, params.Repository.CommitID)
		if err != nil {
			log.Fatal(err)
		}
	}

	// Create Makefile
	mf, err := build.CreateMakefile(buildDir, repoConfig)
	if err != nil {
		log.Fatalf("error creating Makefile: %s", err)
	}

	if *Verbose || params.ShowOnly {
		// Show Makefile
		data, err := makex.Marshal(mf)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println(string(data))
		if params.ShowOnly {
			return
		}
	}

	// Run Makefile
	err = params.runMakefile(mf)
	if err != nil {
		log.Fatal(err)
	}

	if params.Test {
		// Compare expected with actual
		expectedBuildDir, err := buildstore.BuildDir(repoStore, params.Repository.CommitID)
		if err != nil {
			log.Fatal(err)
		}

		diffOut, err := exec.Command("diff", "-ur", "--exclude=config.json", expectedBuildDir, buildDir).CombinedOutput()
		log.Print("\n\n\n")
		log.Print("###########################")
		log.Print("##      TEST RESULTS     ##")
		log.Print("###########################")
		if len(diffOut) > 0 {
			diffStr := string(diffOut)
			diffStr = strings.Replace(diffStr, buildDir, "<test-build>", -1)
			log.Printf(diffStr)
			log.Printf(brush.Red("** FAIL **").String())
		} else if err != nil {
			log.Fatal(err)
			log.Printf(brush.Red("** ERROR **").String())
		} else if err == nil {
			log.Printf(brush.Green("** PASS **").String())
		}
	}
}

type makeParams struct {
	Repository       *repository
	RepositoryConfig *repositoryConfigurator
	Goals            []string

	ShowOnly bool
	Test     bool
	TestKeep bool
	Makex    *makex.Config
}

func mustParseMakeParams(args []string) *makeParams {
	fs := flag.NewFlagSet("make", flag.ExitOnError)
	r := AddRepositoryFlags(fs)
	rc := AddRepositoryConfigFlags(fs, r)
	showOnly := fs.Bool("mf", false, "print generated Makefile and exit")
	test := fs.Bool("test", false, "test build output against expected output in .sourcegraph-data/")
	testKeep := fs.Bool("test-keep", false, "do NOT delete test build directory after test, used in conjunction with -test")

	conf := &makex.Default
	makex.Flags(fs, conf, "")

	fs.Usage = func() {
		fmt.Fprintln(os.Stderr, `usage: `+Name+` make [options] [target...]

Generates and executes a Makefile that processes a repository, creating graph of
definitions, references, and dependencies in a repository's code at a specific
revision.

Run "`+Name+` makefile" to print the generated Makefile and exit.

This command uses makex to execute the Makefile, but the Makefile is also
compatible with GNU make. You can use the "`+Name+` makefile" command to
generate a Makefile to use with GNU make, if you'd like.

The options are:
	   `)
		fs.PrintDefaults()
		fmt.Fprintln(os.Stderr)
		os.Exit(1)
	}
	fs.Parse(args)

	return &makeParams{
		Repository:       r,
		RepositoryConfig: rc,
		Goals:            fs.Args(),
		ShowOnly:         *showOnly,
		Test:             *test,
		TestKeep:         *testKeep,
		Makex:            conf,
	}
}

func (p *makeParams) verify() error {
	vcsType := vcs.VCSByName[p.Repository.vcsTypeName]
	if vcsType == nil {
		return fmt.Errorf("%s: unknown VCS type %q", Name, p.Repository.vcsTypeName)
	}
	return nil
}

func (p *makeParams) runMakefile(mf *makex.Makefile) error {
	goals := p.Goals
	if len(goals) == 0 {
		if defaultRule := mf.DefaultRule(); defaultRule != nil {
			goals = []string{defaultRule.Target()}
		} else {
			// No rules in Makefile
			return nil
		}
	}

	mk := p.Makex.NewMaker(mf, goals...)

	if p.Makex.DryRun {
		err := mk.DryRun(os.Stdout)
		if err != nil {
			return err
		}
		return nil
	}

	err := os.Chdir(p.Repository.RootDir)
	if err != nil {
		return err
	}

	err = mk.Run()
	if err != nil {
		return err
	}

	return nil
}
<commit_msg>relative buildDataDir<commit_after>package srcgraph

import (
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"path/filepath"

	"strings"

	"github.com/aybabtme/color/brush"
	"github.com/sourcegraph/go-vcs"
	"github.com/sourcegraph/makex"
	"sourcegraph.com/sourcegraph/srcgraph/build"
	"sourcegraph.com/sourcegraph/srcgraph/buildstore"
	"sourcegraph.com/sourcegraph/srcgraph/task2"
)

func makefile(args []string) {
	make_(append(args, "-mf"))
}

func make_(args []string) {
	params := mustParseMakeParams(args)
	if err := params.verify(); err != nil {
		log.Fatal(err)
	}
	repoConfig := params.RepositoryConfig.GetRepositoryConfig(task2.DefaultContext)

	repoStore, err := buildstore.NewRepositoryStore(params.Repository.RootDir)
	if err != nil {
		log.Fatal(err)
	}

	// Get build directory (${REPO}/.sourcegraph-data/...)
	var buildDir string
	if params.Test {
		var err error
		buildDir, err = ioutil.TempDir("", fmt.Sprintf("sourcegraph-data.%s.%s-", strings.Replace(string(repoConfig.URI), "/", "-", -1),
			params.Repository.CommitID))
		if err != nil {
			log.Fatal(err)
		}
		if params.TestKeep {
			defer log.Printf("Test build directory: %s", buildDir)
		} else {
			defer os.RemoveAll(buildDir)
		}
	} else {
		buildDir, err = buildstore.BuildDir(repoStore, params.Repository.CommitID)
		if err != nil {
			log.Fatal(err)
		}

		// Use a relative base path for the Makefile so that we aren't tied to
		// absolute paths. This makes the Makefile more portable between hosts. (And
		// makex uses vfs, which restricts it to accessing only files under a
		// certain path.)
		buildDir, err = filepath.Rel(params.Repository.RootDir, buildDir)
		if err != nil {
			log.Fatal(err)
		}
	}

	// Create Makefile
	mf, err := build.CreateMakefile(buildDir, repoConfig)
	if err != nil {
		log.Fatalf("error creating Makefile: %s", err)
	}

	if *Verbose || params.ShowOnly {
		// Show Makefile
		data, err := makex.Marshal(mf)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println(string(data))
		if params.ShowOnly {
			return
		}
	}

	// Run Makefile
	err = params.runMakefile(mf)
	if err != nil {
		log.Fatal(err)
	}

	if params.Test {
		// Compare expected with actual
		expectedBuildDir, err := buildstore.BuildDir(repoStore, params.Repository.CommitID)
		if err != nil {
			log.Fatal(err)
		}

		diffOut, err := exec.Command("diff", "-ur", "--exclude=config.json", expectedBuildDir, buildDir).CombinedOutput()
		log.Print("\n\n\n")
		log.Print("###########################")
		log.Print("##      TEST RESULTS     ##")
		log.Print("###########################")
		if len(diffOut) > 0 {
			diffStr := string(diffOut)
			diffStr = strings.Replace(diffStr, buildDir, "<test-build>", -1)
			log.Printf(diffStr)
			log.Printf(brush.Red("** FAIL **").String())
		} else if err != nil {
			log.Fatal(err)
			log.Printf(brush.Red("** ERROR **").String())
		} else if err == nil {
			log.Printf(brush.Green("** PASS **").String())
		}
	}
}

type makeParams struct {
	Repository       *repository
	RepositoryConfig *repositoryConfigurator
	Goals            []string

	ShowOnly bool
	Test     bool
	TestKeep bool
	Makex    *makex.Config
}

func mustParseMakeParams(args []string) *makeParams {
	fs := flag.NewFlagSet("make", flag.ExitOnError)
	r := AddRepositoryFlags(fs)
	rc := AddRepositoryConfigFlags(fs, r)
	showOnly := fs.Bool("mf", false, "print generated Makefile and exit")
	test := fs.Bool("test", false, "test build output against expected output in .sourcegraph-data/")
	testKeep := fs.Bool("test-keep", false, "do NOT delete test build directory after test, used in conjunction with -test")

	conf := &makex.Default
	makex.Flags(fs, conf, "")

	fs.Usage = func() {
		fmt.Fprintln(os.Stderr, `usage: `+Name+` make [options] [target...]

Generates and executes a Makefile that processes a repository, creating graph of
definitions, references, and dependencies in a repository's code at a specific
revision.

Run "`+Name+` makefile" to print the generated Makefile and exit.

This command uses makex to execute the Makefile, but the Makefile is also
compatible with GNU make. You can use the "`+Name+` makefile" command to
generate a Makefile to use with GNU make, if you'd like.

The options are:
	   `)
		fs.PrintDefaults()
		fmt.Fprintln(os.Stderr)
		os.Exit(1)
	}
	fs.Parse(args)

	return &makeParams{
		Repository:       r,
		RepositoryConfig: rc,
		Goals:            fs.Args(),
		ShowOnly:         *showOnly,
		Test:             *test,
		TestKeep:         *testKeep,
		Makex:            conf,
	}
}

func (p *makeParams) verify() error {
	vcsType := vcs.VCSByName[p.Repository.vcsTypeName]
	if vcsType == nil {
		return fmt.Errorf("%s: unknown VCS type %q", Name, p.Repository.vcsTypeName)
	}
	return nil
}

func (p *makeParams) runMakefile(mf *makex.Makefile) error {
	goals := p.Goals
	if len(goals) == 0 {
		if defaultRule := mf.DefaultRule(); defaultRule != nil {
			goals = []string{defaultRule.Target()}
		} else {
			// No rules in Makefile
			return nil
		}
	}

	err := os.Chdir(p.Repository.RootDir)
	if err != nil {
		return err
	}

	mk := p.Makex.NewMaker(mf, goals...)

	if p.Makex.DryRun {
		err := mk.DryRun(os.Stdout)
		if err != nil {
			return err
		}
		return nil
	}

	err = mk.Run()
	if err != nil {
		return err
	}

	return nil
}
<|endoftext|>


<commit_before>package toolbox

import (
	"fmt"
	"os"
	"strings"
	"time"
	"github.com/pkg/errors"
	"io/ioutil"
	"bytes"
)

//ValueProvider represents a value provider
type ValueProvider interface {
	//Get returns a value for passed in context and arguments. Context can be used to manage state.
	Get(context Context, arguments ...interface{}) (interface{}, error)
}

//ValueProviderRegistry registry of value providers
type ValueProviderRegistry interface {
	Register(name string, valueProvider ValueProvider)

	Contains(name string) bool

	Names() []string

	Get(name string) ValueProvider
}

type valueProviderRegistryImpl struct {
	registry map[string](ValueProvider)
}

func (r valueProviderRegistryImpl) Register(name string, valueProvider ValueProvider) {
	r.registry[name] = valueProvider
}

func (r valueProviderRegistryImpl) Contains(name string) bool {
	_, ok := r.registry[name]
	return ok
}

func (r valueProviderRegistryImpl) Get(name string) ValueProvider {
	if result, ok := r.registry[name]; ok {
		return result
	}
	panic(fmt.Sprintf("failed to lookup name: %v", name))
}

func (r valueProviderRegistryImpl) Names() []string {
	return MapKeysToStringSlice(&r.registry)
}

//NewValueProviderRegistry create new NewValueProviderRegistry
func NewValueProviderRegistry() ValueProviderRegistry {
	var result ValueProviderRegistry = &valueProviderRegistryImpl{
		registry: make(map[string]ValueProvider),
	}
	return result
}

type envValueProvider struct{}

func (p envValueProvider) Get(context Context, arguments ...interface{}) (interface{}, error) {
	key := arguments[0].(string)
	value, found := os.LookupEnv(key)
	if found {
		return value, nil
	}
	return nil, fmt.Errorf("failed to lookup %v in env", key)
}

//NewEnvValueProvider returns a provider that returns a value of env variables.
func NewEnvValueProvider() ValueProvider {
	var result ValueProvider = &envValueProvider{}
	return result
}

type dateOfBirthProvider struct {}

func (p dateOfBirthProvider) Get(context Context, arguments ...interface{}) (interface{}, error) {
	if len(arguments) < 1 {
		return nil, errors.New("expected <age> | [month], [day], [timeformat]")
	}
	now := time.Now()
	age := AsInt(arguments[0])
	var month int = int(now.Month())
	var day int = now.Day()
	var timeFormat = "yyyy-MM-dd"
	if len(arguments) >= 2 {
		month = AsInt(arguments[1])
	}
	if len(arguments) >= 3 {
		day = AsInt(arguments[2])
	}
	if len(arguments) >= 4 {
		timeFormat = AsString(arguments[3])
	}

	dateOfBirthText := fmt.Sprintf("%04d-%02d-%02d", now.Year() - age, month, day)
	date,err := time.Parse(DateFormatToLayout("yyyy-MM-dd"), dateOfBirthText)
	if (err != nil) {
		return nil, err
	}
	return date.Format(DateFormatToLayout(timeFormat)), nil
}

//NewDateOfBirthValueProvider provider for computing date for supplied expected age, month and day
func NewDateOfBirthrovider() ValueProvider {
	return &dateOfBirthProvider{}
}

type castedValueProvider struct{}

func (p castedValueProvider) Get(context Context, arguments ...interface{}) (interface{}, error) {
	key := arguments[0].(string)
	if len(arguments) < 2 {
		return nil, fmt.Errorf("failed to cast to %v due to invalid number of arguments, Wanted 2 but had:%v", key, len(arguments))
	}
	switch key {
	case "time":
		if len(arguments) != 3 {
			return nil, fmt.Errorf("failed to cast to time due to invalid number of arguments expected 2, but had %v", len(arguments)-1)
		}
		castedTime, err := ParseTime(AsString(arguments[1]), AsString(arguments[2]))
		if err != nil {
			return nil, fmt.Errorf("failed to cast to time %v due to %v", AsString(arguments[1]), err)
		}
		return castedTime, nil
	case "int":
		return AsInt(arguments[1]), nil
	case "float":
		return AsFloat(arguments[1]), nil
	case "bool":
		return AsBoolean(arguments[1]), nil
	case "string":
		return AsString(arguments[1]), nil

	}
	return nil, fmt.Errorf("failed to cast to %v - unsupported type", key)
}

//NewCastedValueProvider return a provider that return casted value type
func NewCastedValueProvider() ValueProvider {
	var result ValueProvider = &castedValueProvider{}
	return result
}

type currentTimeProvider struct{}

func (p currentTimeProvider) Get(context Context, arguments ...interface{}) (interface{}, error) {
	return time.Now(), nil
}

//NewCurrentTimeProvider returns a provder that returns time.Now()
func NewCurrentTimeProvider() ValueProvider {
	var result ValueProvider = &currentTimeProvider{}
	return result
}

type timeDiffProvider struct{}

func (p timeDiffProvider) Get(context Context, arguments ...interface{}) (interface{}, error) {

	var resultTime time.Time
	var durationDelta time.Duration

	if len(arguments) >= 1 {
		if strings.ToLower(AsString(arguments[0])) == "now" {
			resultTime = time.Now()
		} else {
			extractedTime := AsTime(arguments[0], "")
			if extractedTime != nil {
				resultTime = *extractedTime
			}
		}
	}

	if len(arguments) >= 3 {
		var amount = AsInt(arguments[1])
		switch strings.ToLower(AsString(arguments[2])) {
		case "day":
			durationDelta = time.Duration(amount*24) * time.Hour
		case "week":
			durationDelta = time.Duration(amount*24*7) * time.Hour
		case "hour":
			durationDelta = time.Duration(amount) * time.Hour
		case "min":
			durationDelta = time.Duration(amount) * time.Minute
		case "sec":
			durationDelta = time.Duration(amount) * time.Second
		}
	}
	var format = ""
	if len(arguments) == 4 {
		format = AsString(arguments[3])
	}
	resultTime = resultTime.Add(durationDelta)
	switch format {
	case "unix":
		return int(resultTime.Unix()+resultTime.UnixNano()) / 1000000000, nil
	case "timestamp":
		return int(resultTime.Unix()+resultTime.UnixNano()) / 1000000, nil

	default:
		if len(format) > 0 {
			return resultTime.Format(DateFormatToLayout(format)), nil
		}
	}
	return resultTime, nil
}

//NewTimeDiffProvider returns a provider that delta, time unit  and optionally format
//format as java date format or unix or timestamp
func NewTimeDiffProvider() ValueProvider {
	var result ValueProvider = &timeDiffProvider{}
	return result
}

type weekdayProvider struct{}

func (p weekdayProvider) Get(context Context, arguments ...interface{}) (interface{}, error) {
	var now = time.Now()
	return int(now.Weekday()), nil
}

func NewWeekdayProvider() ValueProvider {
	return &weekdayProvider{}
}

type nilValueProvider struct{}

func (p nilValueProvider) Get(context Context, arguments ...interface{}) (interface{}, error) {
	return nil, nil
}

//NewNilValueProvider returns a provider that returns a nil
func NewNilValueProvider() ValueProvider {
	var result ValueProvider = &nilValueProvider{}
	return result
}

type currentDateProvider struct{}

func (p currentDateProvider) Get(context Context, arguments ...interface{}) (interface{}, error) {
	return time.Now().Local().Format("20060102"), nil
}

//NewCurrentDateProvider returns a provider that returns current date in the format yyyymmdd, i.e. 20170205
func NewCurrentDateProvider() ValueProvider {
	var result ValueProvider = &currentDateProvider{}
	return result
}

//Dictionary represents simply lookup interface
type Dictionary interface {
	//Get returns value for passed in key or error
	Get(key string) (interface{}, error)

	//Exists checks if key exists
	Exists(key string) bool
}

//MapDictionary alias to map of string and interface{}
type MapDictionary map[string]interface{}

func (d *MapDictionary) Get(name string) (interface{}, error) {
	if result, found := (*d)[name]; found {
		return result, nil
	}
	return nil, fmt.Errorf("failed to lookup: %v", name)
}

func (d *MapDictionary) Exists(name string) bool {
	_, found := (*d)[name]
	return found
}

type dictionaryProvider struct {
	dictionaryContentKey interface{}
}

func (p dictionaryProvider) Get(context Context, arguments ...interface{}) (interface{}, error) {
	if len(arguments) == 0 {
		return nil, fmt.Errorf("expected at least one argument but had 0")
	}
	var key = AsString(arguments[0])
	var dictionary Dictionary
	context.GetInto(p.dictionaryContentKey, &dictionary)
	if len(arguments) == 1 && !dictionary.Exists(key) {
		return nil, nil
	}
	return dictionary.Get(key)
}

//NewDictionaryProvider creates a new Dictionary provider, it takes a key context that is a MapDictionary pointer
func NewDictionaryProvider(contextKey interface{}) ValueProvider {
	return &dictionaryProvider{contextKey}
}

type betweenPredicateValueProvider struct{}

func (p *betweenPredicateValueProvider) Get(context Context, arguments ...interface{}) (interface{}, error) {
	if len(arguments) != 2 {
		return nil, fmt.Errorf("expected 2 arguments with between predicate but had %v", len(arguments))
	}
	predicate := NewBetweenPredicate(arguments[0], arguments[1])
	return &predicate, nil
}

//NewBetweenPredicateValueProvider returns a new between value provider
func NewBetweenPredicateValueProvider() ValueProvider {
	return &betweenPredicateValueProvider{}
}

type withinSecPredicateValueProvider struct{}

func (p *withinSecPredicateValueProvider) Get(context Context, arguments ...interface{}) (interface{}, error) {
	if len(arguments) != 3 {
		return nil, fmt.Errorf("expected 3 arguments <ds:within_sec [timestamp, delta, dateFormat]>  predicate, but had %v", len(arguments))
	}

	if arguments[0] == "now" {
		arguments[0] = time.Now()
	}
	dateFormat := AsString(arguments[2])
	dateLayout := DateFormatToLayout(dateFormat)
	targetTime := AsTime(arguments[0], dateLayout)
	if targetTime == nil {
		return nil, fmt.Errorf("Unable convert %v to time.Time", arguments[0])
	}
	delta := AsInt(arguments[1])
	predicate := NewWithinPredicate(*targetTime, delta, dateLayout)
	return &predicate, nil
}

//NewWithinSecPredicateValueProvider returns a new within second value provider
func NewWithinSecPredicateValueProvider() ValueProvider {
	return &withinSecPredicateValueProvider{}
}

type fileValueProvider struct{}

func (p *fileValueProvider) Get(context Context, arguments ...interface{}) (interface{}, error) {
	filePath := AsString(arguments[0])

	fileContent, err := ioutil.ReadFile(filePath)
	if err != nil {
		panic(err)
	}
	content := bytes.TrimSpace(fileContent)
	result := string(content)
	return result, nil
}

//NewFileValueProvider create  new file value provider
func NewFileValueProvider() ValueProvider {
	return  &fileValueProvider{}
}
<commit_msg>patched cycle<commit_after>package toolbox

import (
	"fmt"
	"os"
	"strings"
	"time"
	"github.com/pkg/errors"
	"io/ioutil"
	"bytes"
)

//ValueProvider represents a value provider
type ValueProvider interface {
	//Get returns a value for passed in context and arguments. Context can be used to manage state.
	Get(context Context, arguments ...interface{}) (interface{}, error)
}

//ValueProviderRegistry registry of value providers
type ValueProviderRegistry interface {
	Register(name string, valueProvider ValueProvider)

	Contains(name string) bool

	Names() []string

	Get(name string) ValueProvider
}

type valueProviderRegistryImpl struct {
	registry map[string](ValueProvider)
}

func (r valueProviderRegistryImpl) Register(name string, valueProvider ValueProvider) {
	r.registry[name] = valueProvider
}

func (r valueProviderRegistryImpl) Contains(name string) bool {
	_, ok := r.registry[name]
	return ok
}

func (r valueProviderRegistryImpl) Get(name string) ValueProvider {
	if result, ok := r.registry[name]; ok {
		return result
	}
	panic(fmt.Sprintf("failed to lookup name: %v", name))
}

func (r valueProviderRegistryImpl) Names() []string {
	return MapKeysToStringSlice(&r.registry)
}

//NewValueProviderRegistry create new NewValueProviderRegistry
func NewValueProviderRegistry() ValueProviderRegistry {
	var result ValueProviderRegistry = &valueProviderRegistryImpl{
		registry: make(map[string]ValueProvider),
	}
	return result
}

type envValueProvider struct{}

func (p envValueProvider) Get(context Context, arguments ...interface{}) (interface{}, error) {
	key := arguments[0].(string)
	value, found := os.LookupEnv(key)
	if found {
		return value, nil
	}
	return nil, fmt.Errorf("failed to lookup %v in env", key)
}

//NewEnvValueProvider returns a provider that returns a value of env variables.
func NewEnvValueProvider() ValueProvider {
	var result ValueProvider = &envValueProvider{}
	return result
}

type dateOfBirthProvider struct {}

func (p dateOfBirthProvider) Get(context Context, arguments ...interface{}) (interface{}, error) {
	if len(arguments) < 1 {
		return nil, errors.New("expected <age> | [month], [day], [timeformat]")
	}
	now := time.Now()
	age := AsInt(arguments[0])
	var month int = int(now.Month())
	var day int = now.Day()
	var timeFormat = "yyyy-MM-dd"
	if len(arguments) >= 2 {
		month = AsInt(arguments[1])
	}
	if len(arguments) >= 3 {
		day = AsInt(arguments[2])
	}
	if len(arguments) >= 4 {
		timeFormat = AsString(arguments[3])
	}

	dateOfBirthText := fmt.Sprintf("%04d-%02d-%02d", now.Year() - age, month, day)
	date,err := time.Parse(DateFormatToLayout("yyyy-MM-dd"), dateOfBirthText)
	if (err != nil) {
		return nil, err
	}
	return date.Format(DateFormatToLayout(timeFormat)), nil
}

//NewDateOfBirthValueProvider provider for computing date for supplied expected age, month and day
func NewDateOfBirthrovider() ValueProvider {
	return &dateOfBirthProvider{}
}

type castedValueProvider struct{}

func (p castedValueProvider) Get(context Context, arguments ...interface{}) (interface{}, error) {
	key := arguments[0].(string)
	if len(arguments) < 2 {
		return nil, fmt.Errorf("failed to cast to %v due to invalid number of arguments, Wanted 2 but had:%v", key, len(arguments))
	}
	switch key {
	case "time":
		if len(arguments) != 3 {
			return nil, fmt.Errorf("failed to cast to time due to invalid number of arguments expected 2, but had %v", len(arguments)-1)
		}
		castedTime, err := ParseTime(AsString(arguments[1]), AsString(arguments[2]))
		if err != nil {
			return nil, fmt.Errorf("failed to cast to time %v due to %v", AsString(arguments[1]), err)
		}
		return castedTime, nil
	case "int":
		return AsInt(arguments[1]), nil
	case "float":
		return AsFloat(arguments[1]), nil
	case "bool":
		return AsBoolean(arguments[1]), nil
	case "string":
		return AsString(arguments[1]), nil

	}
	return nil, fmt.Errorf("failed to cast to %v - unsupported type", key)
}

//NewCastedValueProvider return a provider that return casted value type
func NewCastedValueProvider() ValueProvider {
	var result ValueProvider = &castedValueProvider{}
	return result
}

type currentTimeProvider struct{}

func (p currentTimeProvider) Get(context Context, arguments ...interface{}) (interface{}, error) {
	return time.Now(), nil
}

//NewCurrentTimeProvider returns a provder that returns time.Now()
func NewCurrentTimeProvider() ValueProvider {
	var result ValueProvider = &currentTimeProvider{}
	return result
}

type timeDiffProvider struct{}

func (p timeDiffProvider) Get(context Context, arguments ...interface{}) (interface{}, error) {

	var resultTime time.Time
	var durationDelta time.Duration

	if len(arguments) >= 1 {
		if strings.ToLower(AsString(arguments[0])) == "now" {
			resultTime = time.Now()
		} else {
			extractedTime := AsTime(arguments[0], "")
			if extractedTime != nil {
				resultTime = *extractedTime
			}
		}
	}

	if len(arguments) >= 3 {
		var amount = AsInt(arguments[1])
		switch strings.ToLower(AsString(arguments[2])) {
		case "day":
			durationDelta = time.Duration(amount*24) * time.Hour
		case "week":
			durationDelta = time.Duration(amount*24*7) * time.Hour
		case "hour":
			durationDelta = time.Duration(amount) * time.Hour
		case "min":
			durationDelta = time.Duration(amount) * time.Minute
		case "sec":
			durationDelta = time.Duration(amount) * time.Second
		}
	}
	var format = ""
	if len(arguments) == 4 {
		format = AsString(arguments[3])
	}
	resultTime = resultTime.Add(durationDelta)
	switch format {
	case "unix":
		return int(resultTime.Unix()+resultTime.UnixNano()) / 1000000000, nil
	case "timestamp":
		return int(resultTime.Unix()+resultTime.UnixNano()) / 1000000, nil

	default:
		if len(format) > 0 {
			return resultTime.Format(DateFormatToLayout(format)), nil
		}
	}
	return resultTime, nil
}

//NewTimeDiffProvider returns a provider that delta, time unit  and optionally format
//format as java date format or unix or timestamp
func NewTimeDiffProvider() ValueProvider {
	var result ValueProvider = &timeDiffProvider{}
	return result
}

type weekdayProvider struct{}

func (p weekdayProvider) Get(context Context, arguments ...interface{}) (interface{}, error) {
	var now = time.Now()
	return int(now.Weekday()), nil
}

func NewWeekdayProvider() ValueProvider {
	return &weekdayProvider{}
}

type nilValueProvider struct{}

func (p nilValueProvider) Get(context Context, arguments ...interface{}) (interface{}, error) {
	return nil, nil
}

//NewNilValueProvider returns a provider that returns a nil
func NewNilValueProvider() ValueProvider {
	var result ValueProvider = &nilValueProvider{}
	return result
}

type currentDateProvider struct{}

func (p currentDateProvider) Get(context Context, arguments ...interface{}) (interface{}, error) {
	return time.Now().Local().Format("20060102"), nil
}

//NewCurrentDateProvider returns a provider that returns current date in the format yyyymmdd, i.e. 20170205
func NewCurrentDateProvider() ValueProvider {
	var result ValueProvider = &currentDateProvider{}
	return result
}

//Dictionary represents simply lookup interface
type Dictionary interface {
	//Get returns value for passed in key or error
	Get(key string) (interface{}, error)

	//Exists checks if key exists
	Exists(key string) bool
}

//MapDictionary alias to map of string and interface{}
type MapDictionary map[string]interface{}

func (d *MapDictionary) Get(name string) (interface{}, error) {
	if result, found := (*d)[name]; found {
		return result, nil
	}
	return nil, fmt.Errorf("failed to lookup: %v", name)
}

func (d *MapDictionary) Exists(name string) bool {
	_, found := (*d)[name]
	return found
}

type dictionaryProvider struct {
	dictionaryContentKey interface{}
}

func (p dictionaryProvider) Get(context Context, arguments ...interface{}) (interface{}, error) {
	if len(arguments) == 0 {
		return nil, fmt.Errorf("expected at least one argument but had 0")
	}
	var key = AsString(arguments[0])
	var dictionary Dictionary
	context.GetInto(p.dictionaryContentKey, &dictionary)
	if len(arguments) == 1 && !dictionary.Exists(key) {
		return nil, nil
	}
	return dictionary.Get(key)
}

//NewDictionaryProvider creates a new Dictionary provider, it takes a key context that is a MapDictionary pointer
func NewDictionaryProvider(contextKey interface{}) ValueProvider {
	return &dictionaryProvider{contextKey}
}

type betweenPredicateValueProvider struct{}

func (p *betweenPredicateValueProvider) Get(context Context, arguments ...interface{}) (interface{}, error) {
	if len(arguments) != 2 {
		return nil, fmt.Errorf("expected 2 arguments with between predicate but had %v", len(arguments))
	}
	predicate := NewBetweenPredicate(arguments[0], arguments[1])
	return &predicate, nil
}

//NewBetweenPredicateValueProvider returns a new between value provider
func NewBetweenPredicateValueProvider() ValueProvider {
	return &betweenPredicateValueProvider{}
}

type withinSecPredicateValueProvider struct{}

func (p *withinSecPredicateValueProvider) Get(context Context, arguments ...interface{}) (interface{}, error) {
	if len(arguments) != 3 {
		return nil, fmt.Errorf("expected 3 arguments <ds:within_sec [timestamp, delta, dateFormat]>  predicate, but had %v", len(arguments))
	}

	if arguments[0] == "now" {
		arguments[0] = time.Now()
	}
	dateFormat := AsString(arguments[2])
	dateLayout := DateFormatToLayout(dateFormat)
	targetTime := AsTime(arguments[0], dateLayout)
	if targetTime == nil {
		return nil, fmt.Errorf("Unable convert %v to time.Time", arguments[0])
	}
	delta := AsInt(arguments[1])
	predicate := NewWithinPredicate(*targetTime, delta, dateLayout)
	return &predicate, nil
}

//NewWithinSecPredicateValueProvider returns a new within second value provider
func NewWithinSecPredicateValueProvider() ValueProvider {
	return &withinSecPredicateValueProvider{}
}

type fileValueProvider struct{}

func (p *fileValueProvider) Get(context Context, arguments ...interface{}) (interface{}, error) {
	filePath := AsString(arguments[0])

	fileContent, err := ioutil.ReadFile(filePath)
	if err != nil {
		panic(err)
	}
	content := bytes.TrimSpace(fileContent)
	result := string(content)
	return result, nil
}

//NewFileValueProvider create  new file value provider
func NewFileValueProvider(trim bool) ValueProvider {
	return  &fileValueProvider{}
}
<|endoftext|>


<commit_before>/*
   Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

   This file is licensed under the Apache License, Version 2.0 (the "License").
   You may not use this file except in compliance with the License. A copy of
   the License is located at

    http://aws.amazon.com/apache2.0/

   This file 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.
*/
// snippet-start:[sqs.go.receive_lp_message]
package main

// snippet-start:[sqs.go.receive_lp_message.imports]
import (
    "flag"
    "fmt"

    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/sqs"
)
// snippet-end:[sqs.go.receive_lp_message.imports]

// GetQueueURL gets the URL of an Amazon SQS queue
// Inputs:
//     sess is the current session, which provides configuration for the SDK's service clients
//     queueName is the name of the queue
// Output:
//     If success, the URL of the queue and nil
//     Otherwise, an empty string and an error from the call to
func GetQueueURL(sess *session.Session, queue *string) (*sqs.GetQueueUrlOutput, error) {
    // snippet-start:[sqs.go.get_queue_url.call]
    svc := sqs.New(sess)

    result, err := svc.GetQueueUrl(&sqs.GetQueueUrlInput{
        QueueName: queue,
    })
    // snippet-end:[sqs.go.get_queue_url.call]
    if err != nil {
        return nil, err
    }

    return result, nil
}

// GetLPMessages gets the messages from an Amazon SQS long polling queue
// Inputs:
//     sess is the current session, which provides configuration for the SDK's service clients
//     queueURL is the URL of the queue
// Output:
//     If success, nil
//     Otherwise, an error from the call to ReceiveMessage
func GetLPMessages(sess *session.Session, queueURL *string, waitTime *int64) ([]*sqs.Message, error) {
    var msgs []*sqs.Message
    svc := sqs.New(sess)

    // snippet-start:[sqs.go.receive_lp_message.call]
    result, err := svc.ReceiveMessage(&sqs.ReceiveMessageInput{
        QueueUrl: queueURL,
        AttributeNames: aws.StringSlice([]string{
            "SentTimestamp",
        }),
        MaxNumberOfMessages: aws.Int64(1),
        MessageAttributeNames: aws.StringSlice([]string{
            "All",
        }),
        WaitTimeSeconds: waitTime,
    })
    // snippet-end:[sqs.go.receive_lp_message.call]
    if err != nil {
        return msgs, err
    }

    return result.Messages, nil
}

func main() {
    // snippet-start:[sqs.go.receive_lp_message.args]
    queue := flag.String("q", "", "The name of the queue")
    visibility := flag.Int64("v", 5, "How long, in seconds, that messages are hidden from other consumers")
    waitTime := flag.Int64("w", 10, "How long the queue waits for messages")
    flag.Parse()

    if *queue == "" {
        fmt.Println("You must supply a queue name (-q QUEUE")
        return
    }

    if *visibility < 0 {
        *visibility = 0
    }

    if *visibility > 12*60*60 { // 12 hours
        *visibility = 12 * 60 * 60
    }

    if *waitTime < 0 {
        *waitTime = 0
    }

    if *waitTime > 20 {
        *waitTime = 20
    }
    // snippet-end:[sqs.go.receive_lp_message.args]

    // Create a session that gets credential values from ~/.aws/credentials
    // and the default region from ~/.aws/config
    // snippet-start:[sqs.go.receive_lp_message.sess]
    sess := session.Must(session.NewSessionWithOptions(session.Options{
        SharedConfigState: session.SharedConfigEnable,
    }))
    // snippet-end:[sqs.go.receive_lp_message.sess]

    result, err := GetQueueURL(sess, queue)
    if err != nil {
        fmt.Println("Got an error getting the queue URL:")
        fmt.Println(err)
        return
    }

    queueURL := result.QueueUrl

    msgs, err := GetLPMessages(sess, queueURL, waitTime)
    if err != nil {
        fmt.Println("Got an error receiving messages:")
        fmt.Println(err)
        return
    }

    // snippet-start:[sqs.go.receive_lp_message.display]
    fmt.Println("Message IDs:")

    for _, msg := range msgs {
        fmt.Println("    " + *msg.MessageId)
    }
    // snippet-end:[sqs.go.receive_lp_message.display]
}
// snippet-end:[sqs.go.receive_lp_message]
<commit_msg>Removed unused Visibility variable<commit_after>/*
   Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

   This file is licensed under the Apache License, Version 2.0 (the "License").
   You may not use this file except in compliance with the License. A copy of
   the License is located at

    http://aws.amazon.com/apache2.0/

   This file 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.
*/
// snippet-start:[sqs.go.receive_lp_message]
package main

// snippet-start:[sqs.go.receive_lp_message.imports]
import (
    "flag"
    "fmt"

    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/sqs"
)

// snippet-end:[sqs.go.receive_lp_message.imports]

// GetQueueURL gets the URL of an Amazon SQS queue
// Inputs:
//     sess is the current session, which provides configuration for the SDK's service clients
//     queueName is the name of the queue
// Output:
//     If success, the URL of the queue and nil
//     Otherwise, an empty string and an error from the call to
func GetQueueURL(sess *session.Session, queue *string) (*sqs.GetQueueUrlOutput, error) {
    // snippet-start:[sqs.go.get_queue_url.call]
    svc := sqs.New(sess)

    result, err := svc.GetQueueUrl(&sqs.GetQueueUrlInput{
        QueueName: queue,
    })
    // snippet-end:[sqs.go.get_queue_url.call]
    if err != nil {
        return nil, err
    }

    return result, nil
}

// GetLPMessages gets the messages from an Amazon SQS long polling queue
// Inputs:
//     sess is the current session, which provides configuration for the SDK's service clients
//     queueURL is the URL of the queue
// Output:
//     If success, nil
//     Otherwise, an error from the call to ReceiveMessage
func GetLPMessages(sess *session.Session, queueURL *string, waitTime *int64) ([]*sqs.Message, error) {
    var msgs []*sqs.Message
    svc := sqs.New(sess)

    // snippet-start:[sqs.go.receive_lp_message.call]
    result, err := svc.ReceiveMessage(&sqs.ReceiveMessageInput{
        QueueUrl: queueURL,
        AttributeNames: aws.StringSlice([]string{
            "SentTimestamp",
        }),
        MaxNumberOfMessages: aws.Int64(1),
        MessageAttributeNames: aws.StringSlice([]string{
            "All",
        }),
        WaitTimeSeconds: waitTime,
    })
    // snippet-end:[sqs.go.receive_lp_message.call]
    if err != nil {
        return msgs, err
    }

    return result.Messages, nil
}

func main() {
    // snippet-start:[sqs.go.receive_lp_message.args]
    queue := flag.String("q", "", "The name of the queue")
    waitTime := flag.Int64("w", 10, "How long the queue waits for messages")
    flag.Parse()

    if *queue == "" {
        fmt.Println("You must supply a queue name (-q QUEUE")
        return
    }

    if *waitTime < 0 {
        *waitTime = 0
    }

    if *waitTime > 20 {
        *waitTime = 20
    }
    // snippet-end:[sqs.go.receive_lp_message.args]

    // Create a session that gets credential values from ~/.aws/credentials
    // and the default region from ~/.aws/config
    // snippet-start:[sqs.go.receive_lp_message.sess]
    sess := session.Must(session.NewSessionWithOptions(session.Options{
        SharedConfigState: session.SharedConfigEnable,
    }))
    // snippet-end:[sqs.go.receive_lp_message.sess]

    result, err := GetQueueURL(sess, queue)
    if err != nil {
        fmt.Println("Got an error getting the queue URL:")
        fmt.Println(err)
        return
    }

    queueURL := result.QueueUrl

    msgs, err := GetLPMessages(sess, queueURL, waitTime)
    if err != nil {
        fmt.Println("Got an error receiving messages:")
        fmt.Println(err)
        return
    }

    // snippet-start:[sqs.go.receive_lp_message.display]
    fmt.Println("Message IDs:")

    for _, msg := range msgs {
        fmt.Println("    " + *msg.MessageId)
    }
    // snippet-end:[sqs.go.receive_lp_message.display]
}

// snippet-end:[sqs.go.receive_lp_message]
<|endoftext|>


<commit_before>/*
 * Copyright 2020 The FedLearner Authors. All Rights Reserved.
 *
 * 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 operator

import (
	"encoding/json"
	"fmt"
	"strconv"
	"strings"

	"github.com/bytedance/fedlearner/deploy/kubernetes_operator/pkg/apis/fedlearner.k8s.io/v1alpha1"
)

const (
	ServiceFormat = "%s.%s.svc.cluster.local"
)

type ClusterSpec struct {
	Services map[v1alpha1.FLReplicaType][]string `json:"clusterSpec"`
}

func NewClusterSpec(namespace string, app *v1alpha1.FLApp) ClusterSpec {
	clusterSpec := ClusterSpec{
		Services: make(map[v1alpha1.FLReplicaType][]string),
	}
	for rtype := range app.Spec.FLReplicaSpecs {
		rt := strings.ToLower(string(rtype))
		replicas := getReplicas(app, rtype)
		port, err := GetPortFromApp(app, rtype)
		if err != nil {
			port = v1alpha1.DefaultPort
		}
		for index := 0; index < replicas; index++ {
			serviceName := fmt.Sprintf(ServiceFormat, GenIndexName(app.Name, strings.ToLower(app.Spec.Role), rt, strconv.Itoa(index)), namespace)
			clusterSpec.Services[rtype] = append(clusterSpec.Services[rtype], fmt.Sprintf("%s:%d", serviceName, port))
		}
	}
	return clusterSpec
}

func (cs ClusterSpec) Marshal() ([]byte, error) {
	return json.Marshal(cs)
}
<commit_msg>fix: fix service format (#773)<commit_after>/*
 * Copyright 2020 The FedLearner Authors. All Rights Reserved.
 *
 * 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 operator

import (
	"encoding/json"
	"fmt"
	"strconv"
	"strings"

	"github.com/bytedance/fedlearner/deploy/kubernetes_operator/pkg/apis/fedlearner.k8s.io/v1alpha1"
)

const (
	ServiceFormat = "%s.%s.svc"
)

type ClusterSpec struct {
	Services map[v1alpha1.FLReplicaType][]string `json:"clusterSpec"`
}

func NewClusterSpec(namespace string, app *v1alpha1.FLApp) ClusterSpec {
	clusterSpec := ClusterSpec{
		Services: make(map[v1alpha1.FLReplicaType][]string),
	}
	for rtype := range app.Spec.FLReplicaSpecs {
		rt := strings.ToLower(string(rtype))
		replicas := getReplicas(app, rtype)
		port, err := GetPortFromApp(app, rtype)
		if err != nil {
			port = v1alpha1.DefaultPort
		}
		for index := 0; index < replicas; index++ {
			serviceName := fmt.Sprintf(ServiceFormat, GenIndexName(app.Name, strings.ToLower(app.Spec.Role), rt, strconv.Itoa(index)), namespace)
			clusterSpec.Services[rtype] = append(clusterSpec.Services[rtype], fmt.Sprintf("%s:%d", serviceName, port))
		}
	}
	return clusterSpec
}

func (cs ClusterSpec) Marshal() ([]byte, error) {
	return json.Marshal(cs)
}
<|endoftext|>