Skip to content

Commit

Permalink
Fix: Only adding folders to stack with a compose file. (#299)
Browse files Browse the repository at this point in the history
Co-authored-by: Louis Lam <louislam@users.noreply.github.com>
  • Loading branch information
Ozy-Viking and louislam authored Dec 15, 2023
1 parent db0add7 commit 94ca8a1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
28 changes: 26 additions & 2 deletions backend/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import yaml from "yaml";
import { DockgeSocket, fileExists, ValidationError } from "./util-server";
import path from "path";
import {
acceptedComposeFileNames,
COMBINED_TERMINAL_COLS,
COMBINED_TERMINAL_ROWS,
CREATED_FILE,
Expand Down Expand Up @@ -40,8 +41,7 @@ export class Stack {

if (!skipFSOperations) {
// Check if compose file name is different from compose.yaml
const supportedFileNames = [ "compose.yaml", "compose.yml", "docker-compose.yml", "docker-compose.yaml" ];
for (const filename of supportedFileNames) {
for (const filename of acceptedComposeFileNames) {
if (fs.existsSync(path.join(this.path, filename))) {
this._composeFileName = filename;
break;
Expand Down Expand Up @@ -222,6 +222,26 @@ export class Stack {
}
}

/**
* Checks if a compose file exists in the specified directory.
* @async
* @static
* @param {string} stacksDir - The directory of the stack.
* @param {string} filename - The name of the directory to check for the compose file.
* @returns {Promise<boolean>} A promise that resolves to a boolean indicating whether any compose file exists.
*/
static async composeFileExists(stacksDir : string, filename : string) : Promise<boolean> {
let filenamePath = path.join(stacksDir, filename);
// Check if any compose file exists
for (const filename of acceptedComposeFileNames) {
let composeFile = path.join(filenamePath, filename);
if (await fileExists(composeFile)) {
return true;
}
}
return false;
}

static async getStackList(server : DockgeServer, useCacheForManaged = false) : Promise<Map<string, Stack>> {
let stacksDir = server.stacksDir;
let stackList : Map<string, Stack>;
Expand All @@ -242,6 +262,10 @@ export class Stack {
if (!stat.isDirectory()) {
continue;
}
// If no compose file exists, skip it
if (!await Stack.composeFileExists(stacksDir, filename)) {
continue;
}
let stack = await this.getStack(server, filename);
stack._status = CREATED_FILE;
stackList.set(filename, stack);
Expand Down
7 changes: 7 additions & 0 deletions backend/util-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ export const allowedRawKeys = [
"\u0003", // Ctrl + C
];

export const acceptedComposeFileNames = [
"compose.yaml",
"docker-compose.yaml",
"docker-compose.yml",
"compose.yml",
];

/**
* Generate a decimal integer number from a string
* @param str Input
Expand Down

0 comments on commit 94ca8a1

Please sign in to comment.