Skip to content

Commit

Permalink
refactor(dev-cli)!: more robust tag parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
TillaTheHun0 committed Dec 18, 2023
1 parent 7bb6029 commit 6d26e8d
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 24 deletions.
2 changes: 1 addition & 1 deletion dev-cli/container/src/node/src/bin/ao-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Promise.resolve()
artifactPath: process.env.MODULE_WASM_PATH,
to: process.env.BUNDLER_HOST,
tags: [
...parseTags(process.env.TAGS || ''),
...parseTags(process.env.TAGS || JSON.stringify([[], []])),
// Add the proper tags for ao contract source
...AoModuleTags
]
Expand Down
2 changes: 1 addition & 1 deletion dev-cli/container/src/node/src/bin/ao-spawn.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Promise.resolve()
uploadAoProcess({
walletPath: process.env.WALLET_PATH,
module: process.env.MODULE_TX,
tags: parseTags(process.env.TAGS || '')
tags: parseTags(process.env.TAGS || JSON.stringify([[], []]))
})
)
// log transaction id
Expand Down
18 changes: 10 additions & 8 deletions dev-cli/container/src/node/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,16 @@ export const determineBundlerHost = (host) => {
* @param {string} tagsStr
* @returns {Tag[]} an array of parsed tags
*/
export const parseTags = (tagsStr) =>
tagsStr
? tagsStr
.split(',')
.map((pairs) => pairs.trim()) // ['foo:bar', 'fizz: buzz']
.map((pairs) => pairs.split(':').map((v) => v.trim())) // [['foo', 'bar'], ['fizz', 'buzz']]
.map(([name, value]) => ({ name, value }))
: [] // TODO: filter out dups?
export const parseTags = (tagZipStr) => {
// [ [name1, name2], [value1, value2] ]
const [names, values] = JSON.parse(tagZipStr)
const len = Math.min(names.length, values.length)

const tags = []
for (let i = 0; i < len; i++) tags.push({ name: names[i], value: values[i] })

return tags
}

export const useFirstTag = (name) => (tags = []) => {
let found = false
Expand Down
28 changes: 27 additions & 1 deletion dev-cli/container/src/node/test/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,36 @@ import {
checkBalanceWith,
spawnProcessWith,
fundWith,
uploadModuleWith
uploadModuleWith,
parseTags
} from '../src/main.js'
import { AoModuleTags, DEFAULT_BUNDLER_HOST } from '../src/defaults.js'

describe('parseTags', () => {
it('should parse the zip', () => {
assert.deepStrictEqual(
parseTags(JSON.stringify([['foo', 'bar'], ['fizz', 'buzz']])),
[
{ name: 'foo', value: 'fizz' },
{ name: 'bar', value: 'buzz' }
]
)

assert.deepStrictEqual(
parseTags(JSON.stringify([['foo', 'bar'], ['fizz', 'buzz', 'extra']])),
[
{ name: 'foo', value: 'fizz' },
{ name: 'bar', value: 'buzz' }
]
)

assert.deepStrictEqual(
parseTags(JSON.stringify([[], []])),
[]
)
})
})

describe('uploadModuleWith', () => {
const happy = {
walletExists: async () => true,
Expand Down
17 changes: 9 additions & 8 deletions dev-cli/src/commands/publish.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global Deno */

import { Command, basename, resolve } from '../deps.js'
import { hostArgs } from '../utils.js'
import { hostArgs, tagsArg } from '../utils.js'
import { VERSION } from '../versions.js'

function walletArgs (wallet) {
Expand Down Expand Up @@ -42,23 +42,19 @@ function contractSourceArgs (contractWasmPath) {
]
}

function tagArg (tags) {
return tags ? ['-e', `TAGS=${tags.join(',')}`] : []
}

/**
* TODO:
* - Validate existence of wallet
* - Validate existence of contract wasm
* - allow using environment variables to set things like path to wallet
* - require confirmation and bypass with --yes
*/
export async function publish ({ wallet, host, tag }, contractWasmPath) {
export async function publish ({ wallet, host, tag, value }, contractWasmPath) {
const cmdArgs = [
...walletArgs(wallet),
...hostArgs(host),
...contractSourceArgs(contractWasmPath),
...tagArg(tag)
...tagsArg({ tags: tag, values: value })
]

const p = Deno.run({
Expand Down Expand Up @@ -90,7 +86,12 @@ export const command = new Command()
)
.option(
'-t, --tag <tag:string>',
'"name:value" additional tag to add to the transaction',
'additional tag to add to the transaction. MUST have a corresponding --value',
{ collect: true }
)
.option(
'-v, --value <value:string>',
'value of the preceding tag name',
{ collect: true }
)
.arguments('<wasmfile:string>')
Expand Down
11 changes: 8 additions & 3 deletions dev-cli/src/commands/spawn.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ function sourceArgs (src) {
* - Validate existence of wallet
* - require confirmation and bypass with --yes
*/
export async function spawn ({ wallet, tag, source }) {
export async function spawn ({ wallet, tag, value, source }) {
const cmdArgs = [
...walletArgs(wallet),
...sourceArgs(source),
...tagsArg(tag)
...tagsArg({ tags: tag, values: value })
]

const p = Deno.run({
Expand Down Expand Up @@ -52,7 +52,12 @@ export const command = new Command()
)
.option(
'-t, --tag <tag:string>',
'"name:value" additional tag to add to the transaction',
'additional tag to add to the transaction. MUST have a corresponding --value',
{ collect: true }
)
.option(
'-v, --value <value:string>',
'value of the preceding tag name',
{ collect: true }
)
.action(spawn)
12 changes: 10 additions & 2 deletions dev-cli/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,16 @@ export function walletArgs (wallet) {
: []
}

export function tagsArg (tags) {
return tags ? ['-e', `TAGS=${tags.join(',')}`] : []
export function tagsArg ({ tags, values }) {
if (!tags && !values) return []
if (tags && !values) throw new Error('tag values required')
if (values && !tags) throw new Error('tag names required')
if (tags.length !== values.length) throw new Error('tag value length mismatch')

/**
* Pass a stringified zip of [ [tag1, tag2], [value1, value2] ]
*/
return ['-e', `TAGS=${JSON.stringify([tags, values])}`]
}

export function hostArgs (host) {
Expand Down

0 comments on commit 6d26e8d

Please sign in to comment.