Skip to content

Commit

Permalink
headlamp-plugin: Add plugin mgmt commands
Browse files Browse the repository at this point in the history
This patch adds the plugin management commands
install,uninstall,update and list to the
headlamp-plugin cli.

Signed-off-by: yolossn <sannagaraj@microsoft.com>
  • Loading branch information
yolossn committed Jul 1, 2024
1 parent 637603c commit 84bf0ea
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 7 deletions.
5 changes: 5 additions & 0 deletions plugins/headlamp-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ headlamp-plugin --help
headlamp-plugin.js test [package] Test. <package> defaults to current
working directory. Can also be a
folder of packages.
headlamp-plugin.js list List installed plugins.
headlamp-plugin.js install [URL] Install a plugin from the specified
Artifact Hub URL.
headlamp-plugin.js update [pluginName] Update the plugin to the latest version.
headlamp-plugin.js uninstall [pluginName] Uninstall the plugin.
```

## Development notes
Expand Down
157 changes: 157 additions & 0 deletions plugins/headlamp-plugin/bin/headlamp-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const child_process = require('child_process');
const validate = require('validate-npm-package-name');
const yargs = require('yargs/yargs');
const headlampPluginPkg = require('../package.json');
const pluginManager = require('../plugin-management-utils');
const { table } = require('table');

/**
* Creates a new plugin folder.
Expand Down Expand Up @@ -1172,6 +1174,161 @@ yargs(process.argv.slice(2))
process.exitCode = test(argv.package);
}
)
.command(
'install [URL]',
'Install a plugin from the Artiface Hub URL',
yargs => {
yargs
.positional('URL', {
describe: 'URL of the plugin to install',
type: 'string',
})
.option('folderName', {
describe: 'Name of the folder to install the plugin into',
type: 'string',
})
.option('headlampVersion', {
describe: 'Version of headlamp to install the plugin into',
type: 'string',
})
.option('quiet', {
alias: 'q',
describe: 'Do not print logs',
type: 'boolean',
});
},
async argv => {
const { URL, folderName, headlampVersion, quiet } = argv;
const progressCallback = quiet
? null
: data => {
console.log(data.type, ':', data.message);
}; // Use console.log for logs if not in quiet mode
try {
await pluginManager.install(URL, folderName, headlampVersion, progressCallback);
} catch (e) {
console.error(e.message);
process.exit(1); // Exit with error status
}
}
)
.command(
'update [pluginName]',
'Update a plugin to the latest version',
yargs => {
yargs
.positional('pluginName', {
describe: 'Name of the plugin to update',
type: 'string',
})
.positional('folderName', {
describe: 'Name of the folder that contains the plugin',
type: 'string',
})
.positional('headlampVersion', {
describe: 'Version of headlamp to update the plugin into',
type: 'string',
})
.option('quiet', {
alias: 'q',
describe: 'Do not print logs',
type: 'boolean',
});
},
async argv => {
const { pluginName, folderName, headlampVersion, quiet } = argv;
const progressCallback = quiet
? null
: data => {
console.log(data.type, ':', data.message);
}; // Use console.log for logs if not in quiet mode
try {
await pluginManager.update(pluginName, folderName, headlampVersion, progressCallback);
} catch (e) {
console.error(e.message);
process.exit(1); // Exit with error status
}
}
)
.command(
'uninstall [pluginName]',
'Uninstall a plugin',
yargs => {
yargs
.positional('pluginName', {
describe: 'Name of the plugin to uninstall',
type: 'string',
})
.option('folderName', {
describe: 'Name of the folder that contains the plugin',
type: 'string',
})
.option('quiet', {
alias: 'q',
describe: 'Do not print logs',
type: 'boolean',
});
},
async argv => {
const { pluginName, folderName, quiet } = argv;
const progressCallback = quiet
? null
: data => {
console.log(data.type, ':', data.message);
}; // Use console.log for logs if not in quiet mode
try {
await pluginManager.uninstall(pluginName, folderName, progressCallback);
} catch (e) {
console.error(e.message);
process.exit(1); // Exit with error status
}
}
)
.command(
'list',
'List installed plugins',
yargs => {
yargs
.option('folderName', {
describe: 'Name of the folder that contains the plugins',
type: 'string',
})
.option('json', {
alias: 'j',
describe: 'Output in JSON format',
type: 'boolean',
});
},
async argv => {
const { folderName, json } = argv;
const progressCallback = data => {
if (json) {
console.log(JSON.stringify(data.data));
} else {
// display table
const rows = [
['Name', 'Version', 'Folder Name', 'ArticaftHub URL', 'ArtifactHub Version'],
];
data.data.forEach(plugin => {
rows.push([
plugin.pluginName,
plugin.pluginVersion,
plugin.folderName,
plugin.artifacthubURL,
plugin.artifacthubVersion,
]);
});
console.log(table(rows));
}
};
try {
await pluginManager.list(folderName, progressCallback);
} catch (e) {
console.error(e.message);
process.exit(1); // Exit with error status
}
}
)
.demandCommand(1, '')
.strict()
.help().argv;
13 changes: 7 additions & 6 deletions plugins/headlamp-plugin/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion plugins/headlamp-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@
"template",
"lib",
"types",
".storybook"
".storybook",
"plugin-management-utils.js"
],
"keywords": [
"headlamp",
Expand Down

0 comments on commit 84bf0ea

Please sign in to comment.