Skip to content

Commit

Permalink
chore: fix tokens creation (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
manekinekko authored Feb 7, 2020
1 parent 928bf53 commit 69aa9ac
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
12 changes: 10 additions & 2 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,18 @@ export async function runCmd(command: string, loadingMessage?: string, options?:
}

////////
export async function az<T>(command: string, loadingMessage?: string, output = `json`) {
export async function az<T>(command: string, loadingMessage?: string, output: "json" | "tsv" = `json`) {
command = `${command} --output ${output} ` + (IS_DEBUG ? "--verbose" : "");
const message: string = await runCmd(`az ${command}`, loadingMessage, {
let message: string = await runCmd(`az ${command}`, loadingMessage, {
silent: !IS_DEBUG
});

// guess if the output response is a JSON format
if (message.startsWith("{") || message.startsWith("[")) {
return JSON.parse(message || "{}") as T;
} else {
// for other output formats, ie. TSV
message = message.trim();
return ({ message } as unknown) as T;
}
}
Expand Down Expand Up @@ -212,6 +215,11 @@ export function readFileFromDisk(filePath: string) {
export function saveWorkspace(config: Partial<HexaWorkspace>) {
debug(`updating workspace with ${chalk.green(JSON.stringify(config))}`);

if (!config || Object.keys(config).length === 0) {
debug(`skipping saving workspace because config option is empty!`);
return false;
}

// we don't want to store IDs in the workspace file
for (var key in config) {
delete (config as any)[key].id;
Expand Down
5 changes: 1 addition & 4 deletions src/features/storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,5 @@ module.exports = async function() {
storage
});

if (process.env.HEXA_STORAGE_GENERATE_TOKEN) {
return (await require("./tokens"))();
}
return true;
return await (require("./tokens"))();
};
27 changes: 12 additions & 15 deletions src/features/storage/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,27 @@ module.exports = async function() {
if (process.env.HEXA_STORAGE_USE_SAS) {
// https://docs.microsoft.com/en-us/cli/azure/storage/account?view=azure-cli-latest#az-storage-account-generate-sas
const expiryYear = new Date().getFullYear() + 2;
let sas = await az<string>(
let sas = await az<HexaMessageResponse>(
`storage account generate-sas --account-key 00000000 --account-name "${storage.name}" --expiry ${expiryYear}-01-01 --https-only --permissions acuw --resource-types sco --services b`,
`Creating a SAS token...`
`Creating a SAS token...`,
`tsv`
);
saveEnvFile("AZURE_STORAGE_SAS", sas);
debug(`saved SAS key to ${chalk.green(".env")}`);
debug(`got SAS key ${chalk.green(JSON.stringify(sas))}`);

Config.set("storage", {
...storage,
sas
});
saveEnvFile("AZURE_STORAGE_SAS", sas.message);
debug(`saved AZURE_STORAGE_SAS key to ${chalk.green(".env")}`);
} else {
// https://docs.microsoft.com/en-us/cli/azure/storage/account?view=azure-cli-latest#az-storage-account-show-connection-string
let connectionString = await az<{ message: string }>(
let connectionString = await az<HexaMessageResponse>(
`storage account show-connection-string --name "${storage.name}" --resource-group "${project.name}" --subscription "${subscription.name}" --query "connectionString"`,
`Fetching a connection string for storage account ${chalk.cyan(storage.name)}...`,
`tsv`
);
saveEnvFile("AZURE_STORAGE_CONNECTION_STRING", connectionString.message);
debug(`saved ConnectionString key to ${chalk.green(".env")}`);
debug(`got ConnectionString key ${chalk.green(JSON.stringify(connectionString))}`);

Config.set("storage", {
...storage,
connectionString: connectionString.message
});
saveEnvFile("AZURE_STORAGE_CONNECTION_STRING", connectionString.message);
debug(`saved AZURE_STORAGE_CONNECTION_STRING key to ${chalk.green(".env")}`);
}

return true;
};
3 changes: 3 additions & 0 deletions src/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ declare interface ProcessEnv {
declare interface HexaInitOptions {
requestedServices?: string[];
}
declare interface HexaMessageResponse {
message: string;
}

declare interface HexaWorkspace {
storage: AzureStorage;
Expand Down

0 comments on commit 69aa9ac

Please sign in to comment.