Skip to content

Commit

Permalink
Merge pull request #1376 from sasjs/job-command-improvements
Browse files Browse the repository at this point in the history
Job command improvements
  • Loading branch information
YuryShkoda committed Sep 15, 2023
2 parents 39adb7f + f45a2fc commit 580a6a3
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 171 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module.exports = {
statements: 73.51,
branches: 60.57,
functions: 73.56,
lines: 74.17
lines: 74.12
}
},

Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"access": "public"
},
"dependencies": {
"@sasjs/adapter": "4.10.0",
"@sasjs/adapter": "4.10.1",
"@sasjs/core": "4.46.3",
"@sasjs/lint": "2.3.1",
"@sasjs/utils": "3.4.0",
Expand Down
32 changes: 15 additions & 17 deletions src/commands/add/addCredential.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import path from 'path'
import { LogLevel } from '@sasjs/utils/logger'
import { ServerType, Target, HttpsAgentOptions } from '@sasjs/utils/types'

import SASjs from '@sasjs/adapter/node'
import { getNewAccessToken } from '../../utils/auth'
import { isSasJsServerInServerMode } from '../../utils'
import {
getNewAccessToken,
isSasJsServerInServerMode,
getSASjs
} from '../../utils'
import { createFile } from '@sasjs/utils'
import {
getAndValidateServerUrl,
Expand Down Expand Up @@ -54,8 +54,7 @@ export const addCredential = async (
const { access_token, refresh_token } = await getTokens(
target,
client,
secret,
httpsAgentOptions
secret
).catch((err) => {
throw err
})
Expand Down Expand Up @@ -108,8 +107,7 @@ export const addCredential = async (
const { access_token, refresh_token } = await getTokens(
target,
client,
'',
httpsAgentOptions
''
)

if (targetScope === TargetScope.Local) {
Expand Down Expand Up @@ -159,15 +157,9 @@ export const validateTargetName = (targetName: string): string => {
export const getTokens = async (
target: Target,
client: string,
secret: string,
httpsAgentOptions?: HttpsAgentOptions
secret: string
) => {
const adapter = new SASjs({
serverUrl: target.serverUrl,
serverType: target.serverType,
httpsAgentOptions,
debug: process.logger?.logLevel === LogLevel.Debug
})
const adapter = getSASjs(target)
const { access_token, refresh_token } = await getNewAccessToken(
adapter,
client,
Expand All @@ -189,7 +181,9 @@ export const createEnvFileForViya = async (
): Promise<void> => {
const envFileContent = `CLIENT=${client}\nSECRET=${secret}\nACCESS_TOKEN=${accessToken}\nREFRESH_TOKEN=${refreshToken}\n`
const envFilePath = path.join(process.projectDir, `.env.${targetName}`)

await createFile(envFilePath, envFileContent)

process.logger?.success(`Environment file saved at ${envFilePath}`)
}

Expand All @@ -200,7 +194,9 @@ export const createEnvFileForSas9 = async (
): Promise<void> => {
const envFileContent = `SAS_USERNAME=${userName}\nSAS_PASSWORD=${password}\n`
const envFilePath = path.join(process.projectDir, `.env.${targetName}`)

await createFile(envFilePath, envFileContent)

process.logger?.success(`Environment file saved at ${envFilePath}`)
}

Expand All @@ -212,6 +208,8 @@ export const createEnvFileForSasjs = async (
): Promise<void> => {
const envFileContent = `CLIENT=${client}\nACCESS_TOKEN=${accessToken}\nREFRESH_TOKEN=${refreshToken}\n`
const envFilePath = path.join(process.projectDir, `.env.${targetName}`)

await createFile(envFilePath, envFileContent)

process.logger?.success(`Environment file saved at ${envFilePath}`)
}
11 changes: 2 additions & 9 deletions src/commands/add/internal/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import {
getUrl
} from '@sasjs/utils/input'
import { Target, TargetJson, ServerType } from '@sasjs/utils/types'
import { LogLevel } from '@sasjs/utils/logger'
import { encodeToBase64 } from '@sasjs/utils'
import path from 'path'
import dotenv from 'dotenv'
import SASjs from '@sasjs/adapter/node'
import { TargetScope } from '../../../types/targetScope'
import { CommonFields } from '../../../types/commonFields'
import { findTargetInConfiguration } from '../../../utils/config'
import { findTargetInConfiguration, getSASjs } from '../../../utils'

export async function getCommonFields(): Promise<CommonFields> {
const scope = await getAndValidateScope()
Expand Down Expand Up @@ -207,12 +205,7 @@ export async function getAndValidateSasViyaFields(
}
: target.httpsAgentOptions

const sasjs = new SASjs({
serverUrl,
serverType: ServerType.SasViya,
httpsAgentOptions,
debug: process.logger?.logLevel === LogLevel.Debug
})
const sasjs = getSASjs(target)
let contexts: any[] = []
if (scope === TargetScope.Local) {
dotenv.config({
Expand Down
89 changes: 27 additions & 62 deletions src/commands/context/contextCommand.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import SASjs from '@sasjs/adapter/node'
import { readFile, ServerType } from '@sasjs/utils'
import { CommandExample, ReturnCode } from '../../types/command'
import { TargetCommand } from '../../types/command/targetCommand'
import { getAuthConfig } from '../../utils'
import { getAuthConfig, getSASjs } from '../../utils'
import { create } from './create'
import { deleteContext } from './delete'
import { edit } from './edit'
Expand Down Expand Up @@ -145,12 +144,7 @@ export class ContextCommand extends TargetCommand {

async executeCreateContext() {
const { target } = await this.getTargetInfo()
const sasjs = new SASjs({
serverUrl: target.serverUrl,
httpsAgentOptions: target.httpsAgentOptions,
appLoc: target.appLoc,
serverType: target.serverType
})
const sasjs = getSASjs(target)
const config = await this.getConfig().catch((err) => {
process.logger?.error(
`Unable to create context. Error fetching context configuration from ${this.parsed.source}: `,
Expand Down Expand Up @@ -186,12 +180,7 @@ export class ContextCommand extends TargetCommand {

async executeDeleteContext() {
const { target } = await this.getTargetInfo()
const sasjs = new SASjs({
serverUrl: target.serverUrl,
httpsAgentOptions: target.httpsAgentOptions,
appLoc: target.appLoc,
serverType: target.serverType
})
const sasjs = getSASjs(target)

const authConfig = await getAuthConfig(target).catch((err) => {
process.logger?.error(
Expand Down Expand Up @@ -221,119 +210,95 @@ export class ContextCommand extends TargetCommand {

async executeEditContext() {
const { target } = await this.getTargetInfo()
const sasjs = new SASjs({
serverUrl: target.serverUrl,
httpsAgentOptions: target.httpsAgentOptions,
appLoc: target.appLoc,
serverType: target.serverType
})

const sasjs = getSASjs(target)
const config = await this.getConfig().catch((err) => {
process.logger?.error(
`Unable to edit context. Error fetching context configuration from ${this.parsed.source}: `,
err
)

return null
})
if (!config) {
return ReturnCode.InternalError
}

if (!config) return ReturnCode.InternalError

const authConfig = await getAuthConfig(target).catch((err) => {
process.logger?.error(
'Unable to edit context. Error fetching auth config: ',
err
)

return null
})
if (!authConfig) {
return ReturnCode.InternalError
}

if (!authConfig) return ReturnCode.InternalError

const returnCode = await edit(
this.parsed.contextName as string,
config,
sasjs,
authConfig.access_token
)
.then(() => {
return ReturnCode.Success
})
.catch(() => {
return ReturnCode.InternalError
})
.then(() => ReturnCode.Success)
.catch(() => ReturnCode.InternalError)

return returnCode
}

async executeExportContext() {
const { target } = await this.getTargetInfo()
const sasjs = new SASjs({
serverUrl: target.serverUrl,
httpsAgentOptions: target.httpsAgentOptions,
appLoc: target.appLoc,
serverType: target.serverType
})
const sasjs = getSASjs(target)

const authConfig = await getAuthConfig(target).catch((err) => {
process.logger?.error(
'Unable to create context. Error fetching auth config: ',
err
)

return null
})
if (!authConfig) {
return ReturnCode.InternalError
}

if (!authConfig) return ReturnCode.InternalError

const returnCode = await exportContext(
this.parsed.contextName as string,
sasjs,
authConfig.access_token
)
.then(() => {
return ReturnCode.Success
})
.catch(() => {
return ReturnCode.InternalError
})
.then(() => ReturnCode.Success)
.catch(() => ReturnCode.InternalError)

return returnCode
}

async executeListContext() {
const { target } = await this.getTargetInfo()

if (target.serverType !== ServerType.SasViya) {
process.logger?.error(
`'context list' command is only supported for SAS Viya build targets.\nPlease try again with a different target.`
)

return ReturnCode.InternalError
}

const sasjs = new SASjs({
serverUrl: target.serverUrl,
httpsAgentOptions: target.httpsAgentOptions,
appLoc: target.appLoc,
serverType: target.serverType
})
const sasjs = getSASjs(target)

const authConfig = await getAuthConfig(target).catch((err) => {
process.logger?.error(
'Unable to create context. Error fetching auth config: ',
err
)

return null
})
if (!authConfig) {
return ReturnCode.InternalError
}

if (!authConfig) return ReturnCode.InternalError

const returnCode = await list(target, sasjs, authConfig)
.then(() => {
return ReturnCode.Success
})
.catch(() => {
return ReturnCode.InternalError
})
.then(() => ReturnCode.Success)
.catch(() => ReturnCode.InternalError)

return returnCode
}
Expand Down
10 changes: 2 additions & 8 deletions src/commands/flow/flowCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AuthConfig, Target } from '@sasjs/utils'
import SASjs from '@sasjs/adapter/node'
import { CommandExample, ReturnCode } from '../../types/command'
import { TargetCommand } from '../../types/command/targetCommand'
import { displayError, getAuthConfig } from '../../utils'
import { displayError, getAuthConfig, getSASjs } from '../../utils'
import { execute } from './execute'

enum FlowSubCommand {
Expand Down Expand Up @@ -67,13 +67,7 @@ export class FlowCommand extends TargetCommand {

public async execute() {
const { target } = await this.getTargetInfo()

const sasjs = new SASjs({
serverUrl: target.serverUrl,
httpsAgentOptions: target.httpsAgentOptions,
appLoc: target.appLoc,
serverType: target.serverType
})
const sasjs = getSASjs(target)

const authConfig = await getAuthConfig(target as Target).catch((err) => {
displayError(err, 'Error while getting access token.')
Expand Down
Loading

0 comments on commit 580a6a3

Please sign in to comment.