-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: servicepack deploy command (#133)
* feat: servicepack deploy command * docs: update help * chore: namme fixes * chore: refactored command param getters, added tests for command-utils * chore: fixes * chore(*): fix formatting, throw error with nonexistent arguments, fix condition for showing example Co-authored-by: Krishna Acondy <[email protected]>
- Loading branch information
1 parent
58c390b
commit e24ef20
Showing
7 changed files
with
400 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import path from 'path' | ||
import SASjs from '@sasjs/adapter/node' | ||
import chalk from 'chalk' | ||
import { | ||
readFile, | ||
folderExists, | ||
createFile, | ||
createFolder | ||
} from '../utils/file-utils' | ||
import { displayResult } from '../utils/displayResult' | ||
import { | ||
getAccessToken, | ||
findTargetInConfiguration | ||
} from '../utils/config-utils' | ||
|
||
export async function servicePackDeploy( | ||
jsonFilePath = null, | ||
targetName = null, | ||
isForced = false | ||
) { | ||
console.log({ | ||
jsonFilePath, | ||
targetName, | ||
isForced | ||
}) | ||
|
||
if (path.extname(jsonFilePath) !== '.json') { | ||
throw new Error('Provided data file must be valid json.') | ||
} | ||
|
||
const { target, isLocal } = await findTargetInConfiguration(targetName, true) | ||
|
||
if (!target.serverType === 'SASVIYA') { | ||
console.log( | ||
chalk.redBright.bold( | ||
`Deployment failed. This commmand is only available on VIYA servers.` | ||
) | ||
) | ||
|
||
return | ||
} | ||
|
||
console.log( | ||
chalk.cyanBright(`Executing deployServicePack to update SAS server.`) | ||
) | ||
|
||
let output = await deployToSasViyaWithServicePack( | ||
jsonFilePath, | ||
target, | ||
isForced | ||
) | ||
|
||
let outputPath = path.join(process.cwd(), isLocal ? '/sasjsbuild' : '') | ||
|
||
if (!(await folderExists(outputPath))) { | ||
await createFolder(outputPath) | ||
} | ||
|
||
outputPath += '/output.json' | ||
|
||
await createFile(outputPath, output) | ||
|
||
displayResult( | ||
null, | ||
null, | ||
`Request finished. Output is stored at '${outputPath}'` | ||
) | ||
} | ||
|
||
async function deployToSasViyaWithServicePack( | ||
jsonFilePath, | ||
buildTarget, | ||
isForced | ||
) { | ||
const sasjs = new SASjs({ | ||
serverUrl: buildTarget.serverUrl, | ||
appLoc: buildTarget.appLoc, | ||
serverType: buildTarget.serverType | ||
}) | ||
|
||
const CONSTANTS = require('../constants') | ||
const buildDestinationFolder = CONSTANTS.buildDestinationFolder | ||
|
||
const finalFilePathJSON = path.join( | ||
buildDestinationFolder, | ||
`${buildTarget.name}.json` | ||
) | ||
|
||
let jsonContent | ||
|
||
if (jsonFilePath) { | ||
jsonContent = await readFile(path.join(process.cwd(), jsonFilePath)) | ||
} else { | ||
jsonContent = await readFile(finalFilePathJSON) | ||
} | ||
|
||
let jsonObject | ||
|
||
try { | ||
jsonObject = JSON.parse(jsonContent) | ||
} catch (err) { | ||
throw new Error('Provided data file must be valid json.') | ||
} | ||
|
||
const access_token = await getAccessToken(buildTarget) | ||
|
||
if (!access_token) { | ||
console.log( | ||
chalk.redBright.bold( | ||
`Deployment failed. Request is not authenticated.\nRun 'sasjs add' command and provide 'client' and 'secret'.` | ||
) | ||
) | ||
} | ||
|
||
return await sasjs.deployServicePack( | ||
jsonObject, | ||
null, | ||
null, | ||
access_token, | ||
isForced | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { servicePackDeploy } from './deploy' | ||
import { | ||
getCommandParameter, | ||
getCommandParameterLastMultiWord, | ||
isFlagPresent | ||
} from '../utils/command-utils' | ||
|
||
import chalk from 'chalk' | ||
|
||
export async function processServicepack(commandLine) { | ||
const command = commandLine[1] | ||
const commands = { | ||
deploy: 'deploy' | ||
} | ||
|
||
if (!commands.hasOwnProperty(command)) { | ||
console.log( | ||
chalk.redBright( | ||
`Not supported servicepack command. Supported commands are:\n${Object.keys( | ||
commands | ||
).join('\n')}` | ||
) | ||
) | ||
|
||
return | ||
} | ||
|
||
const commandExample = | ||
'sasjs servicepack <command> --source ../viyadeploy.json --target targetName' | ||
|
||
switch (command) { | ||
case commands.deploy: | ||
let targetName = getCommandParameterLastMultiWord('-t', '--target', commandLine, commandExample) | ||
let jsonFilePath = getCommandParameter('-s', '--source', commandLine, commandExample) | ||
let isForced = isFlagPresent('-f', commandLine) | ||
|
||
servicePackDeploy(jsonFilePath, targetName, isForced) | ||
break | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import chalk from 'chalk' | ||
|
||
export function isFlagPresent(flag, commandLine) { | ||
return commandLine.indexOf(flag) > -1 | ||
} | ||
|
||
export function getCommandParameter( | ||
commandFlag, | ||
commandFlagLong, | ||
commandLine, | ||
commandExample = '' | ||
) { | ||
let parameterValueFlagIndex = commandLine.indexOf(commandFlagLong) | ||
|
||
if (parameterValueFlagIndex === -1) | ||
parameterValueFlagIndex = commandLine.indexOf(commandFlag) | ||
|
||
if (parameterValueFlagIndex === -1) { | ||
const errorMessage = chalk.redBright( | ||
`'${commandFlag || commandFlagLong}' flag is missing. ${ | ||
commandExample ? "(eg '" + commandExample + "')" : '' | ||
}` | ||
) | ||
throw new Error(errorMessage) | ||
|
||
return | ||
} | ||
|
||
let parameterValue = commandLine[parameterValueFlagIndex + 1] | ||
|
||
return parameterValue | ||
} | ||
|
||
export function getCommandParameterLastMultiWord( | ||
commandFlag, | ||
commandFlagLong, | ||
commandLine, | ||
commandExample = '' | ||
) { | ||
let parameterValue = [] | ||
let parameterFlagIndex = commandLine.indexOf(commandFlagLong) | ||
|
||
if (parameterFlagIndex === -1) | ||
parameterFlagIndex = commandLine.indexOf(commandFlag) | ||
|
||
if (parameterFlagIndex !== -1) { | ||
for (let i = parameterFlagIndex + 1; i < commandLine.length; i++) { | ||
if (commandLine[i].includes('-')) { | ||
const errorMessage = `Parameter '${ | ||
commandFlagLong || commandFlag | ||
}' has to be provided as the last argument ${ | ||
commandExample ? "(eg '" + commandExample + "')" : '' | ||
}` | ||
throw new Error(errorMessage) | ||
} | ||
|
||
parameterValue.push(commandLine[i]) | ||
} | ||
} | ||
|
||
parameterValue = parameterValue.join(' ') | ||
|
||
return parameterValue | ||
} |
Oops, something went wrong.