<commit_before>package sparta

import (
	"archive/zip"

	"github.com/aws/aws-sdk-go/aws/session"
	gocf "github.com/mweagle/go-cloudformation"
	"github.com/sirupsen/logrus"
)

////////////////////////////////////////////////////////////////////////////////
// TYPES
////////////////////////////////////////////////////////////////////////////////

// TemplateDecorator allows Lambda functions to annotate the CloudFormation
// template definition.  Both the resources and the outputs params
// are initialized to an empty ArbitraryJSONObject and should
// be populated with valid CloudFormation ArbitraryJSONObject values.  The
// CloudFormationResourceName() function can be used to generate
// logical CloudFormation-compatible resource names.
// See http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html and
// http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/outputs-section-structure.html for
// more information.
type TemplateDecorator func(serviceName string,
	lambdaResourceName string,
	lambdaResource gocf.LambdaFunction,
	resourceMetadata map[string]interface{},
	S3Bucket string,
	S3Key string,
	buildID string,
	template *gocf.Template,
	context map[string]interface{},
	logger *logrus.Logger) error

// TemplateDecoratorHookFunc is the adapter to transform an existing
// ArchiveHook into a WorkflowHookHandler satisfier
type TemplateDecoratorHookFunc func(serviceName string,
	lambdaResourceName string,
	lambdaResource gocf.LambdaFunction,
	resourceMetadata map[string]interface{},
	S3Bucket string,
	S3Key string,
	buildID string,
	template *gocf.Template,
	context map[string]interface{},
	logger *logrus.Logger) error

// DecorateTemplate calls tdhf(...) to satisfy TemplateDecoratorHandler
func (tdhf TemplateDecoratorHookFunc) DecorateTemplate(serviceName string,
	lambdaResourceName string,
	lambdaResource gocf.LambdaFunction,
	resourceMetadata map[string]interface{},
	S3Bucket string,
	S3Key string,
	buildID string,
	template *gocf.Template,
	context map[string]interface{},
	logger *logrus.Logger) error {
	return tdhf(serviceName,
		lambdaResourceName,
		lambdaResource,
		resourceMetadata,
		S3Bucket,
		S3Key,
		buildID,
		template,
		context,
		logger)
}

// TemplateDecoratorHandler is the interface type to indicate a template
// decoratorHook
type TemplateDecoratorHandler interface {
	DecorateTemplate(serviceName string,
		lambdaResourceName string,
		lambdaResource gocf.LambdaFunction,
		resourceMetadata map[string]interface{},
		S3Bucket string,
		S3Key string,
		buildID string,
		template *gocf.Template,
		context map[string]interface{},
		logger *logrus.Logger) error
}

////////////////////////////////////////////////////////////////////////////////
// WorkflowHandler

// WorkflowHook defines a user function that should be called at a specific
// point in the larger Sparta workflow. The first argument is a map that
// is shared across all LifecycleHooks and which Sparta treats as an opaque
// value.
type WorkflowHook func(context map[string]interface{},
	serviceName string,
	S3Bucket string,
	buildID string,
	awsSession *session.Session,
	noop bool,
	logger *logrus.Logger) error

// WorkflowHookFunc is the adapter to transform an existing
// WorkflowHook into a WorkflowHookHandler satisfier
type WorkflowHookFunc func(context map[string]interface{},
	serviceName string,
	S3Bucket string,
	buildID string,
	awsSession *session.Session,
	noop bool,
	logger *logrus.Logger) error

// DecorateWorkflow calls whf(...) to satisfy WorkflowHookHandler
func (whf WorkflowHookFunc) DecorateWorkflow(context map[string]interface{},
	serviceName string,
	S3Bucket string,
	buildID string,
	awsSession *session.Session,
	noop bool,
	logger *logrus.Logger) error {
	return whf(context,
		serviceName,
		S3Bucket,
		buildID,
		awsSession,
		noop,
		logger)
}

// WorkflowHookHandler is the interface type to indicate a workflow
// hook
type WorkflowHookHandler interface {
	DecorateWorkflow(context map[string]interface{},
		serviceName string,
		S3Bucket string,
		buildID string,
		awsSession *session.Session,
		noop bool,
		logger *logrus.Logger) error
}

////////////////////////////////////////////////////////////////////////////////

// RuntimeLoggerHook is responsible for adding custom hooks to the Logrus
// logger for things like publishing to StackDriver
type RuntimeLoggerHook func(context map[string]interface{},
	serviceName string,
	logger *logrus.Logger) error

////////////////////////////////////////////////////////////////////////////////
// ArchiveHandler

// ArchiveHook provides callers an opportunity to insert additional
// files into the ZIP archive deployed to S3<commit_msg>Remove RuntimeLoggerHook typedef<commit_after>package sparta

import (
	"archive/zip"

	"github.com/aws/aws-sdk-go/aws/session"
	gocf "github.com/mweagle/go-cloudformation"
	"github.com/sirupsen/logrus"
)

////////////////////////////////////////////////////////////////////////////////
// TYPES
////////////////////////////////////////////////////////////////////////////////

// TemplateDecorator allows Lambda functions to annotate the CloudFormation
// template definition.  Both the resources and the outputs params
// are initialized to an empty ArbitraryJSONObject and should
// be populated with valid CloudFormation ArbitraryJSONObject values.  The
// CloudFormationResourceName() function can be used to generate
// logical CloudFormation-compatible resource names.
// See http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html and
// http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/outputs-section-structure.html for
// more information.
type TemplateDecorator func(serviceName string,
	lambdaResourceName string,
	lambdaResource gocf.LambdaFunction,
	resourceMetadata map[string]interface{},
	S3Bucket string,
	S3Key string,
	buildID string,
	template *gocf.Template,
	context map[string]interface{},
	logger *logrus.Logger) error

// TemplateDecoratorHookFunc is the adapter to transform an existing
// ArchiveHook into a WorkflowHookHandler satisfier
type TemplateDecoratorHookFunc func(serviceName string,
	lambdaResourceName string,
	lambdaResource gocf.LambdaFunction,
	resourceMetadata map[string]interface{},
	S3Bucket string,
	S3Key string,
	buildID string,
	template *gocf.Template,
	context map[string]interface{},
	logger *logrus.Logger) error

// DecorateTemplate calls tdhf(...) to satisfy TemplateDecoratorHandler
func (tdhf TemplateDecoratorHookFunc) DecorateTemplate(serviceName string,
	lambdaResourceName string,
	lambdaResource gocf.LambdaFunction,
	resourceMetadata map[string]interface{},
	S3Bucket string,
	S3Key string,
	buildID string,
	template *gocf.Template,
	context map[string]interface{},
	logger *logrus.Logger) error {
	return tdhf(serviceName,
		lambdaResourceName,
		lambdaResource,
		resourceMetadata,
		S3Bucket,
		S3Key,
		buildID,
		template,
		context,
		logger)
}

// TemplateDecoratorHandler is the interface type to indicate a template
// decoratorHook
type TemplateDecoratorHandler interface {
	DecorateTemplate(serviceName string,
		lambdaResourceName string,
		lambdaResource gocf.LambdaFunction,
		resourceMetadata map[string]interface{},
		S3Bucket string,
		S3Key string,
		buildID string,
		template *gocf.Template,
		context map[string]interface{},
		logger *logrus.Logger) error
}

////////////////////////////////////////////////////////////////////////////////
// WorkflowHandler

// WorkflowHook defines a user function that should be called at a specific
// point in the larger Sparta workflow. The first argument is a map that
// is shared across all LifecycleHooks and which Sparta treats as an opaque
// value.
type WorkflowHook func(context map[string]interface{},
	serviceName string,
	S3Bucket string,
	buildID string,
	awsSession *session.Session,
	noop bool,
	logger *logrus.Logger) error

// WorkflowHookFunc is the adapter to transform an existing
// WorkflowHook into a WorkflowHookHandler satisfier
type WorkflowHookFunc func(context map[string]interface{},
	serviceName string,
	S3Bucket string,
	buildID string,
	awsSession *session.Session,
	noop bool,
	logger *logrus.Logger) error

// DecorateWorkflow calls whf(...) to satisfy WorkflowHookHandler
func (whf WorkflowHookFunc) DecorateWorkflow(context map[string]interface{},
	serviceName string,
	S3Bucket string,
	buildID string,
	awsSession *session.Session,
	noop bool,
	logger *logrus.Logger) error {
	return whf(context,
		serviceName,
		S3Bucket,
		buildID,
		awsSession,
		noop,
		logger)
}

// WorkflowHookHandler is the interface type to indicate a workflow
// hook
type WorkflowHookHandler interface {
	DecorateWorkflow(context map[string]interface{},
		serviceName string,
		S3Bucket string,
		buildID string,
		awsSession *session.Session,
		noop bool,
		logger *logrus.Logger) error
}

////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// ArchiveHandler

// ArchiveHook provides callers an opportunity to insert additional
// files into the ZIP archive deployed to S3
{'fraction_non_alphanumeric': 0.15870251442255362, 'fraction_numerical': 0.0034831827582453468, 'mean_word_length': 3.259619842373667, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3324759', 'n_tokens_mistral': 2559, 'n_tokens_neox': 2450, 'n_words': 741}