Skip to content

Commit

Permalink
Ensure interfaces are consistent and refine existing steps
Browse files Browse the repository at this point in the history
  • Loading branch information
keeramis committed Jan 24, 2025
1 parent ce60b83 commit 09115dd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 67 deletions.
3 changes: 2 additions & 1 deletion src/cmd/flash.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ module.exports = class FlashCommand extends CLICommandBase {
files = ['.'];
}

const input = files[0];
const [ input, ...rest ] = files;
const stats = await fs.stat(input);
let filesToProgram;

Expand All @@ -95,6 +95,7 @@ module.exports = class FlashCommand extends CLICommandBase {
const zipInfo = await this._extractFlashFilesFromZip(input);
includeDir = zipInfo.baseDir;
filesToProgram = zipInfo.filesToProgram.map((file) => path.join(includeDir, file));
filesToProgram.push(...rest);
} else {
filesToProgram = files;
}
Expand Down
102 changes: 36 additions & 66 deletions src/cmd/setup-tachyon.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,54 +54,21 @@ module.exports = class SetupTachyonCommands extends CLICommandBase {
try {
api.ensureToken();
} catch {
// User not logged in, prompt to login
const choice = await this._promptForLoginType();
const cloudCommand = new CloudCommand();
if (choice === 'token') {
const resp = await this.ui.prompt([
{
type: 'input',
name: 'token',
message: 'Enter your access token:',
},
]);
await cloudCommand.login({ token: resp.token });
} else {
await cloudCommand.login();
}
// If there was a problem logging in, this method throws an error
await cloudCommand.login();
}
}

async _promptForLoginType() {
const choicesMapping = {
'Access Token': 'token',
'Credentials': 'credentials',
// 'SSO': 'sso',
// 'OTP': 'otp',
};
const question = [
{
type: 'list',
name: 'login',
message: 'Login using:',
choices: Object.keys(choicesMapping),
},
];
const { login } = await this.ui.prompt(question);
return choicesMapping[login];
}

async _selectRegion() {
const regionMapping = {
'NA (North America)': 'na',
'ROW (Rest of the World)': 'row'
'NA (North America)': 'NA',
'RoW (Rest of the World)': 'RoW'
};
const question = [
{
type: 'list',
name: 'region',
message: 'Select a region:',
message: 'Select the region:',
choices: Object.keys(regionMapping),
},
];
Expand All @@ -115,7 +82,7 @@ module.exports = class SetupTachyonCommands extends CLICommandBase {
type: 'input',
name: 'version',
message: 'Enter the version number:',
default: 'default',
default: 'latest',
},
];
const answer = await this.ui.prompt(question);
Expand Down Expand Up @@ -204,10 +171,15 @@ module.exports = class SetupTachyonCommands extends CLICommandBase {
async _getSystemPassword() {
const question = [
{
type: 'input',
type: 'password',
name: 'password',
message: 'Password for the system account (required):',
default: 'default',
validate: (value) => {
if (!value) {
return 'You need a password to log in';
}
return true;
}
},
];
const answer = await this.ui.prompt(question);
Expand All @@ -220,12 +192,11 @@ module.exports = class SetupTachyonCommands extends CLICommandBase {
type: 'input',
name: 'setupWifi',
message: 'Would you like to set up WiFi for your device? (y/n):',
default: true,
default: 'y',
}
];
const { setupWifi } = await this.ui.prompt(question);
if (setupWifi === 'y') {
// TODO: Double check the WiFi credentials with the user
if (setupWifi.toLowerCase() === 'y') {
return this._getWifiCredentials();
}

Expand All @@ -246,59 +217,58 @@ module.exports = class SetupTachyonCommands extends CLICommandBase {
},
];
const res = await this.ui.prompt(questions);
const confirmQuestion = [
{
type: 'input',
name: 'confirm',
message: `Are these credentials correct? (y/n)\nSSID: ${res.ssid}\nPassword: ${res.password}`
}
];
const { confirm } = await this.ui.prompt(confirmQuestion);
if (confirm === 'n') {
return this._getWifiCredentials();
}

return { ssid: res.ssid, password: res.password };
}

async _getKeys() {
// TODO: Do you want to add an SSH key to allow login.
// If yes, prompt for the path to the key
const question = [
{
type: 'input',
name: 'sshKey',
message: 'Enter the path to your SSH public key:'
message: 'Enter the path to your SSH public key:',
validate: (value) => {
if (!fs.existsSync(value)) {
return 'You need to provide a path to your SSH public key';
}
return true;
}
},
];

const { sshKey } = await this.ui.prompt(question);
if (!fs.existsSync(sshKey)) {
return this._getKeys();
}

return fs.readFileSync(sshKey, 'utf8');
}

async _getRegistrationCode(product) {
return this.api.getRegistrationCode(product);
const data = await this.api.getRegistrationCode(product);
return data.registration_code;
}

async _createConfigBlob(regCode, systemPassword, wifiSsid, wifiPassword, sshKey) {
// Format the config and registration code into a config blob (JSON file, prefixed by the file size)
const config = {
registration_code: regCode,
system_password : _generateShadowCompatibleHash(systemPassword),
wifi: {
};

if (wifiSsid) {
config.wifi = {
ssid: wifiSsid,
password: wifiPassword,
},
ssh_key : sshKey,
};
};
}

if (sshKey) {
config.ssh_key = sshKey;
}

// Write config JSON to a temporary file (generate a filename with the temp npm module)
// prefixed by the JSON string length as a 32 bit integer
let jsonString = JSON.stringify(config);
const buffer = Buffer.alloc(4 + Buffer.byteLength(jsonString));
buffer.writeInt32LE(Buffer.byteLength(jsonString), 0);
buffer.writeUInt32BE(Buffer.byteLength(jsonString), 0);
buffer.write(jsonString, 4);

const tempFile = temp.openSync();
Expand Down

0 comments on commit 09115dd

Please sign in to comment.