Skip to content

Commit

Permalink
fixup! feat(cli): build based on configuration
Browse files Browse the repository at this point in the history
fix typing
  • Loading branch information
tuler authored and endersonmaia committed Oct 15, 2024
1 parent 381f9bc commit 7204625
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 33 deletions.
40 changes: 20 additions & 20 deletions apps/cli/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,56 @@ import { TomlPrimitive, parse as parseToml } from "smol-toml";
* Typed Errors
*/
export class InvalidBuilderError extends Error {
constructor(builder: string) {
constructor(builder: TomlPrimitive) {
super(`Invalid builder: ${builder}`);
this.name = "InvalidBuilder";
}
}

export class InvalidDriveFormatError extends Error {
constructor(format: string) {
constructor(format: TomlPrimitive) {
super(`Invalid drive format: ${format}`);
this.name = "InvalidDriveFormatError";
}
}

export class InvalidEmptyDriveFormatError extends Error {
constructor(format: string) {
constructor(format: TomlPrimitive) {
super(`Invalid empty drive format: ${format}`);
this.name = "InvalidEmptyDriveFormatError";
}
}

export class InvalidStringValueError extends Error {
constructor(value: string) {
constructor(value: TomlPrimitive) {
super(`Invalid string value: ${value}`);
this.name = "InvalidStringValueError";
}
}

export class InvalidBooleanValueError extends Error {
constructor(value: string) {
constructor(value: TomlPrimitive) {
super(`Invalid boolean value: ${value}`);
this.name = "InvalidBooleanValueError";
}
}

export class InvalidNumberValueError extends Error {
constructor(value: string) {
constructor(value: TomlPrimitive) {
super(`Invalid number value: ${value}`);
this.name = "InvalidNumberValueError";
}
}

export class InvalidBytesValueError extends Error {
constructor(value: string) {
constructor(value: TomlPrimitive) {
super(`Invalid bytes value: ${value}`);
this.name = "InvalidBytesValueError";
}
}

export class RequiredFieldError extends Error {
constructor(key: string) {
constructor(key: TomlPrimitive) {
super(`Missing required field: ${key}`);
this.name = "RequiredFieldError";
}
Expand Down Expand Up @@ -207,7 +207,7 @@ const parseBoolean = (value: TomlPrimitive, defaultValue: boolean): boolean => {
} else if (typeof value === "boolean") {
return value;
}
throw new InvalidBooleanValueError(value as string);
throw new InvalidBooleanValueError(value);
};

const parseOptionalBoolean = (value: TomlPrimitive): boolean | undefined => {
Expand All @@ -216,7 +216,7 @@ const parseOptionalBoolean = (value: TomlPrimitive): boolean | undefined => {
} else if (typeof value === "boolean") {
return value;
}
throw new InvalidBooleanValueError(value as string);
throw new InvalidBooleanValueError(value);
};

const parseString = (value: TomlPrimitive, defaultValue: string): string => {
Expand All @@ -225,7 +225,7 @@ const parseString = (value: TomlPrimitive, defaultValue: string): string => {
} else if (typeof value === "string") {
return value;
}
throw new InvalidStringValueError(value as unknown as string);
throw new InvalidStringValueError(value);
};

const parseStringArray = (value: TomlPrimitive): string[] => {
Expand All @@ -238,7 +238,7 @@ const parseStringArray = (value: TomlPrimitive): string[] => {
if (typeof v === "string") {
return v;
}
throw new InvalidStringValueError(v as unknown as string);
throw new InvalidStringValueError(v);
});
}
throw new InvalidStringArrayError();
Expand All @@ -250,7 +250,7 @@ const parseRequiredString = (value: TomlPrimitive, key: string): string => {
} else if (typeof value === "string") {
return value;
}
throw new InvalidStringValueError(value as unknown as string);
throw new InvalidStringValueError(value);
};

const parseOptionalString = (value: TomlPrimitive): string | undefined => {
Expand All @@ -259,7 +259,7 @@ const parseOptionalString = (value: TomlPrimitive): string | undefined => {
} else if (typeof value === "string") {
return value;
}
throw new InvalidStringValueError(value as unknown as string);
throw new InvalidStringValueError(value);
};

const parseOptionalStringBoolean = (
Expand All @@ -272,7 +272,7 @@ const parseOptionalStringBoolean = (
} else if (typeof value === "boolean") {
return value;
}
throw new InvalidStringValueError(value as unknown as string);
throw new InvalidStringValueError(value);
};

const parseOptionalNumber = (value: TomlPrimitive): bigint | undefined => {
Expand All @@ -283,7 +283,7 @@ const parseOptionalNumber = (value: TomlPrimitive): bigint | undefined => {
} else if (typeof value === "number") {
return BigInt(value);
}
throw new InvalidNumberValueError(value as string);
throw new InvalidNumberValueError(value);
};

const parseBytes = (value: TomlPrimitive, defaultValue: number): number => {
Expand All @@ -294,7 +294,7 @@ const parseBytes = (value: TomlPrimitive, defaultValue: number): number => {
} else if (typeof value === "number" || typeof value === "string") {
return bytes.parse(value);
}
throw new InvalidBytesValueError(value as unknown as string);
throw new InvalidBytesValueError(value);
};

const parseBuilder = (value: TomlPrimitive): Builder => {
Expand All @@ -314,7 +314,7 @@ const parseBuilder = (value: TomlPrimitive): Builder => {
return "tar";
}
}
throw new InvalidBuilderError(value as string);
throw new InvalidBuilderError(value);
};

const parseFormat = (value: TomlPrimitive): DriveFormat => {
Expand All @@ -328,7 +328,7 @@ const parseFormat = (value: TomlPrimitive): DriveFormat => {
return "sqfs";
}
}
throw new InvalidDriveFormatError(value as string);
throw new InvalidDriveFormatError(value);
};

const parseEmptyFormat = (value: TomlPrimitive): "ext2" | "raw" => {
Expand All @@ -342,7 +342,7 @@ const parseEmptyFormat = (value: TomlPrimitive): "ext2" | "raw" => {
return "raw";
}
}
throw new InvalidEmptyDriveFormatError(value as string);
throw new InvalidEmptyDriveFormatError(value);
};

const parseMachine = (value: TomlPrimitive): MachineConfig => {
Expand Down
24 changes: 11 additions & 13 deletions apps/cli/test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ shared = true`);
new InvalidBuilderError("invalid"),
);
expect(() => parse("[drives.root]\nbuilder = true")).toThrowError(
new InvalidBuilderError(true as unknown as string),
new InvalidBuilderError(true),
);
expect(() => parse("[drives.root]\nbuilder = 10")).toThrowError(
new InvalidBuilderError(10 as unknown as string),
new InvalidBuilderError(10),
);
expect(() => parse("[drives.root]\nbuilder = {}")).toThrowError(
new InvalidBuilderError({} as unknown as string),
new InvalidBuilderError({}),
);
});

Expand All @@ -69,13 +69,13 @@ shared = true`);
new InvalidDriveFormatError("invalid"),
);
expect(() => parse("[drives.root]\nformat = true")).toThrowError(
new InvalidDriveFormatError(true as unknown as string),
new InvalidDriveFormatError(true),
);
expect(() => parse("[drives.root]\nformat = 10")).toThrowError(
new InvalidDriveFormatError(10 as unknown as string),
new InvalidDriveFormatError(10),
);
expect(() => parse("[drives.root]\nformat = {}")).toThrowError(
new InvalidDriveFormatError({} as unknown as string),
new InvalidDriveFormatError({}),
);
});

Expand All @@ -93,21 +93,19 @@ shared = true`);

it("invalid drive: invalid mount", () => {
expect(() => parse("[drives.data]\nmount = 42")).toThrowError(
new InvalidStringValueError(42 as unknown as string),
new InvalidStringValueError(42),
);
});

it("invalid empty drive: invalid fomat", () => {
expect(() =>
parse("[drives.data]\nbuilder = 'empty'\nformat = 42"),
).toThrowError(
new InvalidEmptyDriveFormatError(42 as unknown as string),
);
).toThrowError(new InvalidEmptyDriveFormatError(42));
});

it("invalid boolean value", () => {
expect(() => parse("[machine]\nno-rollup = 42")).toThrowError(
new InvalidBooleanValueError(42 as unknown as string),
new InvalidBooleanValueError(42),
);
});

Expand All @@ -119,7 +117,7 @@ shared = true`);
format = "ext2"
`;
expect(() => parse(invalidTarDrive)).toThrowError(
new InvalidStringValueError(42 as unknown as string),
new InvalidStringValueError(42),
);
});

Expand Down Expand Up @@ -152,7 +150,7 @@ shared = true`);
bootargs = ["no4lvl", "quiet", false]
`;
expect(() => parse(invalidConfig)).toThrowError(
new InvalidStringValueError(false as unknown as string),
new InvalidStringValueError(false),
);
});
});

0 comments on commit 7204625

Please sign in to comment.