Skip to content

Commit

Permalink
feat: Add new lint rule for the directive KillMode
Browse files Browse the repository at this point in the history
  • Loading branch information
hangxingliu committed Feb 22, 2024
1 parent d3c38c4 commit 0c12140
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
export const enum SystemdDiagnosticType {
unknownDirective = 1,
deprecatedDirective = 2,
deprecatedValue = 3,
}
export type SystemdDiagnostic = Diagnostic & {
type?: SystemdDiagnosticType;
Expand All @@ -35,6 +36,14 @@ export function getDiagnosticForUnknown(range: Range, directiveName: string): Sy
return d;
}

export function getDiagnosticForValue(range: Range, directiveName: string, help: string): SystemdDiagnostic {
const d: SystemdDiagnostic = new Diagnostic(range, help);
d.severity = DiagnosticSeverity.Warning;
d.type = SystemdDiagnosticType.deprecatedValue;
d.directive = directiveName;
return d;
}

export class SystemdDiagnosticManager {
private _collection: DiagnosticCollection;
private static readonly collectionName = "Systemd";
Expand Down
15 changes: 15 additions & 0 deletions src/vscode-lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
SystemdDiagnosticManager,
SystemdDiagnosticType,
getDiagnosticForDeprecated,
getDiagnosticForValue,
getDiagnosticForUnknown,
} from "./diagnostics";
import { getDirectiveKeys } from "./parser/get-directive-keys";
Expand Down Expand Up @@ -82,6 +83,20 @@ export class SystemdLint implements CodeActionProvider {
items.push(getDiagnosticForDeprecated(getRange(), directiveName));
return;
}
// example lint:
// https://github.com/systemd/systemd/blob/effefa30de46f25d0f50a36210a9835097381c2b/src/core/load-fragment.c#L665
if (directiveName === "KillMode") {
if (it.value.toLowerCase() === "none") {
const msg =
"Unit uses KillMode=none. " +
"This is unsafe, as it disables systemd's process lifecycle management for the service. " +
"Please update the service to use a safer KillMode=, such as 'mixed' or 'control-group'. " +
"Support for KillMode=none is deprecated and will eventually be removed.";
const d = getDiagnosticForValue(getRange(it.value.length + 1), directiveName, msg);
items.push(d);
return;
}
}

if (directivePrefixes.find((p) => directiveName.startsWith(p))) return;
if (managers.hasDirective(directiveNameLC)) return;
Expand Down

0 comments on commit 0c12140

Please sign in to comment.