16534: Add database access in lib/controller/localdb.
[arvados.git] / lib / controller / federation / generate.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 // +build ignore
6
7 package main
8
9 import (
10         "bytes"
11         "io"
12         "io/ioutil"
13         "os"
14         "os/exec"
15         "regexp"
16 )
17
18 func main() {
19         checkOnly := false
20         if len(os.Args) == 2 && os.Args[1] == "-check" {
21                 checkOnly = true
22         } else if len(os.Args) != 1 {
23                 panic("usage: go run generate.go [-check]")
24         }
25
26         in, err := os.Open("list.go")
27         if err != nil {
28                 panic(err)
29         }
30         buf, err := ioutil.ReadAll(in)
31         if err != nil {
32                 panic(err)
33         }
34         orig := regexp.MustCompile(`(?ms)\nfunc [^\n]*generated_CollectionList\(.*?\n}\n`).Find(buf)
35         if len(orig) == 0 {
36                 panic("can't find CollectionList func")
37         }
38
39         outfile, err := os.OpenFile("generated.go~", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
40         if err != nil {
41                 panic(err)
42         }
43
44         gofmt := exec.Command("goimports")
45         gofmt.Stdout = outfile
46         gofmt.Stderr = os.Stderr
47         out, err := gofmt.StdinPipe()
48         if err != nil {
49                 panic(err)
50         }
51         go func() {
52                 defer out.Close()
53                 out.Write(regexp.MustCompile(`(?ms)^.*package .*?import.*?\n\)\n`).Find(buf))
54                 io.WriteString(out, "//\n// -- this file is auto-generated -- do not edit -- edit list.go and run \"go generate\" instead --\n//\n\n")
55                 for _, t := range []string{"Container", "Specimen", "User"} {
56                         _, err := out.Write(bytes.ReplaceAll(orig, []byte("Collection"), []byte(t)))
57                         if err != nil {
58                                 panic(err)
59                         }
60                 }
61         }()
62         err = gofmt.Run()
63         if err != nil {
64                 panic(err)
65         }
66         err = outfile.Close()
67         if err != nil {
68                 panic(err)
69         }
70         if checkOnly {
71                 diff := exec.Command("diff", "-u", "/dev/fd/3", "/dev/fd/4")
72                 for _, fnm := range []string{"generated.go", "generated.go~"} {
73                         f, err := os.Open(fnm)
74                         if err != nil {
75                                 panic(err)
76                         }
77                         defer f.Close()
78                         diff.ExtraFiles = append(diff.ExtraFiles, f)
79                 }
80                 diff.Stdout = os.Stdout
81                 diff.Stderr = os.Stderr
82                 err = diff.Run()
83                 if err != nil {
84                         os.Exit(1)
85                 }
86         } else {
87                 err = os.Rename("generated.go~", "generated.go")
88                 if err != nil {
89                         panic(err)
90                 }
91         }
92 }