transform

package
v0.21.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2024 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ExportCmd = &cobra.Command{
	Use:     "get",
	Aliases: []string{"export"},
	Short:   "Get an already existing transform",
	Long: `Get an already existing transform. For example:
mim transform get --id <my-job> -file out.js
or
mim transform export <my-job> > out.js

`,
	Run: func(cmd *cobra.Command, args []string) {
		format := utils.ResolveFormat(cmd)
		if format != "term" {
			pterm.DisableOutput()
		}

		server, token, err := login.ResolveCredentials()
		utils.HandleError(err)

		idOrTitle, _ := cmd.Flags().GetString("id")
		if idOrTitle == "" && len(args) > 0 {
			idOrTitle = args[0]
		}

		if idOrTitle == "" {
			utils.HandleError(errors.New("job id is missing"))
		}

		jobManager := api.NewJobManager(server, token)
		jobId := jobManager.ResolveId(idOrTitle)
		job, err := jobManager.GetJob(jobId)
		utils.HandleError(err)

		output(job)

		pterm.Println()
	},
	TraverseChildren: true,
	ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
		if len(args) != 0 {
			return nil, cobra.ShellCompDirectiveNoFileComp
		}
		return api.GetJobsCompletion(toComplete), cobra.ShellCompDirectiveNoFileComp
	},
}

ExportCmd allows to export an existing transform as javascript

View Source
var ImportCmd = &cobra.Command{
	Use:   "import",
	Short: "Import a transform file into a job",
	Long: `Import a transform file into a job. For example:
mim transform import --job-id <my-job> --file <transform.js>
or
mim transform import <my-job> -f <transform.js>

`,
	Run: func(cmd *cobra.Command, args []string) {

		if len(args) == 0 {
			cmd.Usage()
			os.Exit(0)
		}

		server, token, err := login.ResolveCredentials()
		utils.HandleError(err)

		pterm.EnableDebugMessages()

		file, err := cmd.Flags().GetString("file")
		utils.HandleError(err)

		introSpinner, err := pterm.DefaultSpinner.WithRemoveWhenDone(false).Start("Compiling script")
		utils.HandleError(err)

		importer := NewImporter(file)
		var code []byte
		if filepath.Ext(file) == ".ts" {
			code, err = importer.ImportTs()
		} else {
			code, err = importer.ImportJs()
		}
		utils.HandleError(err)

		introSpinner.Success("Done compiling and minifying")
		introSpinner.Stop()

		encode, err := cmd.Flags().GetBool("encode-only")
		jobId, err := cmd.Flags().GetString("job-id")
		if len(args) > 0 {
			jobId = args[0]
		}

		if jobId == "" {
			encode = true
		}

		encoded := importer.Encode(code)
		if encode {
			pterm.Println(encoded)
			pterm.Println()
			os.Exit(1)
		}
		pterm.Success.Println("Done base64 encoding")

		jobManager := api.NewJobManager(server, token)

		resolvedJobId := jobManager.ResolveId(jobId)

		job, err := jobManager.GetJob(resolvedJobId)
		utils.HandleError(err)
		pterm.Success.Println("Fetched job with id " + jobId)

		tfr := make(map[string]interface{})
		tfr["Type"] = "JavascriptTransform"
		tfr["Code"] = encoded

		job.Transform = tfr

		err = updateJob(server, token, job)
		utils.HandleError(err)

		pterm.Success.Println(fmt.Sprintf("Job '%s' was updated", jobId))

		pterm.Println()
	},
	TraverseChildren: true,
	ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
		if len(args) != 0 {
			return nil, cobra.ShellCompDirectiveNoFileComp
		}
		return api.GetJobsCompletion(toComplete), cobra.ShellCompDirectiveNoFileComp
	},
}

ImportCmd allows to import a *.js (or a *.ts) file into an existing job

View Source
var TestCmd = &cobra.Command{
	Use:   "test",
	Short: "Test a transformation",
	Long: `Test a transformation from a job or a file. For example:
mim transform test --file <transform.js> --name sdb.Animal --limit 10
or
cat <transform.js> | mim transform test -n sdb.Animal

`,
	Run: func(cmd *cobra.Command, args []string) {
		format := utils.ResolveFormat(cmd)
		if format == "json" {
			pterm.DisableOutput()
		}

		server, token, err := login.ResolveCredentials()
		utils.HandleError(err)

		file, err := cmd.Flags().GetString("file")
		utils.HandleError(err)

		dataset, err := cmd.Flags().GetString("name")
		utils.HandleError(err)
		if dataset == "" && len(args) > 0 {
			dataset = args[0]
		}

		limit, err := cmd.Flags().GetInt("limit")
		utils.HandleError(err)

		pterm.DefaultSection.Println("Testing script function")

		if file == "" {
			utils.HandleError(errors.New("missing or empty file parameter"))
		}
		var res []byte
		importer := NewImporter(file)
		if filepath.Ext(file) == ".ts" {
			res, err = importer.ImportTs()
		} else {
			res, err = importer.ImportJs()
		}

		utils.HandleError(err)
		code := string(res)

		_, err = goja.Compile("transform_entities", code, false)
		utils.HandleError(err)

		engine := hookEngine(server, token)
		_, err = engine.RunString(code)
		utils.HandleError(err)
		_, err = engine.RunString(wrapperJavascriptFunction)
		utils.HandleError(err)

		_, err = engine.RunString(helperJavascriptFunctions)
		utils.HandleError(err)

		entities, err := getEntities(server, token, dataset, limit)
		utils.HandleError(err)

		transformed, err := transformEntities(entities, engine)
		utils.HandleError(err)
		outputResult(transformed, format)

		pterm.Println()
	},
	TraverseChildren: true,
}

TestCmd allows to import a *.js (or a *.ts) file into an existing job

Functions

func VerifyNodeInstallation

func VerifyNodeInstallation(imp *Importer)

Types

type Importer

type Importer struct {
	// contains filtered or unexported fields
}

func NewImporter

func NewImporter(file string) *Importer

func (*Importer) Cmd

func (imp *Importer) Cmd(cmd []string) ([]byte, error)

func (*Importer) Encode

func (imp *Importer) Encode(code []byte) string

func (*Importer) ImportJs

func (imp *Importer) ImportJs() ([]byte, error)

func (*Importer) ImportTs

func (imp *Importer) ImportTs() ([]byte, error)

func (*Importer) LoadRaw added in v0.16.0

func (imp *Importer) LoadRaw() ([]byte, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL