-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
286256e
commit 88f602f
Showing
10 changed files
with
256 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
src/incus-app-container-files/cli/commands/setpoint/calculate-app-dir-segments.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { assertEquals } from "https://deno.land/std@0.224.0/assert/assert_equals.ts"; | ||
import { calculateAppDirSegments } from "./calculate-app-dir-segments.ts"; | ||
import { s } from "../../../deps.ts"; | ||
import { AbsolutePath } from "../../absolute-path.ts"; | ||
|
||
const cases = [ | ||
["/mnt/apps", "/mnt/apps/app1", ["app1"]], | ||
["/mnt/apps", "/mnt/apps/app1/app2", ["app1", "app2"]], | ||
["/mnt/apps", "/mnt/apps/app1/app2/app3", ["app1", "app2", "app3"]], | ||
] as [AbsolutePath, AbsolutePath, string[]][]; | ||
|
||
for (const [appsDir, appDir, expected] of cases) { | ||
Deno.test(`calculateAppDirSegments(${appsDir}, ${appDir}) => ${s(expected)}`, () => | ||
assertEquals( | ||
calculateAppDirSegments(appsDir)(appDir), | ||
expected, | ||
)); | ||
} |
21 changes: 21 additions & 0 deletions
21
src/incus-app-container-files/cli/commands/setpoint/calculate-app-dir-segments.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { AbsolutePath } from "../../absolute-path.ts"; | ||
import { s } from "../../../deps.ts"; | ||
|
||
export function calculateAppDirSegments<AppsDir extends AbsolutePath>( | ||
appsDir: AppsDir, | ||
): (appDir: AbsolutePath) => string[] { | ||
return function (appDir: AbsolutePath): string[] { | ||
if (appDir === appsDir) { | ||
return []; | ||
} | ||
if (!appDir.startsWith(appsDir)) { | ||
throw new Error( | ||
`Expected appDir to be a subdirectory of appsDir, but got ${ | ||
s({ appDir, appsDir }) | ||
}`, | ||
); | ||
} | ||
const relativeAppDir = appDir.slice(appsDir.length + 1); | ||
return relativeAppDir.split("/"); | ||
}; | ||
} |
44 changes: 44 additions & 0 deletions
44
src/incus-app-container-files/cli/commands/setpoint/calculate-setpoint.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { AbsolutePath } from "../../absolute-path.ts"; | ||
import { CreateAppContainerOptions } from "../create-app-container/options.ts"; | ||
import { Setpoint } from "./setpoint.ts"; | ||
import { | ||
DEFAULT_CONFIG_FILE_LOADERS, | ||
DEFAULT_LOAD_CONFIG_FILES_OPTIONS, | ||
loadConfig, | ||
} from "../../../deps.ts"; | ||
import { findAppDirs } from "./find-app-dirs.ts"; | ||
import { calculateAppDirSegments } from "./calculate-app-dir-segments.ts"; | ||
|
||
/** | ||
* Builds a list of apps, with {@link findAppDirs}. | ||
* Populates each app in the list, with config loaded by https://deno.land/x/load_config_files. { commonNames: ["index", "common", "incus-app-container"]} | ||
* Returns a setpoint, which is a list of apps, with their configs. | ||
* @param appsDir | ||
*/ | ||
export async function calculateSetpoint<AppsDir extends AbsolutePath>( | ||
appsDir: AppsDir, | ||
): Promise<Setpoint<AppsDir>> { | ||
const appDirs: AbsolutePath[] = await findAppDirs(appsDir); | ||
const apps: Setpoint<AppsDir>["apps"] = Object.fromEntries( | ||
await Promise.all(appDirs.map(async (appDir) => [ | ||
appDir, | ||
await loadConfig( | ||
new URL(`file://${appsDir}`), | ||
calculateAppDirSegments(appsDir)(appDir), | ||
{ | ||
commonNames: [ | ||
...DEFAULT_LOAD_CONFIG_FILES_OPTIONS.commonNames, | ||
"incus-app-container", | ||
], | ||
configTransformers: [ | ||
(config) => config as CreateAppContainerOptions<AppsDir>, | ||
], | ||
}, | ||
) as CreateAppContainerOptions<AppsDir>, | ||
])), | ||
); | ||
return { | ||
appsDir, | ||
apps, | ||
}; | ||
} |
85 changes: 85 additions & 0 deletions
85
src/incus-app-container-files/cli/commands/setpoint/find-app-dirs.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { | ||
DEFAULT_CONFIG_FILE_LOADERS, | ||
exists, | ||
pFilter, | ||
pMap, | ||
prop, | ||
} from "../../../deps.ts"; | ||
import { AbsolutePath, isAbsolutePath } from "../../absolute-path.ts"; | ||
|
||
const CONFIG_FILE_EXTENSIONS = Object.keys(DEFAULT_CONFIG_FILE_LOADERS); | ||
|
||
function calculatePossibleConfigFilePaths( | ||
directory: AbsolutePath, | ||
): AbsolutePath[] { | ||
return CONFIG_FILE_EXTENSIONS.map((extension) => | ||
`${directory}/incus-app-container.${extension}` as AbsolutePath | ||
); | ||
} | ||
|
||
async function directoryHasAnyConfigFile( | ||
directory: AbsolutePath, | ||
): Promise<boolean> { | ||
if (!(await exists(directory, { isDirectory: true }))) { | ||
return false; | ||
} | ||
const possibleConfigFilePaths = calculatePossibleConfigFilePaths(directory); | ||
return (await pFilter(possibleConfigFilePaths, exists)).length > 0; | ||
} | ||
|
||
function isDirectory( | ||
entry: Deno.DirEntry, | ||
): entry is Deno.DirEntry & { isDirectory: true } { | ||
return entry.isDirectory; | ||
} | ||
|
||
/** | ||
* Builds a list of apps, by scanning the appsDirectory recursively. | ||
* When it encounters an incus-app-container.${CONFIG_FILE_EXTENSIONS} file in a directory, it considers that directory an app, and doesn't traverse it any deeper. | ||
* When it encounters a directory named "appdata", or a file named "docker-compose.{yml,yaml,json}", it considers its parent directory a likely disabled app, and doesn't traverse it any deeper. | ||
* This function is recursive, and will call itself on subdirectories. | ||
* | ||
* Does NOT read the config files, just returns the directory names. | ||
* | ||
* @param appsDir the directory where app directories are stored | ||
* @returns a list of directories that are apps (have an `incus-app-container.${CONFIG_FILE_EXTENSIONS}` file) | ||
*/ | ||
export async function findAppDirs< | ||
AppsDir extends AbsolutePath, | ||
>(appsDir: AppsDir): Promise<AbsolutePath[]> { | ||
if (await directoryHasAnyConfigFile(appsDir)) { | ||
return [appsDir as AbsolutePath]; | ||
} | ||
return (await pMap( | ||
await pFilter(Deno.readDir(appsDir), prop("isDirectory")), | ||
async (entry: Deno.DirEntry & { isDirectory: true }) => { | ||
const potentialAppDir = `${appsDir}/${entry.name}` as AbsolutePath; | ||
if (await directoryHasAnyConfigFile(potentialAppDir)) { | ||
return [potentialAppDir]; | ||
} | ||
if (entry.name === "appdata") { | ||
return []; | ||
} | ||
if (await exists(`${potentialAppDir}/docker-compose.yml`)) { | ||
return []; | ||
} | ||
if (await exists(`${potentialAppDir}/docker-compose.yaml`)) { | ||
return []; | ||
} | ||
if (await exists(`${potentialAppDir}/docker-compose.json`)) { | ||
return []; | ||
} | ||
return await findAppDirs(potentialAppDir); | ||
}, | ||
)).flat(); | ||
} | ||
|
||
if (import.meta.main) { | ||
const appsDir = new URL(Deno.args[0], import.meta.url).pathname; | ||
if (!isAbsolutePath(appsDir)) { | ||
throw new Error( | ||
`Expected absolute path for apps-dir, but got ${s(appsDir)}`, | ||
); | ||
} | ||
console.log(await findAppDirs(appsDir)); | ||
} |
19 changes: 19 additions & 0 deletions
19
src/incus-app-container-files/cli/commands/setpoint/mod.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { AbsolutePath } from "../../absolute-path.ts"; | ||
import { calculateSetpoint } from "./calculate-setpoint.ts"; | ||
import { SetpointInputOptions } from "../../config.ts"; | ||
|
||
/** | ||
* Prints the current setpoint; the containers we want, according to configuration files. | ||
* @param options | ||
*/ | ||
export async function setpoint<AppsDir extends AbsolutePath>( | ||
options: SetpointInputOptions<AppsDir>, | ||
): Promise<void> { | ||
console.log( | ||
JSON.stringify( | ||
await calculateSetpoint(options.appsDir), | ||
null, | ||
2, | ||
), | ||
); | ||
} |
7 changes: 7 additions & 0 deletions
7
src/incus-app-container-files/cli/commands/setpoint/setpoint.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { AbsolutePath } from "../../absolute-path.ts"; | ||
import { CreateAppContainerOptions } from "../create-app-container/options.ts"; | ||
|
||
export type Setpoint<AppsDir extends AbsolutePath> = { | ||
appsDir: AppsDir; | ||
apps: Record<string, CreateAppContainerOptions<AppsDir>>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters