(ctx context.Context, tabId string, blockDef *waveobj.BlockDef, rtOpts *waveobj.RuntimeOpts, recordTelemetry bool)
| 60 | } |
| 61 | |
| 62 | func CreateBlockWithTelemetry(ctx context.Context, tabId string, blockDef *waveobj.BlockDef, rtOpts *waveobj.RuntimeOpts, recordTelemetry bool) (rtnBlock *waveobj.Block, rtnErr error) { |
| 63 | var blockCreated bool |
| 64 | var newBlockOID string |
| 65 | defer func() { |
| 66 | if rtnErr == nil { |
| 67 | return |
| 68 | } |
| 69 | // if there was an error, and we created the block, clean it up since the function failed |
| 70 | if blockCreated && newBlockOID != "" { |
| 71 | deleteBlockObj(ctx, newBlockOID) |
| 72 | filestore.WFS.DeleteZone(ctx, newBlockOID) |
| 73 | } |
| 74 | }() |
| 75 | if blockDef == nil { |
| 76 | return nil, fmt.Errorf("blockDef is nil") |
| 77 | } |
| 78 | if blockDef.Meta == nil || blockDef.Meta.GetString(waveobj.MetaKey_View, "") == "" { |
| 79 | return nil, fmt.Errorf("no view provided for new block") |
| 80 | } |
| 81 | blockData, err := createBlockObj(ctx, tabId, blockDef, rtOpts) |
| 82 | if err != nil { |
| 83 | return nil, fmt.Errorf("error creating block: %w", err) |
| 84 | } |
| 85 | blockCreated = true |
| 86 | newBlockOID = blockData.OID |
| 87 | // upload the files if present |
| 88 | if len(blockDef.Files) > 0 { |
| 89 | for fileName, fileDef := range blockDef.Files { |
| 90 | err := filestore.WFS.MakeFile(ctx, newBlockOID, fileName, fileDef.Meta, wshrpc.FileOpts{}) |
| 91 | if err != nil { |
| 92 | return nil, fmt.Errorf("error making blockfile %q: %w", fileName, err) |
| 93 | } |
| 94 | err = filestore.WFS.WriteFile(ctx, newBlockOID, fileName, []byte(fileDef.Content)) |
| 95 | if err != nil { |
| 96 | return nil, fmt.Errorf("error writing blockfile %q: %w", fileName, err) |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | if recordTelemetry { |
| 101 | blockView := blockDef.Meta.GetString(waveobj.MetaKey_View, "") |
| 102 | blockController := blockDef.Meta.GetString(waveobj.MetaKey_Controller, "") |
| 103 | go recordBlockCreationTelemetry(blockView, blockController) |
| 104 | } |
| 105 | return blockData, nil |
| 106 | } |
| 107 | |
| 108 | func recordBlockCreationTelemetry(blockView string, blockController string) { |
| 109 | defer func() { |
no test coverage detected