Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@adobe/aio-lib-core-logging": "^2.0.0",
"@adobe/aio-lib-core-networking": "^4.1.0",
"@adobe/aio-lib-env": "^2.0.0",
"@adobe/aio-lib-files": "^3.0.1",
"@adobe/aio-lib-ims": "^6.0.0",
"@adobe/aio-lib-runtime": "^5.0.0",
"@adobe/aio-lib-templates": "^2.2.0",
Expand Down
103 changes: 103 additions & 0 deletions src/commands/app/add/files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Copyright 2023 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

const BaseCommand = require('../../../BaseCommand')

const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-cli-plugin-app:list:extensions', { provider: 'debug' })
const { Flags } = require('@oclif/core')
const fs = require('fs')
const path = require('path')
const filesLib = require('@adobe/aio-lib-files')


const chalk = require('chalk')
const yaml = require('js-yaml')

/*
This is a quick poc intended to add files to remote file storage, and display results in json, yaml or as a list.
Possible future enhancements:
- recursive add

*/

class AddFilesCommand extends BaseCommand {
async run() {
const { flags, args } = await this.parse(AddFilesCommand)
aioLogger.debug(`Add files with flags: ${JSON.stringify(flags)}`)

let value = args['value|filename']
if (flags.file && !value) {
this.error('Missing filename')
}

const extConfig = this.getAppExtConfigs(flags)

const { namespace, auth } = Object.values(extConfig)[0]?.ow
if (namespace && auth) {
const files = await filesLib.init({ ow: { namespace, auth } })
if (flags.file) {
try {
const resolvedPath = path.resolve(value)
const fileContents = fs.readFileSync(resolvedPath, 'utf8')
const results = await files.write(args.path, fileContents)
this.log(`Added ${results.length} files in ${args.path}`)
if (results.length > 0) {
this.log(results.map(r => r.name).join('\n'))
}
} catch (e) {
this.error(`Cannot read file: ${e.message + value}`)
}
} else {
const results = await files.write(args.path, value)
this.log(`Added ${results.length} files in ${args.path}`)
if (results.length > 0) {
this.log(results.map(r => r.name).join('\n'))
}
}




} else {
this.log('Missing config values, or current working directory is not an App Builder app.')
}
}
}

AddFilesCommand.description = `Add files in storage
`
AddFilesCommand.flags = {
...BaseCommand.flags,
json: Flags.boolean({
description: 'Output json',
char: 'j'
}),
yml: Flags.boolean({
description: 'Output yml',
char: 'y'
}),
file: Flags.boolean({
char: 'f',
description: 'value is a path to a file'
})
}

AddFilesCommand.aliases = ['app:add:files', 'app:files:add', 'app:add:file']
AddFilesCommand.args = [{
name: 'path',
description: 'Remote filepath to add',
required: true
}, {
name: 'value|filename',
required: false
}]

module.exports = AddFilesCommand
72 changes: 72 additions & 0 deletions src/commands/app/delete/files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright 2023 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

const BaseCommand = require('../../../BaseCommand')

const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-cli-plugin-app:list:extensions', { provider: 'debug' })
const { Flags } = require('@oclif/core')
const filesLib = require('@adobe/aio-lib-files')


const chalk = require('chalk')
const yaml = require('js-yaml')

/*
This is a quick poc intended to delete files in remote storage, and display results in json, yaml or as a list.
Possible future enhancements:
- prompt before deleting 'Are you sure you want to delete these files?' (thanks co-pilot!)
*/

class DeleteFilesCommand extends BaseCommand {
async run() {
const { flags, args } = await this.parse(DeleteFilesCommand)
aioLogger.debug(`Delete files with flags: ${JSON.stringify(flags)}`)

const extConfig = this.getAppExtConfigs(flags)

const { namespace, auth } = Object.values(extConfig)[0]?.ow
if (namespace && auth) {
const files = await filesLib.init({ ow: { namespace, auth } })
const results = await files.delete(args.path)
this.log(`Deleted ${results.length} files in ${args.path}`)
if (results.length > 0) {
this.log(results.map(r => r.name).join('\n'))
}
} else {
this.log('Missing config values, or current working directory is not an App Builder app.')
}
}
}

DeleteFilesCommand.description = `Delete files in storage
`
DeleteFilesCommand.flags = {
...BaseCommand.flags,
json: Flags.boolean({
description: 'Output json',
char: 'j'
}),
yml: Flags.boolean({
description: 'Output yml',
char: 'y'
})
}

DeleteFilesCommand.aliases = ['app:delete:files', 'app:files:delete', 'app:delete:file', 'app:files:rm', 'app:files:remove']
DeleteFilesCommand.args = [
{
name: 'path',
description: 'Remote filepath to delete',
required: true
}
]

module.exports = DeleteFilesCommand
92 changes: 92 additions & 0 deletions src/commands/app/list/files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Copyright 2023 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

const BaseCommand = require('../../../BaseCommand')

const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-cli-plugin-app:list:extensions', { provider: 'debug' })
const { Flags } = require('@oclif/core')
const filesLib = require('@adobe/aio-lib-files')


const chalk = require('chalk')
const yaml = require('js-yaml')

/*
This is a quick poc intended to list files in remote storage, and display results in json, yaml or as a list.
Possible future enhancements:
- allow wildcards in path, e.g. `aio app list files /my/path/*.json`
- allow filtering by file type, e.g. `aio app list files --type=html`
- display file size in human readable format, e.g. 1.2MB
- allow sorting by name, size, date, etc.
- allow pagination ( thanks co-pilot you were on a roll!)
- display file permissions, modifiedDate, etc.
- display as tree, or table, remove args.path from file names
- display overall storage usage, number of private/public files, and overall size
- aio app add files <path> <file> to upload a file|folder
- aio app delete files <path> <file> to delete a file|folder
*/

class ListFilesCommand extends BaseCommand {
async run() {
const { flags, args } = await this.parse(ListFilesCommand)
aioLogger.debug(`list files with flags: ${JSON.stringify(flags)}`)

const extConfig = this.getAppExtConfigs(flags)

const {namespace, auth} = Object.values(extConfig)[0]?.ow
if (namespace && auth) {
const files = await filesLib.init({ ow: { namespace, auth } })
const filesList = await files.list(args.path)
// print
if (flags.json) {
this.log(JSON.stringify(filesList, 0, 2))
} else if (flags.yml) {
this.log(yaml.dump(filesList))
} else {
if (filesList.length > 0) {
this.log(chalk.bold(`Files: ${args.path}`))
filesList.forEach(fileInfo => {
this.log(`${fileInfo.name} \t${fileInfo.contentType} \t${chalk.dim(`(${fileInfo.contentLength} bytes)`)}`)
})
} else {
this.log('No files found')
}
}
} else {
this.log('Missing config values, or current working directory is not an App Builder app.')
}
}
}

ListFilesCommand.description = `List files in storage
`
ListFilesCommand.flags = {
...BaseCommand.flags,
json: Flags.boolean({
description: 'Output json',
char: 'j'
}),
yml: Flags.boolean({
description: 'Output yml',
char: 'y'
})
}

ListFilesCommand.aliases = ['app:ls:files', 'app:files:list', 'app:files:ls', 'app:files']
ListFilesCommand.args = [
{
name: 'path',
description: 'Remote path to list',
default: '/'
}
]

module.exports = ListFilesCommand