Skip to content
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Copyright 2024 FlexStack Contributors. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Autogenerate a Dockerfile

FlexStack's `new-dockerfile` CLI tool and Go package automatically generates a configurable Dockerfile
based on your project source code. It supports a wide range of languages and frameworks, including Next.js,
Node.js, Python, Ruby, Java/Spring Boot, Go, Elixir/Phoenix, and more.

For detailed documentation, visit the [FlexStack Documentation](https://flexstack.com/docs/languages-and-frameworks/autogenerate-dockerfile) page.

## Installation

### cURL

```sh
curl -sSL https://flexstack.com/install/new-dockerfile | sh
```

### Go Binary

```sh
go install github.com/flexstack/new-dockerfile@latest
```

### Go Package

```sh
go get github.com/flexstack/new-dockerfile
```

## Supported Platforms for CLI

- **macOS** (arm64, x86_64)
- **Linux** (arm64, x86_64)
- **Windows** (x86_64, i386)

## CLI Usage

```sh
new-dockerfile [options]
```

## CLI Options

- `--path` - Path to the project source code (default: `.`)
- `--write` - Write the generated Dockerfile to the project at the specified path (default: `false`)
- `--runtime` - Force a specific runtime, e.g. `node` (default: `auto`)
- `--quiet` - Disable all logging except for errors (default: `false`)
- `--help` - Show help
2 changes: 1 addition & 1 deletion cmd/new-dockerfile/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func main() {
var path string
flag.StringVar(&path, "path", ".", "Path to the project directory")
var noColor bool
flag.BoolVar(&noColor, "no-color", false, "Disable colorized output")
flag.BoolVar(&noColor, "no-color", os.Getenv("NO_COLOR") == "true" || os.Getenv("ANSI_COLORS_DISABLED") == "true", "Disable colorized output")
var runtimeArg string
flag.StringVar(&runtimeArg, "runtime", "", "Force a specific runtime")
var quiet bool
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/flexstack/new-dockerfile

go 1.22.3
go 1.21

toolchain go1.22.3

require (
github.com/lmittmann/tint v1.0.4
Expand Down
8 changes: 8 additions & 0 deletions node/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Copyright 2024 FlexStack Contributors. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

28 changes: 27 additions & 1 deletion node/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
# Autogenerate a Dockerfile from your project source code
# Autogenerate a Dockerfile

FlexStack's `new-dockerfile` CLI tool automatically generates a configurable Dockerfile based on your project source code.
It supports a wide range of languages and frameworks, including Next.js, Node.js, Python, Ruby, Java/Spring Boot, Go,
Elixir/Phoenix, and more.

For detailed documentation, visit the [FlexStack Documentation](https://flexstack.com/docs/languages-and-frameworks/autogenerate-dockerfile) page.

## Supported Platforms

- **macOS** (arm64, x86_64)
- **Linux** (arm64, x86_64)
- **Windows** (x86_64, i386)

## Usage

```sh
npx new-dockerfile [options]
```

## Options

- `--path` - Path to the project source code (default: `.`)
- `--write` - Write the generated Dockerfile to the project at the specified path (default: `false`)
- `--runtime` - Force a specific runtime, e.g. `node` (default: `auto`)
- `--quiet` - Disable all logging except for errors (default: `false`)
- `--help` - Show help
Binary file added node/bin/new-dockerfile
Binary file not shown.
213 changes: 208 additions & 5 deletions node/install.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,208 @@
// TODO:
// 1. Determine the platform and architecture of the system
// 2. Determine the version of the npm package
// 3. Download the Golang binary for the platform and architecture from GitHub releases
console.log("Coming soon...");
// Credits:
// - Bun.js (https://github.com/oven-sh/bun/blob/main/packages/bun-release/src)
const child_process = require("child_process");
const { unzipSync } = require("zlib");
const packageJson = require("./package.json");
const fs = require("fs");

function getPlatform() {}

async function downloadCli(version, platform) {
const ext = platform.os === "win32" ? ".zip" : ".tar.gz";
const response = await fetch(
`https://github.com/flexstack/new-dockerfile/releases/download/v${version}/${platform.bin}${ext}`
);
const tgz = await response.arrayBuffer();
let buffer;

try {
buffer = unzipSync(tgz);
} catch (cause) {
throw new Error("Invalid gzip data", { cause });
}

function str(i, n) {
return String.fromCharCode(...buffer.subarray(i, i + n)).replace(
/\0.*$/,
""
);
}
let offset = 0;
const dst = platform.exe;
while (offset < buffer.length) {
const name = str(offset, 100).replace("package/", "");
const size = parseInt(str(offset + 124, 12), 8);
offset += 512;
if (!isNaN(size)) {
write(dst, buffer.subarray(offset, offset + size));
if (name === platform.exe) {
try {
fs.chmodSync(dst, 0o755);
} catch (error) {}
}
offset += (size + 511) & ~511;
}
}
}

const fetch = "fetch" in globalThis ? webFetch : nodeFetch;

async function webFetch(url, options) {
const response = await globalThis.fetch(url, options);
if (options?.assert !== false && !isOk(response.status)) {
try {
await response.text();
} catch {}
throw new Error(`${response.status}: ${url}`);
}
return response;
}

async function nodeFetch(url, options) {
const { get } = await import("node:http");
return new Promise((resolve, reject) => {
get(url, (response) => {
const status = response.statusCode ?? 501;
if (response.headers.location && isRedirect(status)) {
return nodeFetch(url).then(resolve, reject);
}
if (options?.assert !== false && !isOk(status)) {
return reject(new Error(`${status}: ${url}`));
}
const body = [];
response.on("data", (chunk) => {
body.push(chunk);
});
response.on("end", () => {
resolve({
ok: isOk(status),
status,
async arrayBuffer() {
return Buffer.concat(body).buffer;
},
async text() {
return Buffer.concat(body).toString("utf-8");
},
async json() {
const text = Buffer.concat(body).toString("utf-8");
return JSON.parse(text);
},
});
});
}).on("error", reject);
});
}

function isOk(status) {
return status >= 200 && status <= 204;
}

function isRedirect(status) {
switch (status) {
case 301: // Moved Permanently
case 308: // Permanent Redirect
case 302: // Found
case 307: // Temporary Redirect
case 303: // See Other
return true;
}
return false;
}

const os = process.platform;

const arch =
os === "darwin" && process.arch === "x64" && isRosetta2()
? "arm64"
: process.arch;

const platforms = [
{
os: "darwin",
arch: "x64",
bin: "new-dockerfile-darwin-x86_64",
exe: "bin/new-dockerfile",
},
{
os: "darwin",
arch: "arm64",
bin: "new-dockerfile-darwin-arm64",
exe: "bin/new-dockerfile",
},
{
os: "linux",
arch: "x64",
bin: "new-dockerfile-linux-x86_64",
exe: "bin/new-dockerfile",
},
{
os: "linux",
arch: "arm64",
bin: "new-dockerfile-linux-arm64",
exe: "bin/new-dockerfile",
},
{
os: "win32",
arch: "x64",
bin: "new-dockerfile-windows-x86_64",
exe: "bin/new-dockerfile.exe",
},
{
os: "win32",
arch: "arm64",
bin: "new-dockerfile-windows-arm64",
exe: "bin/new-dockerfile.exe",
},
];

const supportedPlatforms = platforms.filter(
(platform) => platform.os === os && platform.arch === arch
);

function isRosetta2() {
try {
const { exitCode, stdout } = spawn("sysctl", [
"-n",
"sysctl.proc_translated",
]);
return exitCode === 0 && stdout.includes("1");
} catch (error) {
return false;
}
}

function spawn(cmd, args, options = {}) {
const { status, stdout, stderr } = child_process.spawnSync(cmd, args, {
stdio: "pipe",
encoding: "utf-8",
...options,
});
return {
exitCode: status ?? 1,
stdout,
stderr,
};
}

function write(dst, content) {
try {
fs.writeFileSync(dst, content);
return;
} catch (error) {
// If there is an error, ensure the parent directory
// exists and try again.
try {
fs.mkdirSync(path.dirname(dst), { recursive: true });
} catch (error) {
// The directory could have been created already.
}
fs.writeFileSync(dst, content);
}
}

if (supportedPlatforms.length === 0) {
throw new Error("Unsupported platform: " + os + " " + arch);
}

// Read version from package.json
downloadCli(packageJson.config.bin_version, supportedPlatforms[0]);
5 changes: 4 additions & 1 deletion node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"name": "new-dockerfile",
"version": "0.1.1",
"version": "0.1.2",
"config": {
"bin_version": "0.1.0"
},
"description": "Autogenerate Dockerfiles from your project source code",
"main": "index.js",
"bin": {
Expand Down