Skip to content

Commit

Permalink
chore: apply code review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielmpinto committed Oct 28, 2024
1 parent 5e2d877 commit e1ef370
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 37 deletions.
6 changes: 3 additions & 3 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,10 @@
"cmdAuthLoginDescription": "Authenticate the CLI session using the Fleek Platform Web app. Open the URL in your favourite browser to initiate the browser-based login process. Select your preferred authentication method and return to CLI once completed.",
"cmdAuthLogoutDescription": "Ends your active CLI session, securing your account. Disables access to personal features such as storage and site deployment until re-authentication.",
"invalidHostname": "Invalid hostname `{hostname}`. Please try again!",
"functionsUseAssets": "Path to assets to be uploaded to Fleek Storage along with your function code. Accessible via the global variable `fleek.env.ASSETS_URL`.",
"assetsPathIsNotAFolder": "The provided assets path is not a valid folder",
"functionsUseAssets": "Choose the assets directory to upload alongside your function. These assets will be accessible in your code through 'fleek.env.ASSETS_CID', e.g. /User/MyProject/assets.",
"assetsPathIsNotAFolder": "The specified path doesn't exist or is not a valid directory. Please provide a valid directory path containing your assets.",
"uploadingAssets": "Uploading assets...",
"assetsUploadSuccess": "Assets uploaded successfully",
"uploadAssetsFailed": "Failed to upload assets for function",
"uploadAssetsFailed": "Failed to upload function assets to Fleek. Check your network connection and try again.",
"assetsNotSupportedInSgx": "Function assets are not supported in SGX"
}
45 changes: 13 additions & 32 deletions src/commands/functions/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ type DeployActionArgs = {
assetsPath?: string;
};

const deployAction: SdkGuardedFunction<DeployActionArgs> = async ({
sdk,
args,
}) => {
const deployAction: SdkGuardedFunction<DeployActionArgs> = async ({ sdk, args }) => {
const env = getEnvironmentVariables({ env: args.env, envFile: args.envFile });
const functionToDeploy = await getFunctionOrPrompt({ name: args.name, sdk });
const filePath = await getFunctionPathOrPrompt({ path: args.filePath });
Expand All @@ -56,21 +53,11 @@ const deployAction: SdkGuardedFunction<DeployActionArgs> = async ({
return;
}

let assetsCid: string | undefined = undefined;
if (assetsPath) {
console.log('assetsPath', assetsPath);
if (!(await isValidFolder(assetsPath))) {
output.error(t('assetsPathIsNotAFolder'));
return;
}
output.spinner(t('uploadingAssets'));
assetsCid = await uploadFunctionAssets({
sdk,
assetsPath,
functionName: functionToDeploy.name,
});
output.success(t('assetsUploadSuccess'));
}
const assetsCid = await uploadFunctionAssets({
sdk,
assetsPath,
functionName: functionToDeploy.name,
});

const filePathToUpload = isSGX
? await getWasmCodeFromPath({ filePath })
Expand All @@ -87,7 +74,7 @@ const deployAction: SdkGuardedFunction<DeployActionArgs> = async ({
{
format: t('uploadProgress', { action: t('uploadCodeToIpfs') }),
},
cliProgress.Presets.shades_grey,
cliProgress.Presets.shades_grey
);

const uploadResult = await getUploadResult({
Expand All @@ -107,7 +94,7 @@ const deployAction: SdkGuardedFunction<DeployActionArgs> = async ({
action: 'deploy',
tryAgain: t('tryAgain'),
message: t('uploadToIpfsFailed'),
}),
})
);

return;
Expand All @@ -133,7 +120,7 @@ const deployAction: SdkGuardedFunction<DeployActionArgs> = async ({
action: 'deploy',
tryAgain: t('tryAgain'),
message: t('uploadToIpfsFailed'),
}),
})
);

return;
Expand Down Expand Up @@ -187,9 +174,7 @@ const deployAction: SdkGuardedFunction<DeployActionArgs> = async ({
output.spinner(t('networkFetchMappings'));
try {
// TODO: The `fleek-test` address should be an env var
await fetch(
`https://fleek-test.network/services/0/ipfs/${uploadResult.pin.cid}`,
);
await fetch(`https://fleek-test.network/services/0/ipfs/${uploadResult.pin.cid}`);
} catch {
output.error(t('networkFetchFailed'));
return;
Expand All @@ -207,20 +192,16 @@ const deployAction: SdkGuardedFunction<DeployActionArgs> = async ({
output.printNewLine();
output.log(`Blake3 Hash: ${blake3Hash} `);
output.log(
`Invoke by sending request to https://fleek-test.network/services/3 with payload of {hash: <Blake3Hash>, decrypt: true, inputs: "foo"}`,
`Invoke by sending request to https://fleek-test.network/services/3 with payload of {hash: <Blake3Hash>, decrypt: true, inputs: "foo"}`
);
output.printNewLine();
output.hint(`Here's an example:`);
output.link(
`curl ${functionToDeploy.invokeUrl} --data '{"hash": "${blake3Hash}", "decrypt": true, "input": "foo"}'`,
);
output.link(`curl ${functionToDeploy.invokeUrl} --data '{"hash": "${blake3Hash}", "decrypt": true, "input": "foo"}'`);
}

if (isUntrustedPublicEnvironment) {
output.log(t('callFleekFunctionByNetworkUrlReq'));
output.link(
`https://fleek-test.network/services/1/ipfs/${uploadResult.pin.cid}`,
);
output.link(`https://fleek-test.network/services/1/ipfs/${uploadResult.pin.cid}`);
}
};

Expand Down
14 changes: 12 additions & 2 deletions src/commands/functions/utils/uploadFunctionAssets.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import type { FleekSdk } from '@fleek-platform/sdk/node';
import { output } from '../../../cli';
import { t } from '../../../utils/translation';
import { isValidFolder } from '@fleek-platform/utils-validation';

export const uploadFunctionAssets = async ({
sdk,
assetsPath,
functionName,
}: {
sdk: FleekSdk;
assetsPath: string;
functionName: string;
}): Promise<string> => {
assetsPath?: string;
}): Promise<string | undefined> => {
if (!assetsPath) {
return;
}

if (!(await isValidFolder(assetsPath))) {
output.error(t('assetsPathIsNotAFolder'));
return;
}

try {
output.spinner(t('uploadingAssets'));
const result = await sdk.storage().uploadDirectory({
Expand Down

0 comments on commit e1ef370

Please sign in to comment.