|
1 | | -const fs = require('fs'); |
2 | | -const path = require('path'); |
3 | | -const fetch = require('node-fetch'); |
4 | | -const { execSync } = require('child_process'); |
| 1 | +package main |
5 | 2 |
|
6 | | -const args = process.argv.slice(2); |
7 | | -const action = args[0]; |
8 | | -const libDir = path.join(process.env.HOME, '.hacker-lang', 'libs'); |
9 | | -const packageListUrl = 'https://github.com/Bytes-Repository/bytes.io/blob/main/repository/bytes.io'; |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/url" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | +) |
10 | 11 |
|
11 | | -if (!fs.existsSync(libDir)) { |
12 | | - fs.mkdirSync(libDir, { recursive: true }); |
13 | | -} |
| 12 | +func main() { |
| 13 | + args := os.Args[1:] |
| 14 | + if len(args) == 0 { |
| 15 | + usage() |
| 16 | + } |
| 17 | + action := args[0] |
| 18 | + home := os.Getenv("HOME") |
| 19 | + if home == "" { |
| 20 | + fmt.Println("Error: HOME environment variable not set") |
| 21 | + os.Exit(1) |
| 22 | + } |
| 23 | + libDir := filepath.Join(home, ".hackeros", "hacker-lang", "libs") |
| 24 | + err := os.MkdirAll(libDir, 0755) |
| 25 | + if err != nil { |
| 26 | + fmt.Printf("Error creating library directory: %v\n", err) |
| 27 | + os.Exit(1) |
| 28 | + } |
| 29 | + packageListUrl := "https://raw.githubusercontent.com/Bytes-Repository/bytes.io/main/repository/bytes.io" |
| 30 | + tmpList := "/tmp/bytes.io" |
| 31 | + |
| 32 | + downloadList := func() error { |
| 33 | + cmd := exec.Command("curl", "-s", "-o", tmpList, packageListUrl) |
| 34 | + return cmd.Run() |
| 35 | + } |
| 36 | + |
| 37 | + parsePackages := func() map[string]string { |
| 38 | + packages := make(map[string]string) |
| 39 | + data, err := os.ReadFile(tmpList) |
| 40 | + if err != nil { |
| 41 | + fmt.Printf("Error reading package list: %v\n", err) |
| 42 | + os.Exit(1) |
| 43 | + } |
| 44 | + lines := strings.Split(string(data), "\n") |
| 45 | + for _, line := range lines { |
| 46 | + trimmed := strings.TrimSpace(line) |
| 47 | + if strings.Contains(trimmed, "=>") { |
| 48 | + parts := strings.SplitN(trimmed, "=>", 2) |
| 49 | + if len(parts) == 2 { |
| 50 | + name := strings.TrimSpace(parts[0]) |
| 51 | + u := strings.TrimSpace(parts[1]) |
| 52 | + if name != "" && u != "" { |
| 53 | + packages[name] = u |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + return packages |
| 59 | + } |
| 60 | + |
| 61 | + getPackages := func() map[string]string { |
| 62 | + err := downloadList() |
| 63 | + if err != nil { |
| 64 | + fmt.Printf("Error downloading package list: %v\n", err) |
| 65 | + os.Exit(1) |
| 66 | + } |
| 67 | + return parsePackages() |
| 68 | + } |
| 69 | + |
| 70 | + getFilename := func(urlStr string) string { |
| 71 | + u, err := url.Parse(urlStr) |
| 72 | + if err != nil { |
| 73 | + fmt.Printf("Error parsing URL: %v\n", err) |
| 74 | + os.Exit(1) |
| 75 | + } |
| 76 | + return filepath.Base(u.Path) |
| 77 | + } |
| 78 | + |
| 79 | + getInstalled := func(packages map[string]string, libDir string) []string { |
| 80 | + var installed []string |
| 81 | + for name, urlStr := range packages { |
| 82 | + filename := getFilename(urlStr) |
| 83 | + libPath := filepath.Join(libDir, filename) |
| 84 | + if _, err := os.Stat(libPath); err == nil { |
| 85 | + installed = append(installed, name) |
| 86 | + } |
| 87 | + } |
| 88 | + return installed |
| 89 | + } |
14 | 90 |
|
15 | | -async function fetchPackageList() { |
16 | | - try { |
17 | | - const response = await fetch(packageListUrl); |
18 | | - if (!response.ok) throw new Error(`HTTP ${response.status}`); |
19 | | - const text = await response.text(); |
20 | | - const lines = text.split('\n'); |
21 | | - const packages = {}; |
22 | | - let in_config = false; |
23 | | - for (const line of lines) { |
24 | | - const trimmed = line.trim(); |
25 | | - if (trimmed === '[') { |
26 | | - in_config = true; |
27 | | - continue; |
28 | | - } |
29 | | - if (trimmed === ']') { |
30 | | - in_config = false; |
31 | | - continue; |
32 | | - } |
33 | | - if (in_config && trimmed.includes('=>')) { |
34 | | - const [name, url] = trimmed.split('=>').map(s => s.trim()); |
35 | | - if (name && url) { |
36 | | - packages[name] = url; |
37 | | - } |
38 | | - } |
39 | | - } |
40 | | - return packages; |
41 | | - } catch (err) { |
42 | | - console.error(`Error fetching package list: ${err.message}`); |
43 | | - process.exit(1); |
44 | | - } |
| 91 | + if action == "list" { |
| 92 | + fmt.Println("Fetching available libraries...") |
| 93 | + packages := getPackages() |
| 94 | + fmt.Println("Available libraries:") |
| 95 | + for name := range packages { |
| 96 | + fmt.Printf("- %s\n", name) |
| 97 | + } |
| 98 | + fmt.Println("\nInstalled libraries:") |
| 99 | + installed := getInstalled(packages, libDir) |
| 100 | + for _, name := range installed { |
| 101 | + fmt.Printf("- %s\n", name) |
| 102 | + } |
| 103 | + } else if action == "install" { |
| 104 | + if len(args) < 2 { |
| 105 | + usage() |
| 106 | + } |
| 107 | + libname := args[1] |
| 108 | + packages := getPackages() |
| 109 | + urlStr, ok := packages[libname] |
| 110 | + if !ok { |
| 111 | + fmt.Printf("Library %s not found in package list.\n", libname) |
| 112 | + os.Exit(1) |
| 113 | + } |
| 114 | + filename := getFilename(urlStr) |
| 115 | + libPath := filepath.Join(libDir, filename) |
| 116 | + tmpPath := filepath.Join("/tmp", filename) |
| 117 | + if _, err := os.Stat(libPath); err == nil { |
| 118 | + fmt.Printf("Removing existing %s...\n", libname) |
| 119 | + err := os.Remove(libPath) |
| 120 | + if err != nil { |
| 121 | + fmt.Printf("Error removing existing library: %v\n", err) |
| 122 | + os.Exit(1) |
| 123 | + } |
| 124 | + } |
| 125 | + fmt.Printf("Installing %s from %s...\n", libname, urlStr) |
| 126 | + cmd := exec.Command("curl", "-L", "-o", tmpPath, urlStr) |
| 127 | + err = cmd.Run() |
| 128 | + if err != nil { |
| 129 | + fmt.Printf("Error downloading library: %v\n", err) |
| 130 | + os.Exit(1) |
| 131 | + } |
| 132 | + err = os.Rename(tmpPath, libPath) |
| 133 | + if err != nil { |
| 134 | + fmt.Printf("Error moving library: %v\n", err) |
| 135 | + os.Exit(1) |
| 136 | + } |
| 137 | + cmd = exec.Command("chmod", "+x", libPath) |
| 138 | + err = cmd.Run() |
| 139 | + if err != nil { |
| 140 | + fmt.Printf("Warning: Error making library executable: %v\n", err) |
| 141 | + } |
| 142 | + fmt.Printf("Installed %s to %s\n", libname, libPath) |
| 143 | + } else if action == "update" { |
| 144 | + fmt.Println("Checking for library updates...") |
| 145 | + packages := getPackages() |
| 146 | + installed := getInstalled(packages, libDir) |
| 147 | + for _, lib := range installed { |
| 148 | + urlStr := packages[lib] |
| 149 | + filename := getFilename(urlStr) |
| 150 | + libPath := filepath.Join(libDir, filename) |
| 151 | + tmpPath := filepath.Join("/tmp", filename) |
| 152 | + fmt.Printf("Updating %s...\n", lib) |
| 153 | + err := os.Remove(libPath) |
| 154 | + if err != nil { |
| 155 | + fmt.Printf("Error removing old library: %v\n", err) |
| 156 | + continue |
| 157 | + } |
| 158 | + cmd := exec.Command("curl", "-L", "-o", tmpPath, urlStr) |
| 159 | + err = cmd.Run() |
| 160 | + if err != nil { |
| 161 | + fmt.Printf("Error downloading update: %v\n", err) |
| 162 | + continue |
| 163 | + } |
| 164 | + err = os.Rename(tmpPath, libPath) |
| 165 | + if err != nil { |
| 166 | + fmt.Printf("Error moving update: %v\n", err) |
| 167 | + continue |
| 168 | + } |
| 169 | + cmd = exec.Command("chmod", "+x", libPath) |
| 170 | + err = cmd.Run() |
| 171 | + if err != nil { |
| 172 | + fmt.Printf("Warning: Error making library executable: %v\n", err) |
| 173 | + } |
| 174 | + fmt.Printf("%s updated\n", lib) |
| 175 | + } |
| 176 | + } else { |
| 177 | + usage() |
| 178 | + } |
45 | 179 | } |
46 | 180 |
|
47 | | -if (action === 'list') { |
48 | | - console.log('Fetching available libraries...'); |
49 | | - fetchPackageList().then(packages => { |
50 | | - console.log('Available libraries:'); |
51 | | - Object.keys(packages).forEach(lib => console.log(`- ${lib}`)); |
52 | | - const installed = fs.existsSync(libDir) ? fs.readdirSync(libDir).filter(f => fs.lstatSync(path.join(libDir, f)).isDirectory()) : []; |
53 | | - console.log('\nInstalled libraries:'); |
54 | | - installed.forEach(lib => console.log(`- ${lib}`)); |
55 | | - }); |
56 | | -} else if (action === 'install') { |
57 | | - const libname = args[1]; |
58 | | - if (!libname) { |
59 | | - console.error('Usage: hacker-library install <libname>'); |
60 | | - process.exit(1); |
61 | | - } |
62 | | - fetchPackageList().then(packages => { |
63 | | - if (!packages[libname]) { |
64 | | - console.error(`Library ${libname} not found in package list.`); |
65 | | - process.exit(1); |
66 | | - } |
67 | | - const repoUrl = packages[libname]; |
68 | | - const libPath = path.join(libDir, libname); |
69 | | - console.log(`Installing ${libname} from ${repoUrl}...`); |
70 | | - try { |
71 | | - if (fs.existsSync(libPath)) { |
72 | | - console.log(`Removing existing ${libname}...`); |
73 | | - execSync(`rm -rf ${libPath}`); |
74 | | - } |
75 | | - execSync(`git clone ${repoUrl} ${libPath}`, { stdio: 'inherit' }); |
76 | | - if (!fs.existsSync(path.join(libPath, 'main.hacker'))) { |
77 | | - console.error(`Library ${libname} missing main.hacker`); |
78 | | - execSync(`rm -rf ${libPath}`); |
79 | | - process.exit(1); |
80 | | - } |
81 | | - console.log(`Installed ${libname} to ${libPath}`); |
82 | | - } catch (err) { |
83 | | - console.error(`Error installing ${libname}: ${err.message}`); |
84 | | - process.exit(1); |
85 | | - } |
86 | | - }); |
87 | | -} else if (action === 'update') { |
88 | | - console.log('Checking for library updates...'); |
89 | | - const installed = fs.existsSync(libDir) ? fs.readdirSync(libDir).filter(f => fs.lstatSync(path.join(libDir, f)).isDirectory()) : []; |
90 | | - fetchPackageList().then(packages => { |
91 | | - installed.forEach(lib => { |
92 | | - if (packages[lib]) { |
93 | | - const libPath = path.join(libDir, lib); |
94 | | - console.log(`Updating ${lib}...`); |
95 | | - try { |
96 | | - execSync(`cd ${libPath} && git pull`, { stdio: 'inherit' }); |
97 | | - console.log(`${lib} updated`); |
98 | | - } catch (err) { |
99 | | - console.error(`Error updating ${lib}: ${err.message}`); |
100 | | - } |
101 | | - } else { |
102 | | - console.log(`${lib}: No update info available`); |
103 | | - } |
104 | | - }); |
105 | | - }); |
106 | | -} else { |
107 | | - console.error('Usage: hacker-library [list|install|update] [libname]'); |
108 | | - process.exit(1); |
| 181 | +func usage() { |
| 182 | + fmt.Println("Usage: hacker-library [list|install|update] [libname]") |
| 183 | + os.Exit(1) |
109 | 184 | } |
0 commit comments