| 74 | } |
| 75 | |
| 76 | func (s *SPDXConverter) conv(path string, fset *token.FileSet, f *ast.File) error { |
| 77 | if len(f.Comments) == 0 { |
| 78 | return nil |
| 79 | } |
| 80 | cg := f.Comments[0] |
| 81 | if !strings.HasPrefix(cg.List[0].Text, "// Copyright") { |
| 82 | log.Printf("file %v did not start with a Copyright string. skipping...", path) |
| 83 | return nil |
| 84 | } |
| 85 | var spdx string |
| 86 | for exp, tmp := range RegexpMap { |
| 87 | if exp.MatchString(cg.Text()) { |
| 88 | spdx = tmp |
| 89 | } |
| 90 | } |
| 91 | if spdx == "" { |
| 92 | log.Printf("could not determine license for %v", path) |
| 93 | return nil |
| 94 | } |
| 95 | |
| 96 | copyRight := cg.List[0].Text |
| 97 | cg.List[0].Text, cg.List[1].Text = spdx, copyRight |
| 98 | cg.List = cg.List[:2] |
| 99 | |
| 100 | fd, err := os.OpenFile(path, os.O_TRUNC|os.O_RDWR, 0660) |
| 101 | if err != nil { |
| 102 | return fmt.Errorf("failed to open original source file for conversion: %w", err) |
| 103 | } |
| 104 | defer fd.Close() |
| 105 | |
| 106 | err = format.Node(fd, fset, f) |
| 107 | if err != nil { |
| 108 | return fmt.Errorf("failed to write converted file %v: %w", path, err) |
| 109 | } |
| 110 | |
| 111 | return nil |
| 112 | } |
| 113 | |
| 114 | func main() { |
| 115 | switch { |