|
| 1 | +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +// or more contributor license agreements. Licensed under the Elastic License; |
| 3 | +// you may not use this file except in compliance with the Elastic License. |
| 4 | + |
| 5 | +package common |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "errors" |
| 10 | + "fmt" |
| 11 | + "io" |
| 12 | + "os" |
| 13 | + "os/exec" |
| 14 | + "strings" |
| 15 | + |
| 16 | + "github.com/magefile/mage/sh" |
| 17 | +) |
| 18 | + |
| 19 | +func runCommand(cmd string, args ...string) error { |
| 20 | + s := strings.Join(append([]string{cmd}, args...), " ") |
| 21 | + fmt.Printf(">> %s\n", s) |
| 22 | + err := sh.Run(cmd, args...) |
| 23 | + if err != nil { |
| 24 | + return fmt.Errorf("failed running %s, please fix the issues reported: %w", s, err) |
| 25 | + } |
| 26 | + return nil |
| 27 | +} |
| 28 | + |
| 29 | +// Notice Generates NOTICE.txt. |
| 30 | +func Notice() (err error) { |
| 31 | + fmt.Println("Generating NOTICE") |
| 32 | + if err := runCommand("go", "mod", "tidy"); err != nil { |
| 33 | + return err |
| 34 | + } |
| 35 | + if err := runCommand("go", "mod", "download"); err != nil { |
| 36 | + return err |
| 37 | + } |
| 38 | + |
| 39 | + // piping output of the first command to the second |
| 40 | + // similar to former Makefile implementation |
| 41 | + // |
| 42 | + // go list -m -json all | go run go.elastic.co/go-licence-detector \ |
| 43 | + // -includeIndirect \ |
| 44 | + // -rules dev-tools/notice/rules.json \ |
| 45 | + // -overrides dev-tools/notice/overrides.json \ |
| 46 | + // -noticeTemplate dev-tools/notice/NOTICE.txt.tmpl \ |
| 47 | + // -noticeOut NOTICE.txt \ |
| 48 | + // -depsOut "" |
| 49 | + listCmd := exec.Command("go", "list", "-m", "-json", "all") |
| 50 | + licDetectCmd := exec.Command("go", "run", "go.elastic.co/go-licence-detector", |
| 51 | + "-includeIndirect", |
| 52 | + "-rules", "dev-tools/notice/rules.json", |
| 53 | + "-overrides", "dev-tools/notice/overrides.json", |
| 54 | + "-noticeTemplate", "dev-tools/notice/NOTICE.txt.tmpl", |
| 55 | + "-noticeOut", "NOTICE.txt", |
| 56 | + "-depsOut", "") |
| 57 | + |
| 58 | + fmt.Printf(">> %s | %s\n", strings.Join(listCmd.Args, " "), strings.Join(licDetectCmd.Args, " ")) |
| 59 | + |
| 60 | + r, w := io.Pipe() |
| 61 | + defer r.Close() |
| 62 | + defer w.Close() |
| 63 | + |
| 64 | + var buf bytes.Buffer |
| 65 | + listCmd.Stdout = w |
| 66 | + licDetectCmd.Stdin = r |
| 67 | + licDetectCmd.Stderr = &buf |
| 68 | + |
| 69 | + if err := listCmd.Start(); err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + if err := licDetectCmd.Start(); err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + |
| 76 | + if err := listCmd.Wait(); err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + w.Close() |
| 80 | + |
| 81 | + if err := licDetectCmd.Wait(); err != nil { |
| 82 | + // copy error to stdout, helpful if tool failed |
| 83 | + if _, cerr := io.Copy(os.Stdout, &buf); cerr != nil { |
| 84 | + return errors.Join(fmt.Errorf("failed to read stderr: %w", cerr), err) |
| 85 | + } |
| 86 | + return err |
| 87 | + } |
| 88 | + |
| 89 | + // cat dev-tools/notice/NOTICE.txt.append >> NOTICE.txt |
| 90 | + fmt.Printf(">> %s\n", "cat dev-tools/notice/NOTICE.txt.append >> NOTICE.txt") |
| 91 | + const ( |
| 92 | + infn = "dev-tools/notice/NOTICE.txt.append" |
| 93 | + outfn = "NOTICE.txt" |
| 94 | + ) |
| 95 | + |
| 96 | + f, err := os.Open(infn) |
| 97 | + if err != nil { |
| 98 | + return fmt.Errorf("failed to open file %s: %w", infn, err) |
| 99 | + } |
| 100 | + defer f.Close() |
| 101 | + |
| 102 | + out, err := os.OpenFile(outfn, os.O_WRONLY|os.O_APPEND, 0644) |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("failed to open file %s: %w", outfn, err) |
| 105 | + } |
| 106 | + |
| 107 | + defer func() { |
| 108 | + cerr := out.Close() |
| 109 | + if err == nil { |
| 110 | + err = cerr |
| 111 | + } |
| 112 | + }() |
| 113 | + |
| 114 | + if _, err := io.Copy(out, f); err != nil { |
| 115 | + return fmt.Errorf("failed to append file %s: %w", outfn, err) |
| 116 | + } |
| 117 | + |
| 118 | + return nil |
| 119 | +} |
0 commit comments