2020-07-28 12:40:47 +00:00
|
|
|
/**
|
|
|
|
* @license
|
2021-04-13 21:44:58 +00:00
|
|
|
* Copyright 2016-2021 Balena Ltd.
|
2020-07-28 12:40:47 +00:00
|
|
|
*
|
|
|
|
* Licensed 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 CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { flags } from '@oclif/command';
|
|
|
|
import Command from '../../command';
|
|
|
|
import * as cf from '../../utils/common-flags';
|
|
|
|
import { getVisuals, stripIndent } from '../../utils/lazy';
|
|
|
|
|
|
|
|
interface FlagsDef {
|
|
|
|
type: string;
|
|
|
|
drive?: string;
|
|
|
|
help: void;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ArgsDef {
|
|
|
|
key: string;
|
|
|
|
value: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class ConfigWriteCmd extends Command {
|
|
|
|
public static description = stripIndent`
|
|
|
|
Write a key-value pair to configuration of a device or OS image.
|
|
|
|
|
|
|
|
Write a key-value pair to the config.json file on the mounted filesystem,
|
|
|
|
e.g. the SD card of a provisioned device or balenaOS image.
|
|
|
|
`;
|
|
|
|
|
|
|
|
public static examples = [
|
|
|
|
'$ balena config write --type raspberrypi3 username johndoe',
|
|
|
|
'$ balena config write --type raspberrypi3 --drive /dev/disk2 username johndoe',
|
|
|
|
'$ balena config write --type raspberrypi3 files.network/settings "..."',
|
|
|
|
];
|
|
|
|
|
|
|
|
public static args = [
|
|
|
|
{
|
|
|
|
name: 'key',
|
|
|
|
description: 'the key of the config parameter to write',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'value',
|
|
|
|
description: 'the value of the config parameter to write',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
public static usage = 'config write <key> <value>';
|
|
|
|
|
|
|
|
public static flags: flags.Input<FlagsDef> = {
|
2021-04-13 21:44:58 +00:00
|
|
|
type: cf.deviceType,
|
|
|
|
drive: cf.driveOrImg,
|
2020-07-28 12:40:47 +00:00
|
|
|
help: cf.help,
|
|
|
|
};
|
|
|
|
|
|
|
|
public static authenticated = true;
|
|
|
|
|
|
|
|
public static root = true;
|
|
|
|
|
|
|
|
public async run() {
|
|
|
|
const { args: params, flags: options } = this.parse<FlagsDef, ArgsDef>(
|
|
|
|
ConfigWriteCmd,
|
|
|
|
);
|
|
|
|
|
2021-04-13 21:44:58 +00:00
|
|
|
const { safeUmount } = await import('../../utils/helpers');
|
2020-07-28 12:40:47 +00:00
|
|
|
|
|
|
|
const drive =
|
|
|
|
options.drive || (await getVisuals().drive('Select the device drive'));
|
2021-04-13 21:44:58 +00:00
|
|
|
await safeUmount(drive);
|
2020-07-28 12:40:47 +00:00
|
|
|
|
|
|
|
const config = await import('balena-config-json');
|
|
|
|
const configJSON = await config.read(drive, options.type);
|
|
|
|
|
|
|
|
console.info(`Setting ${params.key} to ${params.value}`);
|
2021-07-08 23:23:46 +00:00
|
|
|
ConfigWriteCmd.updateConfigJson(configJSON, params.key, params.value);
|
2020-07-28 12:40:47 +00:00
|
|
|
|
2021-04-13 21:44:58 +00:00
|
|
|
await safeUmount(drive);
|
2020-07-28 12:40:47 +00:00
|
|
|
|
|
|
|
await config.write(drive, options.type, configJSON);
|
|
|
|
|
|
|
|
console.info('Done');
|
|
|
|
}
|
2021-07-08 23:23:46 +00:00
|
|
|
|
|
|
|
/** Call Lodash's _.setWith(). Moved here for ease of testing. */
|
|
|
|
static updateConfigJson(configJSON: object, key: string, value: string) {
|
|
|
|
const _ = require('lodash') as typeof import('lodash');
|
|
|
|
// note: _.setWith() is needed instead of _.set() because, given a key
|
|
|
|
// like `os.udevRules.101`, _.set() creates a udevRules array (rather
|
|
|
|
// than a dictionary) and sets the 101st array element to value, while
|
|
|
|
// we actually want udevRules to be dictionary like { '101': value }
|
|
|
|
_.setWith(configJSON, key, value, (v) => v || {});
|
|
|
|
}
|
2020-07-28 12:40:47 +00:00
|
|
|
}
|