Skip to content

Commit

Permalink
Accept partial urls for github2file
Browse files Browse the repository at this point in the history
  • Loading branch information
gusanmaz committed Jul 25, 2024
1 parent 0cb95c0 commit c9ed39c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
15 changes: 10 additions & 5 deletions cmd/github2file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package cmd

import (
"fmt"
ignore "github.com/sabhiram/go-gitignore"
"os"
"path/filepath"
"strings"

"github.com/gusanmaz/onefile/utils"
ignore "github.com/sabhiram/go-gitignore"
"github.com/spf13/cobra"
)

Expand All @@ -20,10 +20,15 @@ func NewGitHub2FileCmd() *cobra.Command {
Short: "Fetch a GitHub repository and save as JSON or Markdown",
Long: `Fetch a GitHub repository and save its structure and contents as JSON or Markdown.
Exclude patterns can be specified directly or by referencing a file with @.
Example: -e "*.go @.gitignore" -e "utils/extension_language_map.json go.mod go.sum"`,
Example: -e "*.go @.gitignore" -e "utils/extension_language_map.json go.mod go.sum"
The repository URL can be specified in various formats:
- Full URL: https://github.com/username/repo
- Without protocol: github.com/username/repo
- Short form: username/repo`,
Run: func(cmd *cobra.Command, args []string) {
if repoURL == "" && !allRepos {
fmt.Println("Please provide a GitHub URL or use the -a flag")
fmt.Println("Please provide a GitHub repository URL or use the -a flag")
return
}

Expand Down Expand Up @@ -56,15 +61,15 @@ Example: -e "*.go @.gitignore" -e "utils/extension_language_map.json go.mod go.s
}

for _, repo := range repos {
fetchAndSaveRepo(fmt.Sprintf("https://github.com/%s/%s", owner, repo.Name), outputType, outputDir, outputName, gitIgnore, useGit, githubToken, includeGit, includeNonText, showExcluded)
fetchAndSaveRepo(fmt.Sprintf("%s/%s", owner, repo.Name), outputType, outputDir, outputName, gitIgnore, useGit, githubToken, includeGit, includeNonText, showExcluded)
}
} else {
fetchAndSaveRepo(repoURL, outputType, outputDir, outputName, gitIgnore, useGit, githubToken, includeGit, includeNonText, showExcluded)
}
},
}

cmd.Flags().StringVarP(&repoURL, "url", "u", "", "GitHub repository URL")
cmd.Flags().StringVarP(&repoURL, "url", "u", "", "GitHub repository URL or shorthand (e.g., username/repo)")
cmd.Flags().StringVarP(&outputType, "type", "t", "md", "Output type: json or md")
cmd.Flags().StringVarP(&outputDir, "output-dir", "d", ".", "Output directory")
cmd.Flags().StringVarP(&outputName, "output-name", "n", "", "Output file name (without extension)")
Expand Down
32 changes: 21 additions & 11 deletions utils/github_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,29 @@ func FetchUserRepos(username, githubToken string) ([]GithubRepo, error) {
}

func ParseGitHubURL(url string) (owner string, repo string, path string, err error) {
// Remove any leading "https://" or "http://"
url = strings.TrimPrefix(url, "https://")
url = strings.TrimPrefix(url, "http://")

// Remove any leading "github.com/"
url = strings.TrimPrefix(url, "github.com/")

parts := strings.Split(url, "/")
if len(parts) < 5 || parts[2] != "github.com" {
return "", "", "", fmt.Errorf("invalid GitHub URL: %s", url)
}
owner = parts[3]
repo = parts[4]
path = ""
if len(parts) > 5 {
if parts[5] == "tree" && len(parts) > 7 {
path = strings.Join(parts[7:], "/")
} else if parts[5] != "tree" {
path = strings.Join(parts[5:], "/")

if len(parts) < 2 {
return "", "", "", fmt.Errorf("invalid GitHub URL or repository format: %s", url)
}

owner = parts[0]
repo = parts[1]

if len(parts) > 2 {
if parts[2] == "tree" && len(parts) > 3 {
path = strings.Join(parts[4:], "/")
} else {
path = strings.Join(parts[2:], "/")
}
}

return owner, repo, path, nil
}

0 comments on commit c9ed39c

Please sign in to comment.