This repository was archived by the owner on Dec 29, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathgclone.go
More file actions
168 lines (138 loc) · 3.91 KB
/
Copy pathgclone.go
File metadata and controls
168 lines (138 loc) · 3.91 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
// Config holds the user configuration
type Config struct {
DefaultUsername string `json:"default_username"`
TmpDirectory string `json:"tmp_directory"`
}
func getConfigPath() string {
homeDir, err := os.UserHomeDir()
if err != nil {
fmt.Printf("Error getting home directory: %v\n", err)
os.Exit(1)
}
return filepath.Join(homeDir, ".gclone", "config.json")
}
func loadConfig() Config {
configPath := getConfigPath()
config := Config{
DefaultUsername: "",
TmpDirectory: filepath.Join("C:", "Dev", "tmp"),
}
// Try to read existing config
data, err := os.ReadFile(configPath)
if err == nil {
err = json.Unmarshal(data, &config)
if err != nil {
fmt.Printf("Error parsing config file: %v\n", err)
os.Exit(1)
}
}
return config
}
func saveConfig(config Config) error {
configPath := getConfigPath()
configDir := filepath.Dir(configPath)
// Create config directory if it doesn't exist
if err := os.MkdirAll(configDir, 0755); err != nil {
return fmt.Errorf("error creating config directory: %v", err)
}
// Save config
data, err := json.MarshalIndent(config, "", " ")
if err != nil {
return fmt.Errorf("error marshaling config: %v", err)
}
if err := os.WriteFile(configPath, data, 0644); err != nil {
return fmt.Errorf("error writing config file: %v", err)
}
return nil
}
func setupConfig() {
config := loadConfig()
fmt.Print("Enter your default GitHub username: ")
fmt.Scanln(&config.DefaultUsername)
fmt.Printf("Enter your temporary directory path (press Enter for default '%s'): ", config.TmpDirectory)
var tmpDir string
fmt.Scanln(&tmpDir)
if tmpDir != "" {
config.TmpDirectory = tmpDir
}
if err := saveConfig(config); err != nil {
fmt.Printf("Error saving config: %v\n", err)
os.Exit(1)
}
fmt.Println("Configuration saved successfully!")
}
func main() {
// Define flags
openInVSCode := flag.Bool("c", false, "Open the cloned repository in VSCode")
useTmpDir := flag.Bool("tmp", false, "Clone into temporary directory")
configureFlag := flag.Bool("config", false, "Configure gclone settings")
flag.Parse()
// Handle configuration
if *configureFlag {
setupConfig()
return
}
// Load configuration
config := loadConfig()
// Check if username is configured
if config.DefaultUsername == "" {
fmt.Println("gclone is not configured. Please run 'gclone -config' first.")
os.Exit(1)
}
// Check if repository name is provided
args := flag.Args()
if len(args) < 1 {
fmt.Println("Usage: gclone [-c] [-tmp] repository-name")
fmt.Println(" gclone -config (to configure settings)")
os.Exit(1)
}
repoName := args[0]
// If only one word is provided (no slash), prepend default username
if !strings.Contains(repoName, "/") {
repoName = config.DefaultUsername + "/" + repoName
}
// Construct GitHub URL
githubURL := fmt.Sprintf("https://github.com/%s", repoName)
// Determine clone directory
cloneDir := "."
if *useTmpDir {
cloneDir = config.TmpDirectory
// Create tmp directory if it doesn't exist
if err := os.MkdirAll(cloneDir, 0755); err != nil {
fmt.Printf("Error creating tmp directory: %v\n", err)
os.Exit(1)
}
}
// Extract the final directory name from the repository
finalDirName := filepath.Join(cloneDir, strings.Split(repoName, "/")[len(strings.Split(repoName, "/"))-1])
// Execute git clone
cmd := exec.Command("git", "clone", githubURL)
cmd.Dir = cloneDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
fmt.Printf("Cloning %s...\n", githubURL)
if err := cmd.Run(); err != nil {
fmt.Printf("Error cloning repository: %v\n", err)
os.Exit(1)
}
// Open in VSCode if requested
if *openInVSCode {
codeCmd := exec.Command("code", finalDirName)
if err := codeCmd.Run(); err != nil {
fmt.Printf("Error opening VSCode: %v\n", err)
os.Exit(1)
}
fmt.Println("Opened in VSCode")
}
fmt.Println("Clone completed successfully!")
}