Go API Documentation

github.com/TykTechnologies/tyk/cli/bundler

No package summary is available.

Package

Files: 1. Third party imports: 1. Imports from organisation: 1. Tests: 0. Benchmarks: 0.

Constants

const (
	cmdName	= "bundle"
	cmdDesc	= "Manage plugin bundles"

	defaultManifestPath	= "manifest.json"
	defaultBundlePath	= "bundle.zip"
	defaultBundlePerm	= 0755
)

Vars

var (
	bundler	*Bundler	= &Bundler{}

	errNoHooks	= errors.New("No hooks defined")
	errNoDriver	= errors.New("No driver specified")
	errManifestLoad	= errors.New("Couldn't load manifest file")
	errBundleData	= errors.New("Couldn't read/write bundle data")
	errBundleSign	= errors.New("Couldn't sign bundle")

	log	= logger.Get().WithField("prefix", "tyk")
)

Types

Bundler

Bundler wraps the bundler data structure.

Field name Field type Comment
keyPath

*string

No comment on field.
bundlePath

*string

No comment on field.
skipSigning

*bool

No comment on field.
manifestPath

*string

No comment on field.
type Bundler struct {
	keyPath		*string
	bundlePath	*string
	skipSigning	*bool
	manifestPath	*string
}

Functions

func AddTo

AddTo initializes an importer object.

func AddTo(app *kingpin.Application) {
	cmd := app.Command(cmdName, cmdDesc)

	buildCmd := cmd.Command("build", "Build a new plugin bundle using a manifest file and its specified files")
	bundler.keyPath = buildCmd.Flag("key", "Key for bundle signature").Short('k').String()
	bundler.bundlePath = buildCmd.Flag("output", "Output file").Short('o').Default(defaultBundlePath).String()
	bundler.skipSigning = buildCmd.Flag("skip-signing", "Skip bundle signing").Short('y').Bool()
	bundler.manifestPath = buildCmd.Flag("manifest", "Path to manifest file").Default(defaultManifestPath).Short('m').String()
	buildCmd.Action(bundler.Build)
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func (*Bundler) Build

Build builds a bundle.

func (b *Bundler) Build(ctx *kingpin.ParseContext) error {
	manifestPath := *b.manifestPath
	bundlePath := *b.bundlePath
	skipSigning := *b.skipSigning
	key := *b.keyPath

	log.Infof("Building bundle using '%s'", manifestPath)
	manifest, err := b.loadManifest(manifestPath)
	if err != nil {
		return err
	}
	if bundlePath == defaultBundlePath {
		log.Warningf("Using default bundle path '%s'", defaultBundlePath)
	}

	// Write the file:
	bundleBuf := new(bytes.Buffer)
	for _, file := range manifest.FileList {
		var data []byte
		data, err = ioutil.ReadFile(file)
		if err != nil {
			break
		}
		bundleBuf.Write(data)
	}
	if err != nil {
		return err
	}

	// Compute the checksum and append it to the manifest data structure:
	manifest.Checksum = fmt.Sprintf("%x", md5.Sum(bundleBuf.Bytes()))

	if key == "" {
		if skipSigning {
			log.Warning("The bundle will be unsigned")
		} else {
			log.Warning("The bundle will be unsigned, type \"y\" or \"yes\" to confirm:")
			reader := bufio.NewReader(os.Stdin)
			text, _ := reader.ReadString('\n')
			ch := text[0:1]
			if ch != "y" {
				log.Fatal("Aborting")
			}
		}
	} else {
		err = b.sign(key, manifest, bundleBuf)
		if err != nil {
			return err
		}
	}

	manifestData, err := json.Marshal(&manifest)
	if err != nil {
		return err
	}

	// Write the ZIP contents into a buffer:
	buf := new(bytes.Buffer)
	zipWriter := zip.NewWriter(buf)
	for _, file := range manifest.FileList {
		var outputFile io.Writer
		outputFile, err = zipWriter.Create(file)
		if err != nil {
			break
		}
		var data []byte
		data, err = ioutil.ReadFile(file)
		if err != nil {
			break
		}
		if _, err = outputFile.Write(data); err != nil {
			break
		}
	}
	if err != nil {
		return err
	}

	// Append the updated manifest file to the ZIP file:
	newManifest, err := zipWriter.Create(defaultManifestPath)
	_, err = newManifest.Write(manifestData)
	zipWriter.Close()
	err = ioutil.WriteFile(bundlePath, buf.Bytes(), defaultBundlePerm)
	if err != nil {
		return err
	}
	log.Infof("Wrote '%s' (%d bytes)", bundlePath, buf.Len())
	return nil
}

Cognitive complexity: 38, Cyclomatic complexity: 17

Uses: bufio.NewReader, bytes.Buffer, fmt.Sprintf, io.Writer, ioutil.ReadFile, ioutil.WriteFile, json.Marshal, md5.Sum, os.Stdin, zip.NewWriter.

func (*Bundler) Bundle

Bundle is the entrypoint function for this subcommand.

func (b *Bundler) Bundle(ctx *kingpin.ParseContext) error {
	return nil
}

Cognitive complexity: 0, Cyclomatic complexity: 1

Private functions

func loadManifest

loadManifest (path string) (*apidef.BundleManifest, error)
References: ioutil.ReadFile, json.Unmarshal.

func sign

sign (key string, manifest *apidef.BundleManifest, bundle *bytes.Buffer) error
References: base64.StdEncoding, goverify.LoadPrivateKeyFromFile.

func validateManifest

validateManifest (manifest *apidef.BundleManifest) error
References: errors.New, os.Stat.


Tests

Files: 1. Third party imports: 1. Imports from organisation: 0. Tests: 3. Benchmarks: 0.

Vars

var (
	testApp	*kingpin.Application

	standardManifest	= &apidef.BundleManifest{
		FileList:	[]string{},
		CustomMiddleware: apidef.MiddlewareSection{
			Pre: []apidef.MiddlewareDefinition{
				{
					Name: "MyPreHook",
				},
			},
			Driver:	"python",
		},
	}
)

Test functions

TestBuild

References: apidef.BundleManifest, apidef.MiddlewareDefinition, apidef.MiddlewareSection, apidef.PythonDriver, bytes.Buffer, errors.Is, ioutil.WriteFile, json.Unmarshal, kingpin.ParseContext, os.Remove, strings.Contains, testing.T, zip.OpenReader.

TestCommands

References: os.Remove.

TestMain

References: kingpin.New, os.Exit.