-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathroot.go
More file actions
127 lines (109 loc) · 3.56 KB
/
Copy pathroot.go
File metadata and controls
127 lines (109 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package cli
import (
"errors"
"fmt"
"path/filepath"
"strings"
"github.com/dagimg-dot/gitsnip/internal/app"
"github.com/dagimg-dot/gitsnip/internal/app/model"
apperrors "github.com/dagimg-dot/gitsnip/internal/errors"
"github.com/spf13/cobra"
)
var (
branch string
method string
token string
provider string
quiet bool
rootCmd = &cobra.Command{
Use: "gitsnip <repository_url> <folder_path> [output_dir]",
Short: "Download a specific folder from a Git repository (GitHub)",
Long: `Gitsnip allows you to download a specific folder from a remote Git
repository without cloning the entire repository.
Arguments:
repository_url: URL of the GitHub repository (e.g., https://github.com/user/repo)
folder_path: Path to the folder within the repository you want to download.
output_dir: Optional. Directory where the folder should be saved.
Defaults to the folder's base name in the current directory.`,
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
cmd.Help()
return nil
}
return nil
},
Args: cobra.RangeArgs(0, 3),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return nil
}
if len(args) < 2 {
return fmt.Errorf("requires at least repository_url and folder_path arguments")
}
repoURL := args[0]
folderPath := args[1]
outputDir := "" // default
if len(args) == 3 {
outputDir = args[2]
} else {
outputDir = filepath.Base(folderPath)
}
// Normalize folder path to use forward slashes
folderPath = strings.ReplaceAll(folderPath, "\\", "/")
if provider == "" {
if strings.Contains(repoURL, "github.com") {
provider = "github"
} else {
provider = "github"
}
}
methodType := model.MethodTypeSparse
if method == "api" {
methodType = model.MethodTypeAPI
}
providerType := model.ProviderTypeGitHub
// TODO: add other providers when supported
opts := model.DownloadOptions{
RepoURL: repoURL,
Subdir: folderPath,
OutputDir: outputDir,
Branch: branch,
Token: token,
Method: methodType,
Provider: providerType,
Quiet: quiet,
}
if !quiet {
fmt.Printf("Repository URL: %s\n", repoURL)
fmt.Printf("Folder Path: %s\n", folderPath)
fmt.Printf("Target Branch: %s\n", branch)
fmt.Printf("Download Method: %s\n", method)
fmt.Printf("Output Dir: %s\n", outputDir)
fmt.Printf("Provider: %s\n", provider)
fmt.Println("--------------------------------")
}
err := app.Download(opts)
var appErr *apperrors.AppError
if errors.As(err, &appErr) {
cmd.SilenceUsage = true
}
return err
},
}
)
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main().
func Execute() error {
rootCmd.SilenceErrors = true
rootCmd.SilenceUsage = false
return rootCmd.Execute()
}
// init is called by Go before main()
func init() {
// TODO: use PersistentFlags if i want flags to be available to subcommands as well
rootCmd.Flags().StringVarP(&branch, "branch", "b", "main", "Repository branch to download from")
rootCmd.Flags().StringVarP(&method, "method", "m", "sparse", "Download method ('api' or 'sparse')")
rootCmd.Flags().StringVarP(&token, "token", "t", "", "GitHub API token for private repositories or increased rate limits")
rootCmd.Flags().StringVarP(&provider, "provider", "p", "", "Repository provider ('github', more to come)")
rootCmd.Flags().BoolVarP(&quiet, "quiet", "q", false, "Suppress progress output during download")
}