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

Export public interface #70

Merged
merged 1 commit into from
Aug 9, 2023
Merged
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
36 changes: 24 additions & 12 deletions src/ssh-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ const RE_MULTI_VALUE_DIRECTIVE = /^(GlobalKnownHostsFile|Host|IPQoS|SendEnv|User
const RE_QUOTE_DIRECTIVE = /^(?:CertificateFile|IdentityFile|IdentityAgent|User)$/i
const RE_SINGLE_LINE_DIRECTIVE = /^(Include|IdentityFile)$/i

enum LineType {
export enum LineType {
DIRECTIVE = 1,
COMMENT = 2,
}

type Separator = ' ' | '=' | '\t';
export type Separator = ' ' | '=' | '\t';

type Space = ' ' | '\t' | '\n';

interface Directive {
export interface Directive {
type: LineType.DIRECTIVE;
before: string;
after: string;
Expand All @@ -29,28 +29,28 @@ interface Directive {
quoted?: boolean;
}

interface Section extends Directive {
export interface Section extends Directive {
config: SSHConfig;
}

interface Match extends Section {
export interface Match extends Section {
criteria: Record<string, string | string[]>
}

interface Comment {
export interface Comment {
type: LineType.COMMENT;
before: string;
after: string;
content: string;
}

type Line = Match | Section | Directive | Comment;
export type Line = Match | Section | Directive | Comment;

interface FindOptions {
export interface FindOptions {
Host?: string;
}

interface MatchOptions {
export interface MatchOptions {
Host: string;
User?: string;
}
Expand Down Expand Up @@ -133,10 +133,24 @@ function match(criteria: Match['criteria'], context: ComputeContext): boolean {
return true
}

class SSHConfig extends Array<Line> {
export default class SSHConfig extends Array<Line> {
static readonly DIRECTIVE: LineType.DIRECTIVE = LineType.DIRECTIVE
static readonly COMMENT: LineType.COMMENT = LineType.COMMENT

/**
* Parse SSH config text into structured object.
*/
public static parse(text: string): SSHConfig {
return parse(text)
}

/**
* Stringify structured object into SSH config text.
*/
public static stringify(config: SSHConfig): string {
return stringify(config)
}

/**
* Query SSH config by host.
*/
Expand Down Expand Up @@ -653,5 +667,3 @@ export function stringify(config: SSHConfig): string {

return str
}

export default Object.assign(SSHConfig, { parse, stringify })
Loading