Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support input url option for cli #203

Merged
merged 2 commits into from
Dec 11, 2024
Merged

Conversation

sasamuku
Copy link
Member

@sasamuku sasamuku commented Dec 11, 2024

Summary

Support input url option for cli.

./dist-cli/bin/cli.js erd build --input https://github.com/someapp/someapp/blob/main/db/schema.rb  --format schemarb

In most cases, we will receive a GitHub URL.
In that case, convert it to a RawContent URL such as:

https://raw.githubusercontent.com/someapp/someapp/main/db/schema.rb

Related Issue

Changes

Testing

Other Information

@sasamuku sasamuku temporarily deployed to preview - erd-sample December 11, 2024 03:48 — with GitHub Actions Inactive
@sasamuku sasamuku marked this pull request as ready for review December 11, 2024 03:54
@sasamuku sasamuku requested a review from a team as a code owner December 11, 2024 03:54
@sasamuku sasamuku requested review from hoshinotsuyoshi, FunamaYukina, junkisai and MH4GF and removed request for a team December 11, 2024 03:54
Copy link
Member

@MH4GF MH4GF left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍🏻

Comment on lines +4 to +11
function isValidUrl(url: string): boolean {
try {
new URL(url)
return true
} catch {
return false
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💭 I thought URL.canParse() could be used, but it looks like this was only supported in Node.js in v19.
We have not considered the minimum version of Node.js to be supported by the CLI, but since the LTS is v18, it would be better to support that.
So it looks good as it is 👍🏻

ref: https://developer.mozilla.org/en-US/docs/Web/API/URL/canParse_static

Comment on lines +46 to +48
return isGitHubFileUrl(inputPath)
? await downloadGitHubRawContent(inputPath)
: await downloadFile(inputPath)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only GitHub gets special handling, I see.

@MH4GF MH4GF added this pull request to the merge queue Dec 11, 2024
Comment on lines +13 to +49
function isGitHubFileUrl(url: string): boolean {
const parsedUrl = new URL(url)
return parsedUrl.hostname === 'github.com' && url.includes('/blob/')
}

function readLocalFile(filePath: string): string {
if (!fs.existsSync(filePath)) {
throw new Error('Invalid input path. Please provide a valid file.')
}
return fs.readFileSync(filePath, 'utf8')
}

async function downloadGitHubRawContent(githubUrl: string): Promise<string> {
const rawFileUrl = githubUrl
.replace('github.com', 'raw.githubusercontent.com')
.replace('/blob', '')
return await downloadFile(rawFileUrl)
}

async function downloadFile(url: string): Promise<string> {
const response = await fetch(url)
if (!response.ok) {
throw new Error(`Failed to download file: ${response.statusText}`)
}
const data = await response.text()
return data
}

export async function getInputContent(inputPath: string): Promise<string> {
if (!isValidUrl(inputPath)) {
return readLocalFile(inputPath)
}

return isGitHubFileUrl(inputPath)
? await downloadGitHubRawContent(inputPath)
: await downloadFile(inputPath)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that the CLI is handling quite a lot under the --input argument, which might be somewhat unexpected for users.

Perhaps generating the raw URL could be left as the user's responsibility. I’d like to hear others’ opinions on this as well.

Example:

@liam-hq/cli erd build --input https://raw.githubusercontent.com/mastodon/mastodon/refs/heads/main/db/schema.rb --format schemarb

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps generating the raw URL should be left to the user.

For private repositories, GitHub's raw URL appears to include a pre-signed token as a ?token= parameter.

Therefore, this URL conversion feature would only support public repositories. But it is ok, I think now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly, I was wondering if i should convert url.
If it is not useful and understandable for user, let's change implementation👍

Yeah, only public repo is available for now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO: I thought slightly extensive too, but it was a minor point of contention, so once merged, it looked good.

@MH4GF MH4GF removed this pull request from the merge queue due to a manual request Dec 11, 2024
Copy link
Member

@hoshinotsuyoshi hoshinotsuyoshi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. This is fine. While the functionality feels slightly extensive, it’s still perfectly reasonable.

@hoshinotsuyoshi hoshinotsuyoshi added this pull request to the merge queue Dec 11, 2024
Merged via the queue into main with commit 23cf7c9 Dec 11, 2024
13 checks passed
@hoshinotsuyoshi hoshinotsuyoshi deleted the support_input_url_option_cli branch December 11, 2024 04:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants