| 1 | // local build script file, similar to a makefile or collection of bash scripts in other projects |
| 2 | |
| 3 | package main |
| 4 | |
| 5 | import ( |
| 6 | "bufio" |
| 7 | "bytes" |
| 8 | "context" |
| 9 | "errors" |
| 10 | "fmt" |
| 11 | "io" |
| 12 | "log" |
| 13 | "math" |
| 14 | "net/http" |
| 15 | "net/url" |
| 16 | "os" |
| 17 | "os/exec" |
| 18 | "path/filepath" |
| 19 | "regexp" |
| 20 | "runtime" |
| 21 | "strings" |
| 22 | "time" |
| 23 | |
| 24 | "github.com/urfave/cli/v3" |
| 25 | ) |
| 26 | |
| 27 | const ( |
| 28 | badNewsEmoji = "🚨" |
| 29 | goodNewsEmoji = "✨" |
| 30 | checksPassedEmoji = "✅" |
| 31 | |
| 32 | gfmrunVersion = "v1.3.0" |
| 33 | |
| 34 | v3diffWarning = ` |
| 35 | # The unified diff above indicates that the public API surface area |
| 36 | # has changed. If you feel that the changes are acceptable for the |
| 37 | # v3.x series, please run the following command to promote the |
| 38 | # current go docs: |
| 39 | # |
| 40 | # make v3approve |
| 41 | # |
| 42 | ` |
| 43 | ) |
| 44 | |
| 45 | func main() { |
| 46 | topDir, err := func() (string, error) { |
| 47 | ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) |
| 48 | defer cancel() |
| 49 | |
| 50 | if v, err := sh(ctx, "git", "rev-parse", "--show-toplevel"); err == nil { |
| 51 | return strings.TrimSpace(v), nil |
| 52 | } |
| 53 | |
| 54 | return os.Getwd() |
| 55 | }() |
| 56 | if err != nil { |
| 57 | log.Fatal(err) |
| 58 | } |
| 59 | |
| 60 | app := &cli.Command{ |
| 61 | Name: "builder", |
| 62 | Usage: "Do a thing for urfave/cli! (maybe build?)", |
| 63 | Commands: []*cli.Command{ |
| 64 | { |
| 65 | Name: "vet", |
| 66 | Action: topRunAction("go", "vet", "./..."), |
| 67 | }, |
| 68 | { |
| 69 | Name: "test", |
| 70 | Action: TestActionFunc, |
| 71 | }, |
| 72 | { |
| 73 | Name: "gfmrun", |
| 74 | Flags: []cli.Flag{ |
| 75 | &cli.BoolFlag{ |
| 76 | Name: "walk", |
| 77 | Value: false, |
| 78 | Usage: "Walk the specified directory and perform validation on all markdown files", |
| 79 | }, |
| 80 | }, |
| 81 | Action: GfmrunActionFunc, |
| 82 | }, |
| 83 | { |
| 84 | Name: "check-binary-size", |
| 85 | Action: checkBinarySizeActionFunc, |
| 86 | }, |
| 87 | { |
| 88 | Name: "generate", |
| 89 | Action: GenerateActionFunc, |
| 90 | }, |
| 91 | { |
| 92 | Name: "diffcheck", |
| 93 | Action: DiffCheckActionFunc, |
| 94 | }, |
| 95 | { |
| 96 | Name: "ensure-goimports", |
| 97 | Action: EnsureGoimportsActionFunc, |
| 98 | }, |
| 99 | { |
| 100 | Name: "ensure-gfmrun", |
| 101 | Action: EnsureGfmrunActionFunc, |
| 102 | }, |
| 103 | { |
| 104 | Name: "ensure-mkdocs", |
| 105 | Action: EnsureMkdocsActionFunc, |
| 106 | Flags: []cli.Flag{ |
| 107 | &cli.BoolFlag{Name: "upgrade-pip"}, |
| 108 | }, |
| 109 | }, |
| 110 | { |
| 111 | Name: "set-mkdocs-remote", |
| 112 | Action: SetMkdocsRemoteActionFunc, |
| 113 | Flags: []cli.Flag{ |
| 114 | &cli.StringFlag{ |
| 115 | Name: "github-token", |
| 116 | Sources: cli.EnvVars("MKDOCS_REMOTE_GITHUB_TOKEN"), |
| 117 | Required: true, |
| 118 | }, |
| 119 | }, |
| 120 | }, |
| 121 | { |
| 122 | Name: "deploy-mkdocs", |
| 123 | Action: topRunAction("mkdocs", "gh-deploy", "--force"), |
| 124 | }, |
| 125 | { |
| 126 | Name: "lint", |
| 127 | Action: LintActionFunc, |
| 128 | }, |
| 129 | { |
| 130 | Name: "v3diff", |
| 131 | Flags: []cli.Flag{ |
| 132 | &cli.BoolFlag{Name: "color", Value: false}, |
| 133 | }, |
| 134 | Action: V3Diff, |
| 135 | }, |
| 136 | { |
| 137 | Name: "v3approve", |
| 138 | Action: topRunAction( |
| 139 | "cp", |
| 140 | "-v", |
| 141 | "godoc-current.txt", |
| 142 | filepath.Join("testdata", "godoc-v3.x.txt"), |
| 143 | ), |
| 144 | }, |
| 145 | }, |
| 146 | Flags: []cli.Flag{ |
| 147 | &cli.StringFlag{ |
| 148 | Name: "tags", |
| 149 | Usage: "set build tags", |
| 150 | }, |
| 151 | &cli.StringFlag{ |
| 152 | Name: "top-dir", |
| 153 | Value: topDir, |
| 154 | }, |
| 155 | &cli.StringSliceFlag{ |
| 156 | Name: "packages", |
| 157 | Value: []string{"cli", "scripts"}, |
| 158 | }, |
| 159 | }, |
| 160 | } |
| 161 | |
| 162 | if err := app.Run(context.Background(), os.Args); err != nil { |
| 163 | log.Fatal(err) |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | func sh(ctx context.Context, exe string, args ...string) (string, error) { |
| 168 | cmd := exec.CommandContext(ctx, exe, args...) |
| 169 | cmd.Stdin = os.Stdin |
| 170 | cmd.Stderr = os.Stderr |
| 171 | |
| 172 | fmt.Fprintf(os.Stderr, "# ---> %s\n", cmd) |
| 173 | outBytes, err := cmd.Output() |
| 174 | return string(outBytes), err |
| 175 | } |
| 176 | |
| 177 | func topRunAction(arg string, args ...string) cli.ActionFunc { |
| 178 | return func(ctx context.Context, cmd *cli.Command) error { |
| 179 | if err := os.Chdir(cmd.String("top-dir")); err != nil { |
| 180 | return err |
| 181 | } |
| 182 | |
| 183 | return runCmd(ctx, arg, args...) |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | func runCmd(ctx context.Context, arg string, args ...string) error { |
| 188 | cmd := exec.CommandContext(ctx, arg, args...) |
| 189 | |
| 190 | cmd.Stdin = os.Stdin |
| 191 | cmd.Stdout = os.Stdout |
| 192 | cmd.Stderr = os.Stderr |
| 193 | |
| 194 | fmt.Fprintf(os.Stderr, "# ---> %s\n", cmd) |
| 195 | return cmd.Run() |
| 196 | } |
| 197 | |
| 198 | func downloadFile(src, dest string, dirPerm, perm os.FileMode) error { |
| 199 | req, err := http.NewRequest(http.MethodGet, src, nil) |
| 200 | if err != nil { |
| 201 | return err |
| 202 | } |
| 203 | |
| 204 | resp, err := http.DefaultClient.Do(req) |
| 205 | if err != nil { |
| 206 | return err |
| 207 | } |
| 208 | |
| 209 | defer resp.Body.Close() |
| 210 | |
| 211 | if resp.StatusCode >= 300 { |
| 212 | return fmt.Errorf("download file from %[2]s into %[3]s: response %[1]v", resp.StatusCode, src, dest) |
| 213 | } |
| 214 | |
| 215 | if err := os.MkdirAll(filepath.Dir(dest), dirPerm); err != nil { |
| 216 | return err |
| 217 | } |
| 218 | |
| 219 | out, err := os.Create(dest) |
| 220 | if err != nil { |
| 221 | return err |
| 222 | } |
| 223 | |
| 224 | if _, err := io.Copy(out, resp.Body); err != nil { |
| 225 | return err |
| 226 | } |
| 227 | |
| 228 | if err := out.Close(); err != nil { |
| 229 | return err |
| 230 | } |
| 231 | |
| 232 | return os.Chmod(dest, perm) |
| 233 | } |
| 234 | |
| 235 | func VetActionFunc(ctx context.Context, cmd *cli.Command) error { |
| 236 | return runCmd(ctx, "go", "vet", cmd.String("top-dir")+"/...") |
| 237 | } |
| 238 | |
| 239 | func TestActionFunc(ctx context.Context, cmd *cli.Command) error { |
| 240 | tags := cmd.String("tags") |
| 241 | |
| 242 | for _, pkg := range cmd.StringSlice("packages") { |
| 243 | packageName := "github.com/urfave/cli/v3" |
| 244 | |
| 245 | if pkg != "cli" { |
| 246 | packageName = fmt.Sprintf("github.com/urfave/cli/v3/%s", pkg) |
| 247 | } |
| 248 | |
| 249 | args := []string{"test"} |
| 250 | if tags != "" { |
| 251 | args = append(args, []string{"-tags", tags}...) |
| 252 | } |
| 253 | |
| 254 | args = append(args, []string{ |
| 255 | "-v", |
| 256 | "-race", |
| 257 | "--coverprofile", pkg + ".coverprofile", |
| 258 | "--covermode", "atomic", |
| 259 | "--cover", packageName, |
| 260 | packageName, |
| 261 | }...) |
| 262 | |
| 263 | if err := runCmd(ctx, "go", args...); err != nil { |
| 264 | return err |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | return testCleanup(cmd.StringSlice("packages")) |
| 269 | } |
| 270 | |
| 271 | func testCleanup(packages []string) error { |
| 272 | out := &bytes.Buffer{} |
| 273 | |
| 274 | fmt.Fprintf(out, "mode: count\n") |
| 275 | |
| 276 | for _, pkg := range packages { |
| 277 | filename := pkg + ".coverprofile" |
| 278 | |
| 279 | lineBytes, err := os.ReadFile(filename) |
| 280 | if err != nil { |
| 281 | return err |
| 282 | } |
| 283 | |
| 284 | lines := strings.Split(string(lineBytes), "\n") |
| 285 | |
| 286 | fmt.Fprint(out, strings.Join(lines[1:], "\n")) |
| 287 | |
| 288 | if err := os.Remove(filename); err != nil { |
| 289 | return err |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | return os.WriteFile("coverage.txt", out.Bytes(), 0o644) |
| 294 | } |
| 295 | |
| 296 | func GfmrunActionFunc(ctx context.Context, cmd *cli.Command) error { |
| 297 | docsDir := filepath.Join(cmd.String("top-dir"), "docs") |
| 298 | |
| 299 | bash, err := exec.LookPath("bash") |
| 300 | if err != nil { |
| 301 | return err |
| 302 | } |
| 303 | |
| 304 | os.Setenv("SHELL", bash) |
| 305 | |
| 306 | tmpDir, err := os.MkdirTemp("", "urfave-cli*") |
| 307 | if err != nil { |
| 308 | return err |
| 309 | } |
| 310 | |
| 311 | wd, err := os.Getwd() |
| 312 | if err != nil { |
| 313 | return err |
| 314 | } |
| 315 | |
| 316 | if err := os.Chdir(tmpDir); err != nil { |
| 317 | return err |
| 318 | } |
| 319 | |
| 320 | fmt.Fprintf(cmd.ErrWriter, "# ---> workspace/TMPDIR is %q\n", tmpDir) |
| 321 | |
| 322 | if err := runCmd(ctx, "go", "work", "init", docsDir); err != nil { |
| 323 | return err |
| 324 | } |
| 325 | |
| 326 | os.Setenv("TMPDIR", tmpDir) |
| 327 | |
| 328 | if err := os.Chdir(wd); err != nil { |
| 329 | return err |
| 330 | } |
| 331 | |
| 332 | dirPath := cmd.Args().Get(0) |
| 333 | if dirPath == "" { |
| 334 | dirPath = "README.md" |
| 335 | } |
| 336 | |
| 337 | walk := cmd.Bool("walk") |
| 338 | sources := []string{} |
| 339 | |
| 340 | if walk { |
| 341 | // Walk the directory and find all markdown files. |
| 342 | err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error { |
| 343 | if err != nil { |
| 344 | return err |
| 345 | } |
| 346 | |
| 347 | if info.IsDir() { |
| 348 | return nil |
| 349 | } |
| 350 | |
| 351 | if filepath.Ext(path) != ".md" { |
| 352 | return nil |
| 353 | } |
| 354 | |
| 355 | sources = append(sources, path) |
| 356 | return nil |
| 357 | }) |
| 358 | if err != nil { |
| 359 | return err |
| 360 | } |
| 361 | } else { |
| 362 | sources = append(sources, dirPath) |
| 363 | } |
| 364 | |
| 365 | var counter int |
| 366 | inGoBlock := false |
| 367 | goHasPkgMain := false |
| 368 | fenceRe := regexp.MustCompile("^(`{3,}|~{3,})") |
| 369 | |
| 370 | for _, src := range sources { |
| 371 | file, err := os.Open(src) |
| 372 | if err != nil { |
| 373 | return err |
| 374 | } |
| 375 | defer file.Close() |
| 376 | |
| 377 | scanner := bufio.NewScanner(file) |
| 378 | for scanner.Scan() { |
| 379 | line := scanner.Text() |
| 380 | fenceMatch := fenceRe.FindString(line) |
| 381 | if fenceMatch == "" { |
| 382 | if inGoBlock && strings.HasPrefix(line, "package main") { |
| 383 | goHasPkgMain = true |
| 384 | } |
| 385 | continue |
| 386 | } |
| 387 | |
| 388 | lang := strings.TrimSpace(line[len(fenceMatch):]) |
| 389 | |
| 390 | if inGoBlock { |
| 391 | if goHasPkgMain { |
| 392 | counter++ |
| 393 | } |
| 394 | inGoBlock = false |
| 395 | goHasPkgMain = false |
| 396 | continue |
| 397 | } |
| 398 | |
| 399 | switch lang { |
| 400 | case "go": |
| 401 | inGoBlock = true |
| 402 | goHasPkgMain = false |
| 403 | case "sh": |
| 404 | counter++ |
| 405 | case "bash": |
| 406 | counter++ |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | err = file.Close() |
| 411 | if err != nil { |
| 412 | return err |
| 413 | } |
| 414 | |
| 415 | err = scanner.Err() |
| 416 | if err != nil { |
| 417 | return err |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | gfmArgs := []string{ |
| 422 | "--count", |
| 423 | fmt.Sprint(counter), |
| 424 | } |
| 425 | for _, src := range sources { |
| 426 | gfmArgs = append(gfmArgs, "--sources", src) |
| 427 | } |
| 428 | |
| 429 | if err := runCmd(ctx, "gfmrun", gfmArgs...); err != nil { |
| 430 | return err |
| 431 | } |
| 432 | |
| 433 | return os.RemoveAll(tmpDir) |
| 434 | } |
| 435 | |
| 436 | // checkBinarySizeActionFunc checks the size of an example binary to ensure that we are keeping size down |
| 437 | // this was originally inspired by https://github.com/urfave/cli/issues/1055, and followed up on as a part |
| 438 | // of https://github.com/urfave/cli/issues/1057 |
| 439 | func checkBinarySizeActionFunc(ctx context.Context, cmd *cli.Command) (err error) { |
| 440 | const ( |
| 441 | cliSourceFilePath = "./examples/example-cli/example-cli.go" |
| 442 | cliBuiltFilePath = "./examples/example-cli/built-example" |
| 443 | helloSourceFilePath = "./examples/example-hello-world/example-hello-world.go" |
| 444 | helloBuiltFilePath = "./examples/example-hello-world/built-example" |
| 445 | desiredMaxBinarySize = 2.2 |
| 446 | desiredMinBinarySize = 1.49 |
| 447 | mbStringFormatter = "%.1fMB" |
| 448 | ) |
| 449 | |
| 450 | tags := cmd.String("tags") |
| 451 | |
| 452 | // get cli example size |
| 453 | cliSize, err := getSize(ctx, cliSourceFilePath, cliBuiltFilePath, tags) |
| 454 | if err != nil { |
| 455 | return err |
| 456 | } |
| 457 | |
| 458 | // get hello world size |
| 459 | helloSize, err := getSize(ctx, helloSourceFilePath, helloBuiltFilePath, tags) |
| 460 | if err != nil { |
| 461 | return err |
| 462 | } |
| 463 | |
| 464 | // The CLI size diff is the number we are interested in. |
| 465 | // This tells us how much our CLI package contributes to the binary size. |
| 466 | cliSizeDiff := cliSize - helloSize |
| 467 | |
| 468 | // get human readable size, in MB with one decimal place. |
| 469 | // example output is: 35.2MB. (note: this simply an example) |
| 470 | // that output is much easier to reason about than the `35223432` |
| 471 | // that you would see output without the rounding |
| 472 | fileSizeInMB := float64(cliSizeDiff) / float64(1000000) |
| 473 | roundedFileSize := math.Round(fileSizeInMB*10) / 10 |
| 474 | roundedFileSizeString := fmt.Sprintf(mbStringFormatter, roundedFileSize) |
| 475 | |
| 476 | // check against bounds |
| 477 | isLessThanDesiredMin := roundedFileSize < desiredMinBinarySize |
| 478 | isMoreThanDesiredMax := roundedFileSize > desiredMaxBinarySize |
| 479 | desiredMinSizeString := fmt.Sprintf(mbStringFormatter, desiredMinBinarySize) |
| 480 | desiredMaxSizeString := fmt.Sprintf(mbStringFormatter, desiredMaxBinarySize) |
| 481 | |
| 482 | // show guidance |
| 483 | fmt.Printf("\n%s is the current binary size\n", roundedFileSizeString) |
| 484 | // show guidance for min size |
| 485 | if isLessThanDesiredMin { |
| 486 | fmt.Printf(" %s %s is the target min size\n", goodNewsEmoji, desiredMinSizeString) |
| 487 | fmt.Println("") // visual spacing |
| 488 | fmt.Println(" The binary is smaller than the target min size, which is great news!") |
| 489 | fmt.Println(" That means that your changes are shrinking the binary size.") |
| 490 | fmt.Println(" You'll want to go into ./scripts/build.go and decrease") |
| 491 | fmt.Println(" the desiredMinBinarySize, and also probably decrease the ") |
| 492 | fmt.Println(" desiredMaxBinarySize by the same amount. That will ensure that") |
| 493 | fmt.Println(" future PRs will enforce the newly shrunk binary sizes.") |
| 494 | fmt.Println("") // visual spacing |
| 495 | os.Exit(1) |
| 496 | } else { |
| 497 | fmt.Printf(" %s %s is the target min size\n", checksPassedEmoji, desiredMinSizeString) |
| 498 | } |
| 499 | // show guidance for max size |
| 500 | if isMoreThanDesiredMax { |
| 501 | fmt.Printf(" %s %s is the target max size\n", badNewsEmoji, desiredMaxSizeString) |
| 502 | fmt.Println("") // visual spacing |
| 503 | fmt.Println(" The binary is larger than the target max size.") |
| 504 | fmt.Println(" That means that your changes are increasing the binary size.") |
| 505 | fmt.Println(" The first thing you'll want to do is ask your yourself") |
| 506 | fmt.Println(" Is this change worth increasing the binary size?") |
| 507 | fmt.Println(" Larger binary sizes for this package can dissuade its use.") |
| 508 | fmt.Println(" If this change is worth the increase, then we can up the") |
| 509 | fmt.Println(" desired max binary size. To do that you'll want to go into") |
| 510 | fmt.Println(" ./scripts/build.go and increase the desiredMaxBinarySize,") |
| 511 | fmt.Println(" and increase the desiredMinBinarySize by the same amount.") |
| 512 | fmt.Println("") // visual spacing |
| 513 | os.Exit(1) |
| 514 | } else { |
| 515 | fmt.Printf(" %s %s is the target max size\n", checksPassedEmoji, desiredMaxSizeString) |
| 516 | } |
| 517 | |
| 518 | return nil |
| 519 | } |
| 520 | |
| 521 | func GenerateActionFunc(ctx context.Context, cmd *cli.Command) error { |
| 522 | topDir := cmd.String("top-dir") |
| 523 | |
| 524 | cliDocs, err := sh(ctx, "go", "doc", "-all", topDir) |
| 525 | if err != nil { |
| 526 | return err |
| 527 | } |
| 528 | |
| 529 | return os.WriteFile( |
| 530 | filepath.Join(topDir, "godoc-current.txt"), |
| 531 | []byte(cliDocs), |
| 532 | 0o644, |
| 533 | ) |
| 534 | } |
| 535 | |
| 536 | func DiffCheckActionFunc(ctx context.Context, cmd *cli.Command) error { |
| 537 | if err := os.Chdir(cmd.String("top-dir")); err != nil { |
| 538 | return err |
| 539 | } |
| 540 | |
| 541 | if err := runCmd(ctx, "git", "diff", "--exit-code"); err != nil { |
| 542 | return err |
| 543 | } |
| 544 | |
| 545 | return runCmd(ctx, "git", "diff", "--cached", "--exit-code") |
| 546 | } |
| 547 | |
| 548 | func EnsureGoimportsActionFunc(ctx context.Context, cmd *cli.Command) error { |
| 549 | topDir := cmd.String("top-dir") |
| 550 | if err := os.Chdir(topDir); err != nil { |
| 551 | return err |
| 552 | } |
| 553 | |
| 554 | if err := runCmd( |
| 555 | ctx, |
| 556 | "goimports", |
| 557 | "-d", |
| 558 | filepath.Join(topDir, "scripts/build.go"), |
| 559 | ); err == nil { |
| 560 | return nil |
| 561 | } |
| 562 | |
| 563 | os.Setenv("GOBIN", filepath.Join(topDir, ".local/bin")) |
| 564 | |
| 565 | return runCmd(ctx, "go", "install", "golang.org/x/tools/cmd/goimports@latest") |
| 566 | } |
| 567 | |
| 568 | func EnsureGfmrunActionFunc(ctx context.Context, cmd *cli.Command) error { |
| 569 | topDir := cmd.String("top-dir") |
| 570 | gfmrunExe := filepath.Join(topDir, ".local/bin/gfmrun") |
| 571 | |
| 572 | if err := os.Chdir(topDir); err != nil { |
| 573 | return err |
| 574 | } |
| 575 | |
| 576 | if v, err := sh(ctx, gfmrunExe, "--version"); err == nil && strings.TrimSpace(v) == gfmrunVersion { |
| 577 | return nil |
| 578 | } |
| 579 | |
| 580 | gfmrunURL, err := url.Parse( |
| 581 | fmt.Sprintf( |
| 582 | "https://github.com/urfave/gfmrun/releases/download/%[1]s/gfmrun-%[2]s-%[3]s-%[1]s", |
| 583 | gfmrunVersion, runtime.GOOS, runtime.GOARCH, |
| 584 | ), |
| 585 | ) |
| 586 | if err != nil { |
| 587 | return err |
| 588 | } |
| 589 | |
| 590 | return downloadFile(gfmrunURL.String(), gfmrunExe, 0o755, 0o755) |
| 591 | } |
| 592 | |
| 593 | func EnsureMkdocsActionFunc(ctx context.Context, cmd *cli.Command) error { |
| 594 | if err := os.Chdir(cmd.String("top-dir")); err != nil { |
| 595 | return err |
| 596 | } |
| 597 | |
| 598 | if err := runCmd(ctx, "mkdocs", "--version"); err == nil { |
| 599 | return nil |
| 600 | } |
| 601 | |
| 602 | if cmd.Bool("upgrade-pip") { |
| 603 | if err := runCmd(ctx, "pip", "install", "-U", "pip"); err != nil { |
| 604 | return err |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | return runCmd(ctx, "pip", "install", "-r", "mkdocs-requirements.txt") |
| 609 | } |
| 610 | |
| 611 | func SetMkdocsRemoteActionFunc(ctx context.Context, cmd *cli.Command) error { |
| 612 | ghToken := strings.TrimSpace(cmd.String("github-token")) |
| 613 | if ghToken == "" { |
| 614 | return errors.New("empty github token") |
| 615 | } |
| 616 | |
| 617 | if err := os.Chdir(cmd.String("top-dir")); err != nil { |
| 618 | return err |
| 619 | } |
| 620 | |
| 621 | if err := runCmd(ctx, "git", "remote", "rm", "origin"); err != nil { |
| 622 | return err |
| 623 | } |
| 624 | |
| 625 | return runCmd( |
| 626 | ctx, |
| 627 | "git", "remote", "add", "origin", |
| 628 | fmt.Sprintf("https://x-access-token:%[1][email protected]/urfave/cli.git", ghToken), |
| 629 | ) |
| 630 | } |
| 631 | |
| 632 | func LintActionFunc(ctx context.Context, cmd *cli.Command) error { |
| 633 | topDir := cmd.String("top-dir") |
| 634 | if err := os.Chdir(topDir); err != nil { |
| 635 | return err |
| 636 | } |
| 637 | |
| 638 | out, err := sh(ctx, filepath.Join(topDir, ".local/bin/goimports"), "-l", ".") |
| 639 | if err != nil { |
| 640 | return err |
| 641 | } |
| 642 | |
| 643 | if strings.TrimSpace(out) != "" { |
| 644 | fmt.Fprintln(cmd.ErrWriter, "# ---> goimports -l is non-empty:") |
| 645 | fmt.Fprintln(cmd.ErrWriter, out) |
| 646 | |
| 647 | return errors.New("goimports needed") |
| 648 | } |
| 649 | |
| 650 | return nil |
| 651 | } |
| 652 | |
| 653 | func V3Diff(ctx context.Context, cmd *cli.Command) error { |
| 654 | if err := os.Chdir(cmd.String("top-dir")); err != nil { |
| 655 | return err |
| 656 | } |
| 657 | |
| 658 | err := runCmd( |
| 659 | ctx, |
| 660 | "diff", |
| 661 | "--ignore-all-space", |
| 662 | "--minimal", |
| 663 | "--color="+func() string { |
| 664 | if cmd.Bool("color") { |
| 665 | return "always" |
| 666 | } |
| 667 | return "auto" |
| 668 | }(), |
| 669 | "--unified", |
| 670 | "--label=a/godoc", |
| 671 | filepath.Join("testdata", "godoc-v3.x.txt"), |
| 672 | "--label=b/godoc", |
| 673 | "godoc-current.txt", |
| 674 | ) |
| 675 | if err != nil { |
| 676 | fmt.Printf("# %v ---> Hey! <---\n", badNewsEmoji) |
| 677 | fmt.Println(strings.TrimSpace(v3diffWarning)) |
| 678 | } |
| 679 | |
| 680 | return err |
| 681 | } |
| 682 | |
| 683 | func getSize(ctx context.Context, sourcePath, builtPath, tags string) (int64, error) { |
| 684 | args := []string{"build"} |
| 685 | |
| 686 | if tags != "" { |
| 687 | args = append(args, []string{"-tags", tags}...) |
| 688 | } |
| 689 | |
| 690 | args = append(args, []string{ |
| 691 | "-o", builtPath, |
| 692 | "-ldflags", "-s -w", |
| 693 | sourcePath, |
| 694 | }...) |
| 695 | |
| 696 | if err := runCmd(ctx, "go", args...); err != nil { |
| 697 | fmt.Println("issue getting size for example binary") |
| 698 | return 0, err |
| 699 | } |
| 700 | |
| 701 | fileInfo, err := os.Stat(builtPath) |
| 702 | if err != nil { |
| 703 | fmt.Println("issue getting size for example binary") |
| 704 | return 0, err |
| 705 | } |
| 706 | |
| 707 | return fileInfo.Size(), nil |
| 708 | } |