| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "bytes" |
| 6 | "errors" |
| 7 | "flag" |
| 8 | "fmt" |
| 9 | "io" |
| 10 | "os" |
| 11 | "path/filepath" |
| 12 | "strings" |
| 13 | |
| 14 | "github.com/traefik/yaegi/extract" |
| 15 | ) |
| 16 | |
| 17 | func extractCmd(arg []string) error { |
| 18 | var licensePath string |
| 19 | var name string |
| 20 | var exclude string |
| 21 | var include string |
| 22 | var tag string |
| 23 | |
| 24 | eflag := flag.NewFlagSet("run", flag.ContinueOnError) |
| 25 | eflag.StringVar(&licensePath, "license", "", "path to a LICENSE file") |
| 26 | eflag.StringVar(&name, "name", "", "the namespace for the extracted symbols") |
| 27 | eflag.StringVar(&exclude, "exclude", "", "comma separated list of regexp matching symbols to exclude") |
| 28 | eflag.StringVar(&include, "include", "", "comma separated list of regexp matching symbols to include") |
| 29 | eflag.StringVar(&tag, "tag", "", "comma separated list of build tags to be added to the created package") |
| 30 | eflag.Usage = func() { |
| 31 | fmt.Println("Usage: yaegi extract [options] packages...") |
| 32 | fmt.Println("Options:") |
| 33 | eflag.PrintDefaults() |
| 34 | } |
| 35 | |
| 36 | if err := eflag.Parse(arg); err != nil { |
| 37 | return err |
| 38 | } |
| 39 | |
| 40 | args := eflag.Args() |
| 41 | if len(args) == 0 { |
| 42 | return errors.New("missing package") |
| 43 | } |
| 44 | |
| 45 | license, err := genLicense(licensePath) |
| 46 | if err != nil { |
| 47 | return err |
| 48 | } |
| 49 | |
| 50 | wd, err := os.Getwd() |
| 51 | if err != nil { |
| 52 | return err |
| 53 | } |
| 54 | |
| 55 | if name == "" { |
| 56 | name = filepath.Base(wd) |
| 57 | } |
| 58 | ext := extract.Extractor{ |
| 59 | Dest: name, |
| 60 | License: license, |
| 61 | } |
| 62 | if tag != "" { |
| 63 | ext.Tag = strings.Split(tag, ",") |
| 64 | } |
| 65 | |
| 66 | if exclude != "" { |
| 67 | ext.Exclude = strings.Split(exclude, ",") |
| 68 | } |
| 69 | if include != "" { |
| 70 | ext.Include = strings.Split(include, ",") |
| 71 | } |
| 72 | |
| 73 | r := strings.NewReplacer("/", "-", ".", "_", "~", "_") |
| 74 | |
| 75 | for _, pkgIdent := range args { |
| 76 | var buf bytes.Buffer |
| 77 | importPath, err := ext.Extract(pkgIdent, name, &buf) |
| 78 | if err != nil { |
| 79 | fmt.Fprintln(os.Stderr, err) |
| 80 | continue |
| 81 | } |
| 82 | |
| 83 | oFile := r.Replace(importPath) + ".go" |
| 84 | f, err := os.Create(oFile) |
| 85 | if err != nil { |
| 86 | return err |
| 87 | } |
| 88 | |
| 89 | if _, err := io.Copy(f, &buf); err != nil { |
| 90 | _ = f.Close() |
| 91 | return err |
| 92 | } |
| 93 | |
| 94 | if err := f.Close(); err != nil { |
| 95 | return err |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return nil |
| 100 | } |
| 101 | |
| 102 | // genLicense generates the correct LICENSE header text from the provided |
| 103 | // path to a LICENSE file. |
| 104 | func genLicense(fname string) (string, error) { |
| 105 | if fname == "" { |
| 106 | return "", nil |
| 107 | } |
| 108 | |
| 109 | f, err := os.Open(fname) |
| 110 | if err != nil { |
| 111 | return "", fmt.Errorf("could not open LICENSE file: %w", err) |
| 112 | } |
| 113 | defer func() { _ = f.Close() }() |
| 114 | |
| 115 | license := new(strings.Builder) |
| 116 | sc := bufio.NewScanner(f) |
| 117 | for sc.Scan() { |
| 118 | txt := sc.Text() |
| 119 | if txt != "" { |
| 120 | txt = " " + txt |
| 121 | } |
| 122 | license.WriteString("//" + txt + "\n") |
| 123 | } |
| 124 | |
| 125 | if err := sc.Err(); err != nil { |
| 126 | return "", fmt.Errorf("could not scan LICENSE file: %w", err) |
| 127 | } |
| 128 | |
| 129 | return license.String(), nil |
| 130 | } |