func()

in plugins/teststeps/s3fileupload/s3fileupload.go [86:132]


func (ts *FileUpload) Run(ctx xcontext.Context, ch test.TestStepChannels, params test.TestStepParameters,
	ev testevent.Emitter, resumeState json.RawMessage) (json.RawMessage, error) {
	// Validate the parameter
	if err := ts.validateAndPopulate(params); err != nil {
		return nil, err
	}
	f := func(ctx xcontext.Context, target *target.Target) error {
		// expand args
		path, err := ts.localPath.Expand(target)
		if err != nil {
			return fmt.Errorf("failed to expand argument '%s': %v", ts.localPath, err)
		}
		filename, err := ts.fileName.Expand(target)
		if err != nil {
			return fmt.Errorf("failed to expand argument dir '%s': %v", ts.fileName, err)
		}
		// bodyBytes will contain the file data to upload it
		var bodyBytes []byte
		// Compress if compress parameter is true
		if ts.compGzip {
			// Create the archive and write the output to the "out" Writer
			buf, err := createTarArchive(path, ctx)
			if err != nil {
				return fmt.Errorf("error creating an archive: %v", err)
			}
			filename = filename + ".tar.gz"
			bodyBytes = buf.Bytes()
		} else {
			// Read the file that should be uploaded
			bodyBytes, err = os.ReadFile(path)
			if err != nil {
				return fmt.Errorf("could not read the file: %v", err)
			}
		}
		// Upload file
		url, err := ts.upload(filename, bodyBytes, ctx)
		if err != nil {
			return fmt.Errorf("could not upload the file: %v", err)
		}
		// Emit URL event to get the url into the report
		if err := emitEvent(ctx, EventURL, eventURLPayload{Msg: url}, target, ev); err != nil {
			return fmt.Errorf("failed to emit event: %w", err)
		}
		return nil
	}
	return teststeps.ForEachTarget(Name, ctx, ch, f)
}