Debian Codegraph @

internal/gapicgen/cmd/genbot/main.go

raw
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 //go:build !windows
16 // +build !windows
17
18 // genbot is a binary for generating gapics and creating CLs/PRs with the results.
19 // It is intended to be used as a bot, though it can be run locally too.
20 package main
21
22 import (
23 "context"
24 "errors"
25 "flag"
26 "log"
27 "os"
28 "strconv"
29
30 "cloud.google.com/go/internal/gapicgen"
31 "cloud.google.com/go/internal/gapicgen/generator"
32 )
33
34 func main() {
35 log.SetFlags(0)
36 if err := gapicgen.VerifyAllToolsExist([]string{"git", "go", "protoc"}); err != nil {
37 log.Fatal(err)
38 }
39 ctx := context.Background()
40
41 // General bot flags
42 githubAccessToken := flag.String("githubAccessToken", os.Getenv("GITHUB_ACCESS_TOKEN"), "The token used to open pull requests.")
43 githubUsername := flag.String("githubUsername", os.Getenv("GITHUB_USERNAME"), "The GitHub user name for the author.")
44 githubName := flag.String("githubName", os.Getenv("GITHUB_NAME"), "The name of the author for git commits.")
45 githubEmail := flag.String("githubEmail", os.Getenv("GITHUB_EMAIL"), "The email address of the author.")
46 localMode := flag.Bool("local", strToBool(os.Getenv("GENBOT_LOCAL_MODE")), "Enables generating sources locally. This mode will not open any pull requests.")
47 forceAll := flag.Bool("forceAll", strToBool(os.Getenv("GENBOT_FORCE_ALL")), "Enables regenerating everything regardless of changes in googleapis.")
48
49 // flags for local mode
50 googleapisDir := flag.String("googleapis-dir", os.Getenv("GOOGLEAPIS_DIR"), "Directory where sources of googleapis/googleapis resides. If unset the sources will be cloned to a temporary directory that is not cleaned up.")
51 genprotoDir := flag.String("genproto-dir", os.Getenv("GENPROTO_DIR"), "Directory where sources of googleapis/go-genproto resides. If unset the sources will be cloned to a temporary directory that is not cleaned up.")
52 protoDir := flag.String("proto-dir", os.Getenv("PROTO_DIR"), "Directory where sources of google/protobuf resides. If unset the sources will be cloned to a temporary directory that is not cleaned up.")
53 regenOnly := flag.Bool("regen-only", strToBool(os.Getenv("REGEN_ONLY")), "Enabling means no vetting, manifest updates, or compilation.")
54 genAlias := flag.Bool("generate-alias", strToBool(os.Getenv("GENERATE_ALIAS")), "Enabling means alias files will be generated.")
55
56 flag.Parse()
57
58 if *localMode {
59 if err := genLocal(ctx, localConfig{
60 googleapisDir: *googleapisDir,
61 genprotoDir: *genprotoDir,
62 protoDir: *protoDir,
63 regenOnly: *regenOnly,
64 forceAll: *forceAll,
65 genAlias: *genAlias,
66 }); err != nil {
67 if errors.Is(err, generator.ErrNoProcessing) {
68 log.Println(err)
69 os.Exit(0)
70 return
71 }
72 log.Fatal(err)
73 }
74 return
75 }
76 if err := genBot(ctx, botConfig{
77 githubAccessToken: *githubAccessToken,
78 githubUsername: *githubUsername,
79 githubName: *githubName,
80 githubEmail: *githubEmail,
81 forceAll: *forceAll,
82 }); err != nil {
83 if errors.Is(err, generator.ErrNoProcessing) {
84 log.Println(err)
85 os.Exit(0)
86 return
87 }
88 log.Fatal(err)
89 }
90 }
91
92 func strToBool(s string) bool {
93 // Treat error as false
94 b, _ := strconv.ParseBool(s)
95 return b
96 }