|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "bytes" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "path/filepath" |
| 11 | + "strings" |
| 12 | +) |
| 13 | + |
| 14 | +type Module struct { |
| 15 | + Path string |
| 16 | + Version string |
| 17 | + Update *ModuleUpdate |
| 18 | +} |
| 19 | + |
| 20 | +type ModuleUpdate struct { |
| 21 | + Version string |
| 22 | +} |
| 23 | + |
| 24 | +func main() { |
| 25 | + if len(os.Args) < 2 { |
| 26 | + printUsage() |
| 27 | + os.Exit(1) |
| 28 | + } |
| 29 | + |
| 30 | + var checkOnly bool |
| 31 | + var discover bool |
| 32 | + var targetPath string |
| 33 | + |
| 34 | + for _, arg := range os.Args[1:] { |
| 35 | + switch arg { |
| 36 | + case "--check": |
| 37 | + checkOnly = true |
| 38 | + case "--update": |
| 39 | + checkOnly = false |
| 40 | + case "--discover": |
| 41 | + discover = true |
| 42 | + case "--help", "-h": |
| 43 | + printUsage() |
| 44 | + os.Exit(0) |
| 45 | + default: |
| 46 | + if !strings.HasPrefix(arg, "--") { |
| 47 | + targetPath = arg |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // Validate arguments |
| 53 | + if !discover && targetPath == "" { |
| 54 | + fmt.Fprintf(os.Stderr, "Error: must specify a path or use --discover\n") |
| 55 | + printUsage() |
| 56 | + os.Exit(1) |
| 57 | + } |
| 58 | + |
| 59 | + if discover && targetPath != "" { |
| 60 | + fmt.Fprintf(os.Stderr, "Error: cannot use both --discover and a specific path\n") |
| 61 | + printUsage() |
| 62 | + os.Exit(1) |
| 63 | + } |
| 64 | + |
| 65 | + var projects []string |
| 66 | + var err error |
| 67 | + |
| 68 | + if discover { |
| 69 | + projects, err = discoverProjects(".") |
| 70 | + if err != nil { |
| 71 | + fmt.Fprintf(os.Stderr, "Error discovering projects: %v\n", err) |
| 72 | + os.Exit(1) |
| 73 | + } |
| 74 | + if len(projects) == 0 { |
| 75 | + fmt.Println("No Go projects found.") |
| 76 | + os.Exit(0) |
| 77 | + } |
| 78 | + fmt.Printf("🔍 Discovered %d Go projects\n\n", len(projects)) |
| 79 | + } else { |
| 80 | + // Verify the path exists and has a go.mod |
| 81 | + goModPath := filepath.Join(targetPath, "go.mod") |
| 82 | + if _, err := os.Stat(goModPath); os.IsNotExist(err) { |
| 83 | + fmt.Fprintf(os.Stderr, "Error: no go.mod found in %s\n", targetPath) |
| 84 | + os.Exit(1) |
| 85 | + } |
| 86 | + projects = []string{targetPath} |
| 87 | + } |
| 88 | + |
| 89 | + hasUpdates := false |
| 90 | + allUpdates := make(map[string][]Module) |
| 91 | + |
| 92 | + for _, project := range projects { |
| 93 | + updates, err := checkProject(project) |
| 94 | + if err != nil { |
| 95 | + fmt.Fprintf(os.Stderr, "Error checking %s: %v\n", project, err) |
| 96 | + os.Exit(1) |
| 97 | + } |
| 98 | + if len(updates) > 0 { |
| 99 | + hasUpdates = true |
| 100 | + allUpdates[project] = updates |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if !hasUpdates { |
| 105 | + fmt.Println("✅ All dependencies are up to date.") |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + // Print summary of updates needed |
| 110 | + fmt.Println("📦 Dependencies with updates available:") |
| 111 | + for project, updates := range allUpdates { |
| 112 | + fmt.Printf("\n 📁 %s:\n", project) |
| 113 | + for _, mod := range updates { |
| 114 | + fmt.Printf(" - %s: %s → %s\n", mod.Path, mod.Version, mod.Update.Version) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if checkOnly { |
| 119 | + fmt.Println("\n❌ Please update dependencies before merging.") |
| 120 | + os.Exit(1) |
| 121 | + } |
| 122 | + |
| 123 | + // Update mode - apply updates |
| 124 | + fmt.Println("\n🔄 Updating dependencies...") |
| 125 | + for project, updates := range allUpdates { |
| 126 | + fmt.Printf("\n 📁 %s:\n", project) |
| 127 | + if err := updateProject(project, updates); err != nil { |
| 128 | + fmt.Fprintf(os.Stderr, "Error updating %s: %v\n", project, err) |
| 129 | + os.Exit(1) |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + fmt.Println("\n✅ All dependencies updated successfully.") |
| 134 | +} |
| 135 | + |
| 136 | +func printUsage() { |
| 137 | + fmt.Println(`Usage: golang-updater [--check|--update] [--discover|<path>] |
| 138 | +
|
| 139 | +Modes: |
| 140 | + --check Check for outdated dependencies (exit 1 if found) |
| 141 | + --update Update outdated dependencies (default) |
| 142 | +
|
| 143 | +Target: |
| 144 | + --discover Discover all Go projects from current directory |
| 145 | + <path> Path to a specific Go project |
| 146 | +
|
| 147 | +Examples: |
| 148 | + golang-updater --check ./installer |
| 149 | + golang-updater --update ./installer |
| 150 | + golang-updater --check --discover |
| 151 | + golang-updater --update --discover`) |
| 152 | +} |
| 153 | + |
| 154 | +func discoverProjects(root string) ([]string, error) { |
| 155 | + var projects []string |
| 156 | + |
| 157 | + err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error { |
| 158 | + if err != nil { |
| 159 | + return err |
| 160 | + } |
| 161 | + |
| 162 | + // Skip hidden directories and common non-project directories |
| 163 | + if info.IsDir() { |
| 164 | + name := info.Name() |
| 165 | + // Don't skip the root directory itself |
| 166 | + if path != root && (strings.HasPrefix(name, ".") || name == "vendor" || name == "node_modules") { |
| 167 | + return filepath.SkipDir |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + if info.Name() == "go.mod" { |
| 172 | + dir := filepath.Dir(path) |
| 173 | + projects = append(projects, dir) |
| 174 | + } |
| 175 | + |
| 176 | + return nil |
| 177 | + }) |
| 178 | + |
| 179 | + return projects, err |
| 180 | +} |
| 181 | + |
| 182 | +func checkProject(projectPath string) ([]Module, error) { |
| 183 | + goModPath := filepath.Join(projectPath, "go.mod") |
| 184 | + modFile, err := os.Open(goModPath) |
| 185 | + if err != nil { |
| 186 | + return nil, fmt.Errorf("error opening go.mod: %w", err) |
| 187 | + } |
| 188 | + defer modFile.Close() |
| 189 | + |
| 190 | + explicitModules := make(map[string]bool) |
| 191 | + scanner := bufio.NewScanner(modFile) |
| 192 | + for scanner.Scan() { |
| 193 | + line := strings.TrimSpace(scanner.Text()) |
| 194 | + if strings.HasPrefix(line, "require") || strings.HasPrefix(line, ")") { |
| 195 | + continue |
| 196 | + } |
| 197 | + fields := strings.Fields(line) |
| 198 | + if len(fields) >= 1 && !strings.HasPrefix(fields[0], "//") { |
| 199 | + explicitModules[fields[0]] = true |
| 200 | + } |
| 201 | + } |
| 202 | + if err := scanner.Err(); err != nil { |
| 203 | + return nil, fmt.Errorf("error reading go.mod: %w", err) |
| 204 | + } |
| 205 | + |
| 206 | + cmd := exec.Command("go", "list", "-u", "-m", "-json", "all") |
| 207 | + cmd.Dir = projectPath |
| 208 | + output, err := cmd.Output() |
| 209 | + if err != nil { |
| 210 | + return nil, fmt.Errorf("error executing go list: %w", err) |
| 211 | + } |
| 212 | + |
| 213 | + decoder := json.NewDecoder(bytes.NewReader(output)) |
| 214 | + var toUpdate []Module |
| 215 | + |
| 216 | + for decoder.More() { |
| 217 | + var mod Module |
| 218 | + if err := decoder.Decode(&mod); err != nil { |
| 219 | + return nil, fmt.Errorf("error parsing JSON output: %w", err) |
| 220 | + } |
| 221 | + if mod.Update != nil && explicitModules[mod.Path] { |
| 222 | + toUpdate = append(toUpdate, mod) |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + return toUpdate, nil |
| 227 | +} |
| 228 | + |
| 229 | +func updateProject(projectPath string, updates []Module) error { |
| 230 | + for _, mod := range updates { |
| 231 | + updateStr := fmt.Sprintf("%s@%s", mod.Path, mod.Update.Version) |
| 232 | + fmt.Printf(" 🔄 Updating %s\n", updateStr) |
| 233 | + cmd := exec.Command("go", "get", updateStr) |
| 234 | + cmd.Dir = projectPath |
| 235 | + cmd.Stdout = os.Stdout |
| 236 | + cmd.Stderr = os.Stderr |
| 237 | + if err := cmd.Run(); err != nil { |
| 238 | + return fmt.Errorf("error updating %s: %w", updateStr, err) |
| 239 | + } |
| 240 | + } |
| 241 | + |
| 242 | + fmt.Printf(" 🧹 Running go mod tidy...\n") |
| 243 | + cmd := exec.Command("go", "mod", "tidy") |
| 244 | + cmd.Dir = projectPath |
| 245 | + cmd.Stdout = os.Stdout |
| 246 | + cmd.Stderr = os.Stderr |
| 247 | + if err := cmd.Run(); err != nil { |
| 248 | + return fmt.Errorf("error running go mod tidy: %w", err) |
| 249 | + } |
| 250 | + |
| 251 | + return nil |
| 252 | +} |
0 commit comments