Skip to content

Commit

Permalink
add go mod support to "arri use" (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshmossas authored Nov 30, 2024
1 parent e8b01bf commit 82c1787
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 1 deletion.
70 changes: 69 additions & 1 deletion tooling/cli/src/commands/use.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { updateCargoToml, updatePackageJson, updatePubspecYaml } from "./use";
import {
updateCargoToml,
updateGoMod,
updatePackageJson,
updatePubspecYaml,
} from "./use";

describe("updatePackageJson()", () => {
it("updates relevant lines and preserves formatting", () => {
Expand Down Expand Up @@ -115,3 +120,66 @@ tokio = { version = "1.39.2", features = ["full"] }`;
expect(output.updated).toBe(true);
});
});

describe("updateGoMod()", () => {
it("updates relevant lines", () => {
const input = `module main
go 1.23.3
require github.com/modiimedia/arri v0.66.0
require (
github.com/iancoleman/strcase v0.3.0 // indirect
github.com/tidwall/gjson v1.18.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
)
`;
const expectedOutput = `module main
go 1.23.3
require github.com/modiimedia/arri v1.12.0
require (
github.com/iancoleman/strcase v0.3.0 // indirect
github.com/tidwall/gjson v1.18.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
)
`;
const output = updateGoMod(input, "1.12.0");
expect(output.content).toBe(expectedOutput);
});
it("updates relevant lines and preserves comments", () => {
const input = `module main
go 1.23.3
require github.com/modiimedia/arri v0.66.0 // this is a comment
require (
github.com/iancoleman/strcase v0.3.0 // indirect
github.com/tidwall/gjson v1.18.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
)
`;
const expectedOutput = `module main
go 1.23.3
require github.com/modiimedia/arri v1.12.0 // this is a comment
require (
github.com/iancoleman/strcase v0.3.0 // indirect
github.com/tidwall/gjson v1.18.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
)
`;
const output = updateGoMod(input, "1.12.0");
expect(output.content).toBe(expectedOutput);
});
});
48 changes: 48 additions & 0 deletions tooling/cli/src/commands/use.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,20 @@ export default defineCommand({
}
await Promise.all(cargoTomlTasks);

// GO DEPENDENCIES
const goModFiles = await globby(["go.mod", "**/go.mod"]);
const goModFileTasks: Promise<any>[] = [];
for (const file of goModFiles) {
goModFileTasks.push(
readFile(path.resolve(file), "utf8").then((content) => {
const result = updateGoMod(content, version);
if (result.updated) {
fileMap[file] = result.content;
}
}),
);
}

// Update all affected files
const updateTasks: Promise<any>[] = [];
for (const key of Object.keys(fileMap)) {
Expand Down Expand Up @@ -353,3 +367,37 @@ export function updateCargoToml(
}
return { updated, content: newLines.join("\n") };
}

export function updateGoMod(
fileContent: string,
version: string,
): { updated: boolean; content: string } {
const lines = fileContent.split("\n");
const newLines: string[] = [];
let updated = false;
for (const line of lines) {
if (!line.includes("github.com/modiimedia/arri")) {
newLines.push(line);
continue;
}
updated = true;
const lineParts = line.split(" ");
const updatedLineParts: string[] = [];
for (const part of lineParts) {
if (part.startsWith("v") && isNumberChar(part[1] ?? "")) {
updatedLineParts.push(`v${version}`);
continue;
}
updatedLineParts.push(part);
}
newLines.push(updatedLineParts.join(" "));
}
return {
updated,
content: newLines.join("\n"),
};
}

function isNumberChar(char: string) {
return !Number.isNaN(Number(char));
}

0 comments on commit 82c1787

Please sign in to comment.