Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Do not allow to be active together the stable and beta repositories. #2935

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
27 changes: 19 additions & 8 deletions packages/cli/src/lib/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,12 +395,12 @@ function initYargs(): ReturnType<typeof yargs> {
.command('all', 'Show entire config')
.command('<adapter>[.<instance>]', 'Status of a specified adapter instance');
})
.command('repo [<name>]', 'Show repo information', yargs => {
.command('repo [<name/index>]', 'Show repo information', yargs => {
yargs
.command('set <name>', 'Set active repository')
.command('del <name>', 'Remove repository')
.command('add <name> <url>', 'Add repository')
.command('addset <name> <url>', 'Add repository and set it as active one')
.command('set <name/index>', 'Set active repository')
.command('del <name/index>', 'Remove repository')
.command('add <name/index> <url>', 'Add repository')
.command('addset <name/index> <url>', 'Add repository and set it as active one')
.command('show', 'List repositories');
})
.command(['uuid', 'id'], 'Show uuid of the installation', {})
Expand Down Expand Up @@ -2514,9 +2514,9 @@ async function processCommand(
}

case 'repo': {
let repoUrlOrCommand = args[0]; // Repo url or name or "add" / "del" / "set" / "show" / "addset" / "unset"
const repoName = args[1]; // Repo url or name
let repoUrl = args[2]; // Repo url or name
let repoUrlOrCommand: string | undefined = args[0]; // Repo url or name or "add" / "del" / "set" / "show" / "addset" / "unset"
let repoName: string | undefined = args[1]; // Repo url or name
let repoUrl: string | undefined = args[2]; // Repo url or name
if (
repoUrlOrCommand !== 'add' &&
repoUrlOrCommand !== 'del' &&
Expand Down Expand Up @@ -2555,6 +2555,17 @@ async function processCommand(
console.error(`Invalid repository name: "${repoName}"`);
return void callback();
}

// If repository set as number in the list
if (repoName.match(/\d+/)) {
GermanBluefox marked this conversation as resolved.
Show resolved Hide resolved
const obj = await objects.getObject('system.repositories');
if (obj?.native?.repositories) {
if (Object.keys(obj.native.repositories)[parseInt(repoName, 10)]) {
GermanBluefox marked this conversation as resolved.
Show resolved Hide resolved
repoName = Object.keys(obj.native.repositories)[parseInt(repoName, 10)];
}
}
}

if (repoUrlOrCommand === 'add' || repoUrlOrCommand === 'addset') {
if (!repoUrl) {
console.warn(
Expand Down
94 changes: 71 additions & 23 deletions packages/cli/src/lib/setup/setupRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ export class Repo {
*
* @param repoName name of the repository
* @param force force update even if same hash
* @param systemConfig content of system.config object
* @param systemRepos content of system.repositories object
* @param systemConfig content of `system.config` object
* @param systemRepos content of `system.repositories` object
*/
private async updateRepo(
repoName: string,
Expand Down Expand Up @@ -231,7 +231,7 @@ export class Repo {
/**
* Show the repo result on CLI
*
* @param sources Repo json sources
* @param sources Repo JSON sources
* @param flags CLI flags
*/
private async showRepoResult(sources: Record<string, any>, flags: RepoFlags): Promise<void> {
Expand All @@ -256,7 +256,7 @@ export class Repo {
if (installed[name]?.version) {
text += `, installed ${installed[name].version}`;
try {
// tools.upToDate can throw if version is invalid
// tools.upToDate can throw if a version is invalid
if (
sources[name].version !== installed[name].version &&
sources[name].version &&
Expand Down Expand Up @@ -295,12 +295,12 @@ export class Repo {
for (const name of Object.keys(sources)) {
if (installed[name] && installed[name].version && sources[name].version) {
try {
// tools.upToDate can throw if version is invalid
// tools.upToDate can throw if a version is invalid
if (
sources[name].version !== installed[name].version &&
!tools.upToDate(sources[name].version, installed[name].version)
) {
// remove first part of the name
// remove the first part of the name
const n = name.indexOf('.');
list.push(n === -1 ? name : name.substring(n + 1));
}
Expand All @@ -327,7 +327,7 @@ export class Repo {
}

/**
* Show current status of Repo on CLI
* Show the current status of Repo on CLI
*/
async showRepoStatus(): Promise<number> {
try {
Expand Down Expand Up @@ -373,19 +373,25 @@ export class Repo {
* @param repoUrl url of new repo
*/
async add(repoName: string, repoUrl: string): Promise<void> {
const sysRepoObj = await this.objects.getObjectAsync('system.repositories');
const sysRepoObj = await this.objects.getObject('system.repositories');
const obj = sysRepoObj || this.defaultSystemRepo;

if (obj.native.repositories[repoName]) {
throw new Error(`Repository "${repoName}" yet exists: ${obj.native.repositories[repoName].link}`);
} else {
if (repoName && repoName.length < 3 && repoName.match(/\d+/)) {
GermanBluefox marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(
`Name "${repoName}" of repository cannot contain only numbers as it could be confused with an index`,
);
}

obj.native.repositories[repoName] = {
link: repoUrl,
json: null,
};
obj.from = `system.host.${tools.getHostName()}.cli`;
obj.ts = Date.now();
await this.objects.setObjectAsync('system.repositories', obj);
await this.objects.setObject('system.repositories', obj);
}
}

Expand All @@ -395,7 +401,7 @@ export class Repo {
* @param repoName name of repository to remove
*/
async del(repoName: string): Promise<void> {
const obj = await this.objects.getObjectAsync('system.config');
const obj = await this.objects.getObject('system.config');
if (
(obj?.common.activeRepo &&
typeof obj.common.activeRepo === 'string' &&
Expand All @@ -404,7 +410,7 @@ export class Repo {
) {
throw new Error(`Cannot delete active repository: ${repoName}`);
} else {
const repoObj = await this.objects.getObjectAsync('system.repositories');
const repoObj = await this.objects.getObject('system.repositories');
if (repoObj) {
if (!repoObj.native.repositories[repoName]) {
throw new Error(`Repository "${repoName}" not found.`);
Expand All @@ -421,25 +427,46 @@ export class Repo {
/**
* Set specific repo as active one
*
* @param repoName name of the respository to activate
* @param repoName name of the repository to activate
*/
async setActive(repoName: string): Promise<void> {
const sysRepoObj = await this.objects.getObjectAsync('system.repositories');
const sysRepoObj = await this.objects.getObject('system.repositories');
const obj = sysRepoObj || this.defaultSystemRepo;

if (!obj.native.repositories[repoName]) {
throw new Error(`Repository "${repoName}" not found.`);
} else {
const confObj = await this.objects.getObjectAsync('system.config');
const confObj = await this.objects.getObject('system.config');
if (typeof confObj?.common.activeRepo === 'string') {
confObj.common.activeRepo = [confObj.common.activeRepo];
}

if (confObj && !confObj.common.activeRepo.includes(repoName)) {
confObj.common.activeRepo.push(repoName);

// Check that stable and beta repositories cannot be active at the same time.
if (repoName.toLowerCase().includes('beta')) {
GermanBluefox marked this conversation as resolved.
Show resolved Hide resolved
// Stable repository could be "stable", "Stable (default)"
const pos = confObj.common.activeRepo.findIndex(item => item.toLowerCase().includes('stable'));
if (pos !== -1) {
confObj.common.activeRepo.splice(pos, 1);
}
} else if (repoName.toLowerCase().includes('stable')) {
// Beta repository could be "beta" or "Beta (latest)"
const pos = confObj.common.activeRepo.findIndex(item => item.toLowerCase().includes('beta'));
if (pos !== -1) {
confObj.common.activeRepo.splice(pos, 1);
}
}
if (confObj.common.activeRepo.length > 1) {
console.warn(
'More than one repository is active. Please be sure, that this is a desired constellation',
);
}

confObj.from = `system.host.${tools.getHostName()}.cli`;
confObj.ts = Date.now();
await this.objects.setObjectAsync('system.config', confObj);
await this.objects.setObject('system.config', confObj);
}
}
}
Expand All @@ -450,7 +477,7 @@ export class Repo {
* @param repoName name of the repository
*/
async setInactive(repoName: string): Promise<void> {
const confObj = (await this.objects.getObjectAsync('system.config'))!;
const confObj = (await this.objects.getObject('system.config'))!;
if (typeof confObj?.common.activeRepo === 'string') {
confObj.common.activeRepo = [confObj.common.activeRepo];
}
Expand All @@ -460,12 +487,27 @@ export class Repo {
confObj.common.activeRepo.splice(pos, 1);
confObj.from = `system.host.${tools.getHostName()}.cli`;
confObj.ts = Date.now();
await this.objects.setObjectAsync('system.config', confObj);
// If no one repository is active set stable as active
if (!confObj.common.activeRepo.length) {
const sysRepoObj = await this.objects.getObject('system.repositories');
const obj = sysRepoObj || this.defaultSystemRepo;
// find a stable repository
const stableName = Object.keys(obj?.native?.repositories).find(name => name.includes('stable'));
if (stableName) {
confObj.common.activeRepo.push(stableName);
}
if (!confObj.common.activeRepo.length) {
console.warn(
'Actually no one repository is active! Update of adapter will not be possible. Please define some repository as active',
);
}
}
await this.objects.setObject('system.config', confObj);
}
}

/**
* Renames existing repository if old name and link matches, renaming will not be performed if an repo with the new name already exists
* Renames existing repository if old name and link matches, renaming will not be performed if a repo with the new name already exists
*
* @param oldName - name of the current repository
* @param newName - target name
Expand All @@ -475,13 +517,19 @@ export class Repo {
let repoObj;
let sysConfigObj;
try {
sysConfigObj = await this.objects.getObjectAsync('system.config');
repoObj = await this.objects.getObjectAsync('system.repositories');
sysConfigObj = await this.objects.getObject('system.config');
repoObj = await this.objects.getObject('system.repositories');
} catch (err) {
throw new Error(`Could not rename repository "${oldName}" to "${newName}": ${err.message}`);
}

if (repoObj && repoObj.native && repoObj.native.repositories) {
if (newName && newName.length < 3 && newName.match(/\d+/)) {
throw new Error(
`Name "${newName}" of repository cannot contain only numbers as it could be confused with an index`,
);
}

if (repoObj?.native?.repositories) {
if (
repoObj.native.repositories[oldName] &&
repoObj.native.repositories[oldName].link === repoUrl &&
Expand All @@ -491,7 +539,7 @@ export class Repo {
delete repoObj.native.repositories[oldName];

try {
await this.objects.setObjectAsync('system.repositories', repoObj);
await this.objects.setObject('system.repositories', repoObj);
console.log(`Renamed repository "${oldName} to "${newName}"`);
} catch (err) {
throw new Error(`Could not rename repository "${oldName}" to "${newName}": ${err.message}`);
Expand All @@ -513,7 +561,7 @@ export class Repo {
sysConfigObj.common.activeRepo.splice(pos, 1, newName);

try {
await this.objects.setObjectAsync('system.config', sysConfigObj);
await this.objects.setObject('system.config', sysConfigObj);
} catch (err) {
throw new Error(`Could not set "${newName}" as active repository: ${err.message}`);
}
Expand Down
Loading