Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More types #1268

Merged
merged 1 commit into from
Apr 23, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { URI } from "vscode-uri";
import { Connection } from "vscode-languageserver";
import { WorkspaceFolderContext } from "./workspaceManager";
import { CommandRunner } from "../utils/commandRunner";
import { ansibleMetaDataType } from "../utils/getAnsibleMetaData";

export class AnsibleConfig {
private connection: Connection;
Expand All @@ -13,7 +14,7 @@ export class AnsibleConfig {
private _module_locations: string[] = [];
private _ansible_location = "";
private _default_host_list: string[] = [];
private _ansible_meta_data = {};
private _ansible_meta_data = {}; // ini data

constructor(connection: Connection, context: WorkspaceFolderContext) {
this.connection = connection;
Expand Down Expand Up @@ -127,7 +128,7 @@ export class AnsibleConfig {
return this._ansible_location;
}

public get ansible_meta_data(): object {
public get ansible_meta_data(): ansibleMetaDataType {
return this._ansible_meta_data;
}
}
Expand Down
109 changes: 68 additions & 41 deletions packages/ansible-language-server/src/utils/getAnsibleMetaData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,38 @@ import { CommandRunner } from "./commandRunner";
import * as child_process from "child_process";

let context: WorkspaceFolderContext;
let connection: Connection;
let connection: Connection | undefined;

export interface ansibleMetaDataEntryType {
[name: string]:
| {
[name: string]: string | string[] | undefined | object[];
}
| string
| string[]
| object[]
| undefined;
}

export interface ansibleMetaDataType {
"ansible information"?: ansibleMetaDataEntryType;
"python information"?: ansibleMetaDataEntryType;
"ansible-lint information"?: ansibleMetaDataEntryType;
"execution environment information"?: ansibleMetaDataEntryType | undefined;
}

export async function getAnsibleMetaData(
contextLocal: WorkspaceFolderContext,
connectionLocal: Connection,
) {
connectionLocal: Connection | undefined,
): Promise<ansibleMetaDataType> {
context = contextLocal;
connection = connectionLocal;

const ansibleMetaData = {};

ansibleMetaData["ansible information"] = await getAnsibleInfo();
ansibleMetaData["python information"] = await getPythonInfo();
ansibleMetaData["ansible-lint information"] = await getAnsibleLintInfo();
const ansibleMetaData: ansibleMetaDataType = {
"ansible information": await getAnsibleInfo(),
"python information": await getPythonInfo(),
"ansible-lint information": await getAnsibleLintInfo(),
};

const settings = await context.documentSettings.get(
context.workspaceFolder.uri,
Expand All @@ -32,7 +50,7 @@ export async function getAnsibleMetaData(
return ansibleMetaData;
}

export async function getResultsThroughCommandRunner(cmd, arg) {
export async function getResultsThroughCommandRunner(cmd: string, arg: string) {
const settings = await context.documentSettings.get(
context.workspaceFolder.uri,
);
Expand All @@ -56,8 +74,14 @@ export async function getResultsThroughCommandRunner(cmd, arg) {
return result;
}
} catch (error) {
let errorMessage: string;
if (error instanceof Error) {
errorMessage = error.message;
} else {
errorMessage = String(error);
}
console.log(
`cmd '${cmd} ${arg}' was not executed with the following error: ' ${error.toString()}`,
`cmd '${cmd} ${arg}' was not executed with the following error: ' ${errorMessage}`,
);
return undefined;
}
Expand All @@ -66,7 +90,7 @@ export async function getResultsThroughCommandRunner(cmd, arg) {
}

async function getAnsibleInfo() {
const ansibleInfo = {};
const ansibleInfo: ansibleMetaDataEntryType = {};

const ansibleVersionObj = (await context.ansibleConfig).ansible_meta_data;
const ansibleVersionObjKeys = Object.keys(ansibleVersionObj);
Expand All @@ -76,21 +100,25 @@ async function getAnsibleInfo() {
return ansibleInfo;
}

let ansibleCoreVersion;
let ansibleCoreVersion: string[] = [];
if (ansibleVersionObjKeys[0].includes(" [")) {
ansibleCoreVersion = ansibleVersionObjKeys[0].split(" [");
} else {
ansibleCoreVersion = ansibleVersionObjKeys[0].split(" ");
}
ansibleInfo["core version"] = ansibleCoreVersion[1]
.slice(0, -1)
.split(" ")
.pop()
.trim();
?.slice(0, -1)
?.split(" ")
?.pop()
?.trim();

ansibleInfo["location"] = (await context.ansibleConfig).ansible_location;

ansibleInfo["config file path"] = ansibleVersionObj["config file"];
if ("config file" in ansibleVersionObj) {
ansibleInfo["config file path"] = ansibleVersionObj[
"config file"
] as string;
}

ansibleInfo["collections location"] = (
await context.ansibleConfig
Expand All @@ -108,7 +136,7 @@ async function getAnsibleInfo() {
}

async function getPythonInfo() {
const pythonInfo = {};
const pythonInfo: ansibleMetaDataEntryType = {};

const pythonVersionResult = await getResultsThroughCommandRunner(
"python3",
Expand All @@ -118,25 +146,25 @@ async function getPythonInfo() {
return pythonInfo;
}

pythonInfo["version"] = pythonVersionResult.stdout
.trim()
.split(" ")
.pop()
.trim();
pythonInfo["version"] = pythonVersionResult?.stdout
?.trim()
?.split(" ")
?.pop()
?.trim();

const pythonPathResult = await getResultsThroughCommandRunner(
"python3",
'-c "import sys; print(sys.executable)"',
);
pythonInfo["location"] = pythonPathResult.stdout.trim();
pythonInfo["location"] = pythonPathResult?.stdout?.trim();

return pythonInfo;
}

async function getAnsibleLintInfo() {
const ansibleLintInfo = {};
const ansibleLintInfo: ansibleMetaDataEntryType = {};

let ansibleLintVersionResult = await getResultsThroughCommandRunner(
const ansibleLintVersionResult = await getResultsThroughCommandRunner(
"ansible-lint",
"--version",
);
Expand All @@ -153,21 +181,20 @@ async function getAnsibleLintInfo() {
// ansible-lint version reports if a newer version of the ansible-lint is available or not
// along with the current version itself
// so the following lines of code are to segregate the two information into to keys
ansibleLintVersionResult = ansibleLintVersionResult.stdout.trim().split("\n");
const ansibleLintVersion = ansibleLintVersionResult[0];
const ansibleLintUpgradeStatus = ansibleLintVersionResult[1]
? ansibleLintVersionResult[1]
: undefined;

ansibleLintInfo["version"] = ansibleLintVersion
.split("using")[0]
const ansibleLintVersionStdout = ansibleLintVersionResult.stdout
.trim()
.split(" ")
.pop()
.trim();
ansibleLintInfo["upgrade status"] = ansibleLintUpgradeStatus;
.split("\n");
const ansibleLintVersion = ansibleLintVersionStdout[0];
if (ansibleLintVersionStdout.length >= 2) {
ansibleLintInfo["upgrade status"] = ansibleLintVersionStdout[1];
}

ansibleLintInfo["version"] =
ansibleLintVersion?.split("using")[0]?.trim()?.split(" ")?.pop()?.trim() ||
undefined;

ansibleLintInfo["location"] = ansibleLintPathResult.stdout.trim();
ansibleLintInfo["location"] =
ansibleLintPathResult?.stdout?.trim() || undefined;

ansibleLintInfo["config file path"] =
context.ansibleLint.ansibleLintConfigFilePath;
Expand All @@ -176,12 +203,12 @@ async function getAnsibleLintInfo() {
}

async function getExecutionEnvironmentInfo() {
const eeInfo = {};
const eeInfo: ansibleMetaDataEntryType = {};

const basicDetails = (await context.executionEnvironment)
.getBasicContainerAndImageDetails;

eeInfo["container engine"] = basicDetails.containerEngine;
eeInfo["container engine"] = String(basicDetails.containerEngine);
eeInfo["container image"] = basicDetails.containerImage;
eeInfo["container image ID"] = basicDetails.containerImageId;

Expand Down
Loading
Loading