@super-protocol/sp-cli 0.0.2 → 0.0.4-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +217 -263
- package/bin/dev.js +1 -1
- package/dist/commands/account/get-sppi.d.ts +8 -0
- package/dist/commands/account/get-sppi.js +23 -0
- package/dist/commands/account/info.d.ts +22 -0
- package/dist/commands/account/info.js +46 -0
- package/dist/commands/auth/login.d.ts +6 -11
- package/dist/commands/auth/login.js +28 -76
- package/dist/commands/base.d.ts +2 -4
- package/dist/commands/base.js +8 -10
- package/dist/commands/config/add.js +1 -12
- package/dist/commands/config/base.d.ts +5 -3
- package/dist/commands/config/base.js +30 -14
- package/dist/commands/config/create.js +1 -11
- package/dist/commands/config/list.js +20 -19
- package/dist/commands/config/use.js +5 -3
- package/dist/commands/files/download.d.ts +15 -0
- package/dist/commands/files/download.js +55 -0
- package/dist/commands/files/upload.d.ts +18 -0
- package/dist/commands/files/upload.js +79 -0
- package/dist/commands/storage/base.d.ts +13 -0
- package/dist/commands/storage/base.js +125 -0
- package/dist/commands/storage/create.d.ts +11 -0
- package/dist/commands/storage/create.js +53 -0
- package/dist/commands/storage/select.d.ts +9 -0
- package/dist/commands/storage/select.js +40 -0
- package/dist/commands/storage/show.d.ts +17 -0
- package/dist/commands/storage/show.js +36 -0
- package/dist/commands/storage/update.d.ts +14 -0
- package/dist/commands/storage/update.js +187 -0
- package/dist/commands/workflows/extend-lease.d.ts +17 -0
- package/dist/commands/workflows/extend-lease.js +94 -0
- package/dist/config/config-file.schema.d.ts +4 -4
- package/dist/config/config-file.schema.js +1 -1
- package/dist/config/config.schema.d.ts +25 -15
- package/dist/config/config.schema.js +7 -10
- package/dist/constants.d.ts +5 -0
- package/dist/constants.js +5 -0
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +2 -0
- package/dist/hooks/finally/shutdown-blockchain.d.ts +3 -0
- package/dist/hooks/finally/shutdown-blockchain.js +8 -0
- package/dist/lib/container.d.ts +7 -7
- package/dist/lib/container.js +48 -26
- package/dist/managers/account-manager.d.ts +4 -1
- package/dist/managers/account-manager.js +29 -30
- package/dist/managers/config-file-manager.d.ts +2 -2
- package/dist/managers/config-file-manager.js +29 -12
- package/dist/middlewares/auth-middleware.js +5 -1
- package/dist/services/auth.service.d.ts +24 -0
- package/dist/services/auth.service.js +93 -0
- package/dist/services/storage.service.d.ts +73 -0
- package/dist/services/storage.service.js +351 -0
- package/dist/utils/helper.d.ts +6 -0
- package/dist/utils/helper.js +23 -0
- package/dist/utils/progress.d.ts +8 -0
- package/dist/utils/progress.js +35 -0
- package/oclif.manifest.json +432 -156
- package/package.json +19 -9
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { Args, Flags } from '@oclif/core';
|
|
2
|
+
import { StorageService } from '../../services/storage.service.js';
|
|
3
|
+
import { createProgressPrinter } from '../../utils/progress.js';
|
|
4
|
+
import { BaseCommand } from '../base.js';
|
|
5
|
+
export default class FilesUpload extends BaseCommand {
|
|
6
|
+
static args = {
|
|
7
|
+
path: Args.string({ description: 'file or directory to upload', required: true }),
|
|
8
|
+
};
|
|
9
|
+
static description = 'Upload file or directory to remote storage';
|
|
10
|
+
static examples = [
|
|
11
|
+
'<%= config.bin %> <%= command.id %> ./file.txt',
|
|
12
|
+
];
|
|
13
|
+
static flags = {
|
|
14
|
+
filename: Flags.string({
|
|
15
|
+
description: 'The name of the resulting file/directory in the storage',
|
|
16
|
+
required: false,
|
|
17
|
+
}),
|
|
18
|
+
'maximum-concurrent': Flags.integer({
|
|
19
|
+
default: 1, description: 'Maximum concurrent pieces to upload at once per transfer', max: 1000, min: 1,
|
|
20
|
+
}),
|
|
21
|
+
metadata: Flags.string({
|
|
22
|
+
description: 'Path to a metadata file for adding fields to the resource file',
|
|
23
|
+
required: false,
|
|
24
|
+
}),
|
|
25
|
+
output: Flags.string({
|
|
26
|
+
default: 'resource.json',
|
|
27
|
+
description: 'Path to save resource file that is used to access the uploaded file',
|
|
28
|
+
required: false,
|
|
29
|
+
}),
|
|
30
|
+
'skip-encryption': Flags.boolean({
|
|
31
|
+
default: false,
|
|
32
|
+
description: 'Skip file encryption before upload',
|
|
33
|
+
required: false,
|
|
34
|
+
}),
|
|
35
|
+
sync: Flags.boolean({
|
|
36
|
+
default: false,
|
|
37
|
+
description: 'Sync mode: delete files in target that don\'t exist in source',
|
|
38
|
+
required: false,
|
|
39
|
+
}),
|
|
40
|
+
};
|
|
41
|
+
async init() {
|
|
42
|
+
await super.init();
|
|
43
|
+
await this.container.initProviderClient().initConfigManager().build();
|
|
44
|
+
}
|
|
45
|
+
async run() {
|
|
46
|
+
const { args, flags } = await this.parse(FilesUpload);
|
|
47
|
+
const { providerClient } = this.container;
|
|
48
|
+
const storageService = new StorageService(providerClient, this.logger);
|
|
49
|
+
const progress = createProgressPrinter({
|
|
50
|
+
action: 'Upload',
|
|
51
|
+
start: 'Uploading: ',
|
|
52
|
+
});
|
|
53
|
+
try {
|
|
54
|
+
await storageService.upload({
|
|
55
|
+
localPath: args.path,
|
|
56
|
+
maximumConcurrent: flags['maximum-concurrent'],
|
|
57
|
+
metadataPath: flags.metadata,
|
|
58
|
+
outputPath: flags.output,
|
|
59
|
+
remotePath: flags.filename,
|
|
60
|
+
sync: flags.sync,
|
|
61
|
+
withEncryption: !flags['skip-encryption'],
|
|
62
|
+
}, ({ current, key, total }) => {
|
|
63
|
+
progress.advance(key, current / total * 100, `Uploading: ${key} [${current}/${total}]`);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
let reason = '';
|
|
68
|
+
this.logger.error({ err: error }, 'Error trying upload');
|
|
69
|
+
if (error instanceof Error) {
|
|
70
|
+
reason = error.message;
|
|
71
|
+
}
|
|
72
|
+
this.error(`Upload aborted. Reason: ${reason}`);
|
|
73
|
+
}
|
|
74
|
+
finally {
|
|
75
|
+
progress.finish();
|
|
76
|
+
}
|
|
77
|
+
this.log('Upload successfully completed');
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Command } from '@oclif/core';
|
|
2
|
+
import { AddStorageDto } from '@super-protocol/provider-client';
|
|
3
|
+
import { BaseCommand } from '../../commands/base.js';
|
|
4
|
+
import { StorageService } from '../../services/storage.service.js';
|
|
5
|
+
export declare abstract class BaseStorageCommand<T extends typeof Command> extends BaseCommand<T> {
|
|
6
|
+
protected currentDir: string;
|
|
7
|
+
protected storageService: StorageService;
|
|
8
|
+
init(): Promise<void>;
|
|
9
|
+
protected promptS3Credentials(): Promise<NonNullable<AddStorageDto['s3Credentials']>>;
|
|
10
|
+
protected promptStoragePayload(): Promise<AddStorageDto>;
|
|
11
|
+
protected promptStorJCredentials(): Promise<NonNullable<AddStorageDto['storjCredentials']>>;
|
|
12
|
+
protected validateStoragePayload(payload: Partial<AddStorageDto> | undefined): asserts payload is AddStorageDto;
|
|
13
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { password, select, text } from '@clack/prompts';
|
|
2
|
+
import { StorageType } from '@super-protocol/provider-client';
|
|
3
|
+
import { BaseCommand } from '../../commands/base.js';
|
|
4
|
+
import { S3_REGION } from '../../constants.js';
|
|
5
|
+
import { StorageService } from '../../services/storage.service.js';
|
|
6
|
+
export class BaseStorageCommand extends BaseCommand {
|
|
7
|
+
currentDir = process.cwd();
|
|
8
|
+
storageService;
|
|
9
|
+
async init() {
|
|
10
|
+
await super.init();
|
|
11
|
+
await this.container.initConfigManager().initProviderClient().build();
|
|
12
|
+
const { providerClient } = this.container;
|
|
13
|
+
this.storageService = new StorageService(providerClient, this.logger);
|
|
14
|
+
}
|
|
15
|
+
async promptS3Credentials() {
|
|
16
|
+
const region = S3_REGION;
|
|
17
|
+
const readAccessKeyId = this.ensurePromptValue(await text({
|
|
18
|
+
message: 'Read access key ID',
|
|
19
|
+
validate(value) {
|
|
20
|
+
return value ? undefined : 'Read access key ID is required';
|
|
21
|
+
},
|
|
22
|
+
})).trim();
|
|
23
|
+
const readSecretAccessKey = this.ensurePromptValue(await password({
|
|
24
|
+
message: 'Read secret access key',
|
|
25
|
+
validate(value) {
|
|
26
|
+
return value ? undefined : 'Read secret access key is required';
|
|
27
|
+
},
|
|
28
|
+
})).trim();
|
|
29
|
+
const writeAccessKeyId = this.ensurePromptValue(await text({
|
|
30
|
+
message: 'Write access key ID',
|
|
31
|
+
validate(value) {
|
|
32
|
+
return value ? undefined : 'Write access key ID is required';
|
|
33
|
+
},
|
|
34
|
+
})).trim();
|
|
35
|
+
const writeSecretAccessKey = this.ensurePromptValue(await password({
|
|
36
|
+
message: 'Write secret access key',
|
|
37
|
+
validate(value) {
|
|
38
|
+
return value ? undefined : 'Write secret access key is required';
|
|
39
|
+
},
|
|
40
|
+
})).trim();
|
|
41
|
+
return {
|
|
42
|
+
readAccessKeyId,
|
|
43
|
+
readSecretAccessKey,
|
|
44
|
+
region,
|
|
45
|
+
writeAccessKeyId,
|
|
46
|
+
writeSecretAccessKey,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
async promptStoragePayload() {
|
|
50
|
+
const storageType = this.ensurePromptValue(await select({
|
|
51
|
+
message: 'Select storage type',
|
|
52
|
+
options: [
|
|
53
|
+
{ label: 'S3', value: StorageType.S3 },
|
|
54
|
+
{ label: 'StorJ', value: StorageType.StorJ },
|
|
55
|
+
],
|
|
56
|
+
}));
|
|
57
|
+
const bucket = this.ensurePromptValue(await text({
|
|
58
|
+
message: 'Bucket name',
|
|
59
|
+
validate(value) {
|
|
60
|
+
return value ? undefined : 'Bucket is required';
|
|
61
|
+
},
|
|
62
|
+
})).trim();
|
|
63
|
+
const prefix = this.ensurePromptValue(await text({
|
|
64
|
+
defaultValue: '/',
|
|
65
|
+
initialValue: '/',
|
|
66
|
+
message: 'Prefix',
|
|
67
|
+
validate(value) {
|
|
68
|
+
return value ? undefined : 'Prefix is required';
|
|
69
|
+
},
|
|
70
|
+
})).trim();
|
|
71
|
+
const storage = {
|
|
72
|
+
bucket,
|
|
73
|
+
prefix,
|
|
74
|
+
storageType,
|
|
75
|
+
};
|
|
76
|
+
if (storageType === StorageType.S3) {
|
|
77
|
+
storage.s3Credentials = await this.promptS3Credentials();
|
|
78
|
+
}
|
|
79
|
+
if (storageType === StorageType.StorJ) {
|
|
80
|
+
storage.storjCredentials = await this.promptStorJCredentials();
|
|
81
|
+
}
|
|
82
|
+
return storage;
|
|
83
|
+
}
|
|
84
|
+
async promptStorJCredentials() {
|
|
85
|
+
const readAccessToken = this.ensurePromptValue(await password({
|
|
86
|
+
message: 'Read access token',
|
|
87
|
+
validate(value) {
|
|
88
|
+
return value ? undefined : 'Read access token is required';
|
|
89
|
+
},
|
|
90
|
+
})).trim();
|
|
91
|
+
const writeAccessToken = this.ensurePromptValue(await password({
|
|
92
|
+
message: 'Write access token',
|
|
93
|
+
validate(value) {
|
|
94
|
+
return value ? undefined : 'Write access token is required';
|
|
95
|
+
},
|
|
96
|
+
})).trim();
|
|
97
|
+
return {
|
|
98
|
+
readAccessToken,
|
|
99
|
+
writeAccessToken,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
validateStoragePayload(payload) {
|
|
103
|
+
if (!payload || typeof payload !== 'object') {
|
|
104
|
+
this.error('Storage definition must be an object');
|
|
105
|
+
}
|
|
106
|
+
if (!payload.bucket) {
|
|
107
|
+
this.error('Storage definition missing "bucket"');
|
|
108
|
+
}
|
|
109
|
+
if (!payload.prefix) {
|
|
110
|
+
this.error('Storage definition missing "prefix"');
|
|
111
|
+
}
|
|
112
|
+
if (!payload.storageType) {
|
|
113
|
+
this.error('Storage definition missing "storageType"');
|
|
114
|
+
}
|
|
115
|
+
if (!Object.values(StorageType).includes(payload.storageType)) {
|
|
116
|
+
this.error(`Unsupported storage type: ${payload.storageType}`);
|
|
117
|
+
}
|
|
118
|
+
if (payload.storageType === StorageType.S3 && !payload.s3Credentials) {
|
|
119
|
+
this.error('S3 storage requires "s3Credentials"');
|
|
120
|
+
}
|
|
121
|
+
if (payload.storageType === StorageType.StorJ && !payload.storjCredentials) {
|
|
122
|
+
this.error('StorJ storage requires "storjCredentials"');
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { BaseStorageCommand } from './base.js';
|
|
2
|
+
export default class StorageCreate extends BaseStorageCommand<typeof StorageCreate> {
|
|
3
|
+
static description: string;
|
|
4
|
+
static examples: string[];
|
|
5
|
+
static flags: {
|
|
6
|
+
fromFile: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
7
|
+
notDefault: import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
8
|
+
};
|
|
9
|
+
run(): Promise<void>;
|
|
10
|
+
private loadStorageFromFile;
|
|
11
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Flags } from '@oclif/core';
|
|
2
|
+
import fs from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { BaseStorageCommand } from './base.js';
|
|
5
|
+
export default class StorageCreate extends BaseStorageCommand {
|
|
6
|
+
static description = 'Create a new storage entry from file or interactive prompts.';
|
|
7
|
+
static examples = [
|
|
8
|
+
'<%= config.bin %> storage create',
|
|
9
|
+
'<%= config.bin %> storage create --fromFile ./storage.json',
|
|
10
|
+
'<%= config.bin %> storage create --not-default',
|
|
11
|
+
];
|
|
12
|
+
static flags = {
|
|
13
|
+
fromFile: Flags.string({
|
|
14
|
+
aliases: ['fromFile'],
|
|
15
|
+
char: 'f',
|
|
16
|
+
description: 'Path to a JSON file that contains AddStorageDto payload.',
|
|
17
|
+
}),
|
|
18
|
+
notDefault: Flags.boolean({
|
|
19
|
+
description: 'Skip setting the created storage as default.',
|
|
20
|
+
}),
|
|
21
|
+
};
|
|
22
|
+
async run() {
|
|
23
|
+
const { flags } = await this.parse(StorageCreate);
|
|
24
|
+
const payload = flags.fromFile
|
|
25
|
+
? await this.loadStorageFromFile(flags.fromFile)
|
|
26
|
+
: await this.promptStoragePayload();
|
|
27
|
+
try {
|
|
28
|
+
const storage = await this.storageService.createStorage(payload);
|
|
29
|
+
this.log(`Storage created: ${storage.id}`);
|
|
30
|
+
if (!flags.notDefault) {
|
|
31
|
+
await this.storageService.saveStorage(storage.id);
|
|
32
|
+
this.log('Storage set as default');
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
this.error(error instanceof Error ? error.message : String(error));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async loadStorageFromFile(filePath) {
|
|
40
|
+
const absolutePath = path.isAbsolute(filePath)
|
|
41
|
+
? filePath
|
|
42
|
+
: path.join(this.currentDir, filePath);
|
|
43
|
+
try {
|
|
44
|
+
const fileContent = await fs.readFile(absolutePath, 'utf8');
|
|
45
|
+
const parsed = JSON.parse(fileContent);
|
|
46
|
+
this.validateStoragePayload(parsed);
|
|
47
|
+
return parsed;
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
this.error(`Failed to load storage definition: ${error instanceof Error ? error.message : String(error)}`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { BaseStorageCommand } from './base.js';
|
|
2
|
+
export default class StorageSelect extends BaseStorageCommand<typeof StorageSelect> {
|
|
3
|
+
static description: string;
|
|
4
|
+
static examples: string[];
|
|
5
|
+
run(): Promise<{
|
|
6
|
+
label: string;
|
|
7
|
+
selectedStorageId: string;
|
|
8
|
+
}>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { select } from '@clack/prompts';
|
|
2
|
+
import { StoragesUndefinedError } from '../../services/storage.service.js';
|
|
3
|
+
import { BaseStorageCommand } from './base.js';
|
|
4
|
+
export default class StorageSelect extends BaseStorageCommand {
|
|
5
|
+
static description = 'Select a storage that will be used by subsequent commands.';
|
|
6
|
+
static examples = [
|
|
7
|
+
'<%= config.bin %> storage select',
|
|
8
|
+
];
|
|
9
|
+
async run() {
|
|
10
|
+
try {
|
|
11
|
+
const storages = await this.storageService.requestStorages();
|
|
12
|
+
const storageId = this.ensurePromptValue(await select({
|
|
13
|
+
message: 'Select storage',
|
|
14
|
+
options: storages.map(storage => ({
|
|
15
|
+
label: this.storageService.getLabel(storage),
|
|
16
|
+
value: storage.id,
|
|
17
|
+
})),
|
|
18
|
+
}));
|
|
19
|
+
if (!storageId) {
|
|
20
|
+
this.error('Storage ID is required');
|
|
21
|
+
}
|
|
22
|
+
const storage = storages.find(storage => storage.id === storageId);
|
|
23
|
+
if (!storage) {
|
|
24
|
+
this.error(`Storage with ID: ${storageId} not found `);
|
|
25
|
+
}
|
|
26
|
+
const label = this.storageService.getLabel(storage);
|
|
27
|
+
await this.storageService.saveStorage(storageId);
|
|
28
|
+
this.log(`Selected storage: ${label}`);
|
|
29
|
+
return { label, selectedStorageId: storageId };
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
if (error instanceof StoragesUndefinedError) {
|
|
33
|
+
this.error('No storages available. Run "sp storage create" first.');
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
this.error(error instanceof Error ? error.message : String(error));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { BaseStorageCommand } from './base.js';
|
|
2
|
+
export default class StorageShow extends BaseStorageCommand<typeof StorageShow> {
|
|
3
|
+
static description: string;
|
|
4
|
+
static examples: string[];
|
|
5
|
+
run(): Promise<{
|
|
6
|
+
bucket: string;
|
|
7
|
+
createdAt: string;
|
|
8
|
+
id: string;
|
|
9
|
+
isCentralized: boolean;
|
|
10
|
+
prefix: string;
|
|
11
|
+
s3Credentials?: import("@super-protocol/provider-client").components["schemas"]["S3CredentialsResponseDto"];
|
|
12
|
+
storageType: import("@super-protocol/provider-client").components["schemas"]["StorageType"];
|
|
13
|
+
storjCredentials?: import("@super-protocol/provider-client").components["schemas"]["StorJCredentialsResponseDto"];
|
|
14
|
+
updatedAt: string;
|
|
15
|
+
userId: string;
|
|
16
|
+
}>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { StorageType } from '@super-protocol/provider-client';
|
|
2
|
+
import { BaseStorageCommand } from './base.js';
|
|
3
|
+
const stripString = (str, chars) => {
|
|
4
|
+
if (str.length <= 2 * chars) {
|
|
5
|
+
return '*'.repeat(str.length);
|
|
6
|
+
}
|
|
7
|
+
return `${str.slice(0, chars)}...${str.slice(-1 * chars)}`;
|
|
8
|
+
};
|
|
9
|
+
export default class StorageShow extends BaseStorageCommand {
|
|
10
|
+
static description = 'Show current selected storage';
|
|
11
|
+
static examples = [
|
|
12
|
+
'<%= config.bin %> <%= command.id %>',
|
|
13
|
+
];
|
|
14
|
+
async run() {
|
|
15
|
+
const storage = await this.storageService.getCurrentStorage();
|
|
16
|
+
this.log(`Current storage: ${storage.isCentralized ? 'Super Cloud' : 'Custom storage'}`);
|
|
17
|
+
this.log(`- ID: ${storage.id}`);
|
|
18
|
+
this.log(`- Storage type: ${storage.storageType}`);
|
|
19
|
+
this.log(`- Bucket: ${storage.bucket}`);
|
|
20
|
+
this.log(`- Prefix: ${storage.prefix}`);
|
|
21
|
+
this.log(' -- Credentials --');
|
|
22
|
+
if (storage.storageType === StorageType.S3) {
|
|
23
|
+
const { readAccessKeyId, readSecretAccessKey = '', writeAccessKeyId, writeSecretAccessKey = '' } = storage.s3Credentials || {};
|
|
24
|
+
this.log(` readAccessKeyId: ${readAccessKeyId}`);
|
|
25
|
+
this.log(` readSecretAccessKey: ${stripString(readSecretAccessKey, 5)}`);
|
|
26
|
+
this.log(` writeAccessKeyId: ${writeAccessKeyId}`);
|
|
27
|
+
this.log(` writeSecretAccessKey: ${stripString(writeSecretAccessKey, 5)}`);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
const { readAccessToken = '', writeAccessToken = '' } = storage.storjCredentials || {};
|
|
31
|
+
this.log(` readAccessToken: ${stripString(readAccessToken, 5)}`);
|
|
32
|
+
this.log(` writeAccessToken: ${stripString(writeAccessToken, 5)}`);
|
|
33
|
+
}
|
|
34
|
+
return storage;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { BaseStorageCommand } from './base.js';
|
|
2
|
+
export default class StorageUpdate extends BaseStorageCommand<typeof StorageUpdate> {
|
|
3
|
+
static description: string;
|
|
4
|
+
static examples: string[];
|
|
5
|
+
static flags: {
|
|
6
|
+
fromFile: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
7
|
+
id: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
8
|
+
};
|
|
9
|
+
run(): Promise<void>;
|
|
10
|
+
private ensureNonEmptyString;
|
|
11
|
+
private ensureUpdatePayload;
|
|
12
|
+
private loadStorageFromFile;
|
|
13
|
+
private promptStorageUpdate;
|
|
14
|
+
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { confirm, select, text, } from '@clack/prompts';
|
|
2
|
+
import { Flags } from '@oclif/core';
|
|
3
|
+
import { StorageType } from '@super-protocol/provider-client';
|
|
4
|
+
import fs from 'node:fs/promises';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { StoragesUndefinedError } from '../../services/storage.service.js';
|
|
7
|
+
import { BaseStorageCommand } from './base.js';
|
|
8
|
+
export default class StorageUpdate extends BaseStorageCommand {
|
|
9
|
+
static description = 'Update the configuration of an existing storage.';
|
|
10
|
+
static examples = [
|
|
11
|
+
'<%= config.bin %> storage update --id=2de3e3a4-0000-1111-2222-333344445555 --fromFile ./storage-update.json',
|
|
12
|
+
'<%= config.bin %> storage update --id=2de3e3a4-0000-1111-2222-333344445555',
|
|
13
|
+
];
|
|
14
|
+
static flags = {
|
|
15
|
+
fromFile: Flags.string({
|
|
16
|
+
char: 'f',
|
|
17
|
+
description: 'Path to a JSON file that contains UpdateStorageDto payload.',
|
|
18
|
+
}),
|
|
19
|
+
id: Flags.string({
|
|
20
|
+
char: 'i',
|
|
21
|
+
description: 'Storage ID to update',
|
|
22
|
+
}),
|
|
23
|
+
};
|
|
24
|
+
async run() {
|
|
25
|
+
const { flags } = await this.parse(StorageUpdate);
|
|
26
|
+
try {
|
|
27
|
+
const shouldRequestStorages = !flags.fromFile || !flags.id;
|
|
28
|
+
const storages = shouldRequestStorages ? await this.storageService.requestStorages() : undefined;
|
|
29
|
+
let storageId = flags.id;
|
|
30
|
+
if (!storageId) {
|
|
31
|
+
const options = (storages || []).filter(cp => !cp.isCentralized).map(storage => ({
|
|
32
|
+
label: `${storage.id} (${storage.storageType}) ${storage.bucket}/${storage.prefix} `,
|
|
33
|
+
value: storage.id,
|
|
34
|
+
}));
|
|
35
|
+
if (!options || options.length === 0) {
|
|
36
|
+
throw new StoragesUndefinedError('No storages available to update');
|
|
37
|
+
}
|
|
38
|
+
storageId = this.ensurePromptValue(await select({
|
|
39
|
+
message: 'Select storage to update',
|
|
40
|
+
options,
|
|
41
|
+
}));
|
|
42
|
+
}
|
|
43
|
+
if (!storageId) {
|
|
44
|
+
this.error('Storage ID is required');
|
|
45
|
+
}
|
|
46
|
+
const storageToUpdate = storages?.find(storage => storage.id === storageId);
|
|
47
|
+
if (flags.id && storages && !storageToUpdate) {
|
|
48
|
+
this.error(`Storage with ID ${storageId} not found`);
|
|
49
|
+
}
|
|
50
|
+
const updatePayload = flags.fromFile
|
|
51
|
+
? await this.loadStorageFromFile(flags.fromFile)
|
|
52
|
+
: await this.promptStorageUpdate(storageToUpdate);
|
|
53
|
+
await this.storageService.updateStorage(storageId, updatePayload);
|
|
54
|
+
this.log(`Storage updated: ${storageId}`);
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
if (error instanceof StoragesUndefinedError) {
|
|
58
|
+
this.error('No storages available. Run "sp storage create" first.');
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
this.error(error instanceof Error ? error.message : String(error));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
ensureNonEmptyString(value, fieldName) {
|
|
66
|
+
if (typeof value !== 'string' || !value.trim()) {
|
|
67
|
+
this.error(`${fieldName} must be a non-empty string`);
|
|
68
|
+
}
|
|
69
|
+
return value.trim();
|
|
70
|
+
}
|
|
71
|
+
ensureUpdatePayload(payload) {
|
|
72
|
+
if (!payload || typeof payload !== 'object') {
|
|
73
|
+
this.error('Storage update definition must be an object');
|
|
74
|
+
}
|
|
75
|
+
const updateFields = ['bucket', 'prefix', 'storageType', 's3Credentials', 'storjCredentials'];
|
|
76
|
+
const hasUpdates = updateFields.some(key => payload[key] !== undefined);
|
|
77
|
+
if (!hasUpdates) {
|
|
78
|
+
this.error('Update payload must include at least one property');
|
|
79
|
+
}
|
|
80
|
+
if (payload.storageType && !Object.values(StorageType).includes(payload.storageType)) {
|
|
81
|
+
this.error(`Unsupported storage type: ${payload.storageType}`);
|
|
82
|
+
}
|
|
83
|
+
if (payload.s3Credentials) {
|
|
84
|
+
const { readAccessKeyId, readSecretAccessKey, region, writeAccessKeyId, writeSecretAccessKey, } = payload.s3Credentials;
|
|
85
|
+
if (!readAccessKeyId || !readSecretAccessKey || !region || !writeAccessKeyId || !writeSecretAccessKey) {
|
|
86
|
+
this.error('S3 credentials must include readAccessKeyId, readSecretAccessKey, region, writeAccessKeyId and writeSecretAccessKey');
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (payload.storjCredentials) {
|
|
90
|
+
const { readAccessToken, writeAccessToken } = payload.storjCredentials;
|
|
91
|
+
if (!readAccessToken || !writeAccessToken) {
|
|
92
|
+
this.error('StorJ credentials must include readAccessToken and writeAccessToken');
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const sanitized = {};
|
|
96
|
+
if (payload.bucket !== undefined) {
|
|
97
|
+
sanitized.bucket = this.ensureNonEmptyString(payload.bucket, 'Bucket');
|
|
98
|
+
}
|
|
99
|
+
if (payload.prefix !== undefined) {
|
|
100
|
+
sanitized.prefix = this.ensureNonEmptyString(payload.prefix, 'Prefix');
|
|
101
|
+
}
|
|
102
|
+
if (payload.storageType) {
|
|
103
|
+
sanitized.storageType = payload.storageType;
|
|
104
|
+
}
|
|
105
|
+
if (payload.s3Credentials) {
|
|
106
|
+
sanitized.s3Credentials = {
|
|
107
|
+
readAccessKeyId: this.ensureNonEmptyString(payload.s3Credentials.readAccessKeyId, 'S3 credential "readAccessKeyId"'),
|
|
108
|
+
readSecretAccessKey: this.ensureNonEmptyString(payload.s3Credentials.readSecretAccessKey, 'S3 credential "readSecretAccessKey"'),
|
|
109
|
+
region: this.ensureNonEmptyString(payload.s3Credentials.region, 'S3 credential "region"'),
|
|
110
|
+
writeAccessKeyId: this.ensureNonEmptyString(payload.s3Credentials.writeAccessKeyId, 'S3 credential "writeAccessKeyId"'),
|
|
111
|
+
writeSecretAccessKey: this.ensureNonEmptyString(payload.s3Credentials.writeSecretAccessKey, 'S3 credential "writeSecretAccessKey"'),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
if (payload.storjCredentials) {
|
|
115
|
+
sanitized.storjCredentials = {
|
|
116
|
+
readAccessToken: this.ensureNonEmptyString(payload.storjCredentials.readAccessToken, 'StorJ credential "readAccessToken"'),
|
|
117
|
+
writeAccessToken: this.ensureNonEmptyString(payload.storjCredentials.writeAccessToken, 'StorJ credential "writeAccessToken"'),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
return sanitized;
|
|
121
|
+
}
|
|
122
|
+
async loadStorageFromFile(filePath) {
|
|
123
|
+
const absolutePath = path.isAbsolute(filePath) ? filePath : path.join(this.currentDir, filePath);
|
|
124
|
+
try {
|
|
125
|
+
const fileContent = await fs.readFile(absolutePath, 'utf8');
|
|
126
|
+
const parsed = JSON.parse(fileContent);
|
|
127
|
+
return this.ensureUpdatePayload(parsed);
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
this.error(`Failed to load storage update definition: ${error instanceof Error ? error.message : String(error)}`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
async promptStorageUpdate(existingStorage) {
|
|
134
|
+
const payload = {};
|
|
135
|
+
const bucket = this.ensurePromptValue(await text({
|
|
136
|
+
defaultValue: existingStorage?.bucket ?? '',
|
|
137
|
+
message: 'Bucket name (leave empty to keep current)',
|
|
138
|
+
})).trim();
|
|
139
|
+
if (bucket) {
|
|
140
|
+
payload.bucket = bucket;
|
|
141
|
+
}
|
|
142
|
+
const prefix = this.ensurePromptValue(await text({
|
|
143
|
+
defaultValue: existingStorage?.prefix ?? '',
|
|
144
|
+
message: 'Prefix (leave empty to keep current)',
|
|
145
|
+
})).trim();
|
|
146
|
+
if (prefix) {
|
|
147
|
+
payload.prefix = prefix;
|
|
148
|
+
}
|
|
149
|
+
let newStorageType;
|
|
150
|
+
const shouldChangeType = this.ensurePromptValue(await confirm({
|
|
151
|
+
initialValue: false,
|
|
152
|
+
message: existingStorage
|
|
153
|
+
? `Change storage type? (current: ${existingStorage.storageType})`
|
|
154
|
+
: 'Change storage type?',
|
|
155
|
+
}));
|
|
156
|
+
if (shouldChangeType) {
|
|
157
|
+
newStorageType = this.ensurePromptValue(await select({
|
|
158
|
+
message: 'Select new storage type',
|
|
159
|
+
options: [
|
|
160
|
+
{ label: 'Amazon S3', value: StorageType.S3 },
|
|
161
|
+
{ label: 'StorJ', value: StorageType.StorJ },
|
|
162
|
+
],
|
|
163
|
+
}));
|
|
164
|
+
payload.storageType = newStorageType;
|
|
165
|
+
}
|
|
166
|
+
const effectiveType = newStorageType ?? existingStorage?.storageType;
|
|
167
|
+
const credentialsRequired = Boolean(newStorageType);
|
|
168
|
+
if (effectiveType) {
|
|
169
|
+
const updateCredentials = credentialsRequired || this.ensurePromptValue(await confirm({
|
|
170
|
+
initialValue: false,
|
|
171
|
+
message: `Update ${effectiveType} credentials?`,
|
|
172
|
+
}));
|
|
173
|
+
if (updateCredentials) {
|
|
174
|
+
if (effectiveType === StorageType.S3) {
|
|
175
|
+
payload.s3Credentials = await this.promptS3Credentials();
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
payload.storjCredentials = await this.promptStorJCredentials();
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (Object.keys(payload).length === 0) {
|
|
183
|
+
this.error('No changes provided');
|
|
184
|
+
}
|
|
185
|
+
return this.ensureUpdatePayload(payload);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { SelectOptions } from '@clack/prompts';
|
|
2
|
+
import { BaseCommand } from '../base.js';
|
|
3
|
+
export default class WorkflowsExtendLease extends BaseCommand<typeof WorkflowsExtendLease> {
|
|
4
|
+
static args: {
|
|
5
|
+
orderId: import("@oclif/core/interfaces").Arg<string, Record<string, unknown>>;
|
|
6
|
+
};
|
|
7
|
+
static description: string;
|
|
8
|
+
static examples: string[];
|
|
9
|
+
static flags: {
|
|
10
|
+
minutes: import("@oclif/core/interfaces").OptionFlag<number | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
11
|
+
sppi: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
12
|
+
yes: import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
13
|
+
};
|
|
14
|
+
init(): Promise<void>;
|
|
15
|
+
run(): Promise<void>;
|
|
16
|
+
protected selectPrompt<Value>(options: SelectOptions<Value>): Promise<Value>;
|
|
17
|
+
}
|