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