Skip to content
This repository has been archived by the owner on Jun 28, 2024. It is now read-only.

feat: one sdk log env #361

Merged
merged 4 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/commands/prepare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,16 @@ describe('prepare CLI command', () => {
jest.mocked(readFile).mockResolvedValueOnce(fileContent);
jest.mocked(prepareProviderJson).mockResolvedValueOnce(providerJson);

await expect(instance.execute({
logger,
userError,
flags: {},
args: { urlOrPath: 'path/to/FILE.docs.2!87.yaml' },
})).rejects.toThrow(`Provider name inferred from file name is not valid. Please provide provider name explicitly as second argument.`);
await expect(
instance.execute({
logger,
userError,
flags: {},
args: { urlOrPath: 'path/to/FILE.docs.2!87.yaml' },
})
).rejects.toThrow(
`Provider name inferred from file name is not valid. Please provide provider name explicitly as second argument.`
);
});

it('prepares provider json from url', async () => {
Expand Down
30 changes: 30 additions & 0 deletions src/logic/application-code/dotenv/dotenv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ const BEARER_AUTH: SecurityScheme = {
const TOKEN = 'sfs_b31314b7fc8...8ec1930e';

const EXISTING_DOTENV = `SUPERFACE_ONESDK_TOKEN=sfs_b31314b7fc8...8ec1930e

# Set to "on" to enable debug logging
ONESDK_LOG=off

# Deployment zone
# for AWS
MY_PROVIDER_PARAM_TWO=us-west-1
Expand All @@ -45,6 +49,9 @@ describe('createNewDotenv', () => {
content: `# The token for monitoring your Comlinks at https://superface.ai
SUPERFACE_ONESDK_TOKEN=sfs_b31314b7fc8...8ec1930e

# Set to "on" to enable debug logging
ONESDK_LOG=off

# Parameter description
MY_PROVIDER_PARAM_ONE=
MY_PROVIDER_TOKEN=
Expand All @@ -60,6 +67,9 @@ MY_PROVIDER_TOKEN=
expect(result).toStrictEqual({
content: `# Set your OneSDK token to monitor your usage out-of-the-box. Get yours at https://superface.ai
SUPERFACE_ONESDK_TOKEN=

# Set to "on" to enable debug logging
ONESDK_LOG=off
`,
newEmptyEnvVariables: ['SUPERFACE_ONESDK_TOKEN'],
});
Expand All @@ -74,6 +84,9 @@ SUPERFACE_ONESDK_TOKEN=
expect(result).toStrictEqual({
content: `# The token for monitoring your Comlinks at https://superface.ai
SUPERFACE_ONESDK_TOKEN=sfs_b31314b7fc8...8ec1930e

# Set to "on" to enable debug logging
ONESDK_LOG=off
`,
newEmptyEnvVariables: [],
});
Expand All @@ -90,6 +103,9 @@ SUPERFACE_ONESDK_TOKEN=sfs_b31314b7fc8...8ec1930e
expect(result).toStrictEqual({
content: `# The token for monitoring your Comlinks at https://superface.ai
SUPERFACE_ONESDK_TOKEN=sfs_b31314b7fc8...8ec1930e

# Set to "on" to enable debug logging
ONESDK_LOG=off
`,
newEmptyEnvVariables: [],
});
Expand All @@ -106,6 +122,9 @@ SUPERFACE_ONESDK_TOKEN=sfs_b31314b7fc8...8ec1930e
content: `# The token for monitoring your Comlinks at https://superface.ai
SUPERFACE_ONESDK_TOKEN=sfs_b31314b7fc8...8ec1930e

# Set to "on" to enable debug logging
ONESDK_LOG=off

# Parameter description
MY_PROVIDER_PARAM_ONE=

Expand All @@ -129,6 +148,9 @@ MY_PROVIDER_PARAM_TWO=us-west-1
content: `# The token for monitoring your Comlinks at https://superface.ai
SUPERFACE_ONESDK_TOKEN=sfs_b31314b7fc8...8ec1930e

# Set to "on" to enable debug logging
ONESDK_LOG=off

# Parameter description
MY_PROVIDER_PARAM_ONE=

Expand Down Expand Up @@ -171,6 +193,10 @@ MY_PROVIDER_TOKEN=

expect(result).toStrictEqual({
content: `SUPERFACE_ONESDK_TOKEN=sfs_b31314b7fc8...8ec1930e

# Set to "on" to enable debug logging
ONESDK_LOG=off

# Deployment zone
# for AWS
MY_PROVIDER_PARAM_TWO=us-west-1
Expand All @@ -193,6 +219,10 @@ MY_PROVIDER_PARAM_ONE=

expect(result).toStrictEqual({
content: `SUPERFACE_ONESDK_TOKEN=sfs_b31314b7fc8...8ec1930e

# Set to "on" to enable debug logging
ONESDK_LOG=off

# Deployment zone
# for AWS
MY_PROVIDER_PARAM_TWO=us-west-1
Expand Down
14 changes: 13 additions & 1 deletion src/logic/application-code/dotenv/dotenv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
prepareSecurityValues,
} from '@superfaceai/ast';

import { ONESDK_LOG_COMMENT, ONESDK_LOG_ENV } from './onesdk-log';
import {
ONESDK_TOKEN_COMMENT,
ONESDK_TOKEN_ENV,
Expand All @@ -27,22 +28,25 @@ export function createNewDotenv({
parameters,
security,
token,
logEnabled,
}: {
previousDotenv?: string;
providerName: string;
parameters?: IntegrationParameter[];
security?: SecurityScheme[];
token?: string | null;
logEnabled?: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are not passing this new argument. So it's only for potential use in the future?

}): NewDotenv {
const previousContent = previousDotenv ?? '';

const parameterEnvs = getParameterEnvs(providerName, parameters);
const securityEnvs = getSecurityEnvs(providerName, security);
const tokenEnv = makeTokenEnv(token);
const logEnv = makeLogEnv(logEnabled === true ? 'on' : 'off');

const newEnvsOnly = makeFilterForNewEnvs(previousContent);

const newEnvVariables = [tokenEnv, ...parameterEnvs, ...securityEnvs]
const newEnvVariables = [tokenEnv, logEnv, ...parameterEnvs, ...securityEnvs]
.filter(uniqueEnvsOnly)
.filter(newEnvsOnly);

Expand All @@ -54,6 +58,14 @@ export function createNewDotenv({
};
}

function makeLogEnv(logValue = 'off'): EnvVar {
return {
name: ONESDK_LOG_ENV,
value: logValue,
comment: ONESDK_LOG_COMMENT,
};
}

function makeTokenEnv(token?: string | null): EnvVar {
return {
name: ONESDK_TOKEN_ENV,
Expand Down
2 changes: 2 additions & 0 deletions src/logic/application-code/dotenv/onesdk-log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const ONESDK_LOG_ENV = 'ONESDK_LOG';
export const ONESDK_LOG_COMMENT = 'Set to "on" to enable debug logging';
Loading