@techdocs/cli
Version:
Utility CLI for managing TechDocs sites in Backstage.
137 lines (133 loc) • 4.01 kB
JavaScript
var config = require('@backstage/config');
class PublisherConfig {
/**
* Maps publisher-specific config keys to config getters.
*/
static configFactories = {
awsS3: PublisherConfig.getValidAwsS3Config,
azureBlobStorage: PublisherConfig.getValidAzureConfig,
googleGcs: PublisherConfig.getValidGoogleGcsConfig,
openStackSwift: PublisherConfig.getValidOpenStackSwiftConfig
};
/**
* Returns Backstage config suitable for use when instantiating a Publisher. If
* there are any missing or invalid options provided, an error is thrown.
*
* Note: This assumes that proper credentials are set in Environment
* variables for the respective GCS/AWS clients to work.
*/
static getValidConfig(opts) {
const publisherType = opts.publisherType;
if (!PublisherConfig.isKnownPublisher(publisherType)) {
throw new Error(`Unknown publisher type ${opts.publisherType}`);
}
return new config.ConfigReader({
// This backend config is not used at all. Just something needed a create a mock discovery instance.
backend: {
baseUrl: "http://localhost:7007",
listen: {
port: 7007
}
},
techdocs: {
publisher: PublisherConfig.configFactories[publisherType](opts),
legacyUseCaseSensitiveTripletPaths: opts.legacyUseCaseSensitiveTripletPaths
}
});
}
/**
* Typeguard to ensure the publisher has a known config getter.
*/
static isKnownPublisher(type) {
return PublisherConfig.configFactories.hasOwnProperty(type);
}
/**
* Retrieve valid AWS S3 configuration based on the command.
*/
static getValidAwsS3Config(opts) {
return {
type: "awsS3",
awsS3: {
bucketName: opts.storageName,
...opts.awsBucketRootPath && {
bucketRootPath: opts.awsBucketRootPath
},
...opts.awsRoleArn && { credentials: { roleArn: opts.awsRoleArn } },
...opts.awsEndpoint && { endpoint: opts.awsEndpoint },
...opts.awsS3ForcePathStyle && { s3ForcePathStyle: true },
...opts.awsS3sse && { sse: opts.awsS3sse },
...opts.awsProxy && { httpsProxy: opts.awsProxy },
...opts.awsMaxAttempts && {
maxAttempts: Number(opts.awsMaxAttempts)
}
}
};
}
/**
* Retrieve valid Azure Blob Storage configuration based on the command.
*/
static getValidAzureConfig(opts) {
if (!opts.azureAccountName) {
throw new Error(
`azureBlobStorage requires --azureAccountName to be specified`
);
}
return {
type: "azureBlobStorage",
azureBlobStorage: {
containerName: opts.storageName,
credentials: {
accountName: opts.azureAccountName,
accountKey: opts.azureAccountKey
}
}
};
}
/**
* Retrieve valid GCS configuration based on the command.
*/
static getValidGoogleGcsConfig(opts) {
return {
type: "googleGcs",
googleGcs: {
bucketName: opts.storageName,
...opts.gcsBucketRootPath && {
bucketRootPath: opts.gcsBucketRootPath
}
}
};
}
/**
* Retrieves valid OpenStack Swift configuration based on the command.
*/
static getValidOpenStackSwiftConfig(opts) {
const missingParams = [
"osCredentialId",
"osSecret",
"osAuthUrl",
"osSwiftUrl"
].filter((param) => !opts[param]);
if (missingParams.length) {
throw new Error(
`openStackSwift requires the following params to be specified: ${missingParams.join(
", "
)}`
);
}
return {
type: "openStackSwift",
openStackSwift: {
containerName: opts.storageName,
credentials: {
id: opts.osCredentialId,
secret: opts.osSecret
},
authUrl: opts.osAuthUrl,
swiftUrl: opts.osSwiftUrl
}
};
}
}
exports.PublisherConfig = PublisherConfig;
//# sourceMappingURL=PublisherConfig.cjs.js.map
;