xref: /aosp_15_r20/external/bazelbuild-rules_go/go/tools/builders/info.go (revision 9bb1b549b6a84214c53be0924760be030e66b93a)
1// Copyright 2017 The Bazel Authors. All rights reserved.
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// info prints debugging information about the go environment.
16// It is used to help examine the execution environment of rules_go
17package main
18
19import (
20	"flag"
21	"fmt"
22	"log"
23	"os"
24)
25
26func run(args []string) error {
27	args, _, err := expandParamsFiles(args)
28	if err != nil {
29		return err
30	}
31	filename := ""
32	flags := flag.NewFlagSet("info", flag.ExitOnError)
33	flags.StringVar(&filename, "out", filename, "The file to write the report to")
34	goenv := envFlags(flags)
35	if err := flags.Parse(args); err != nil {
36		return err
37	}
38	if err := goenv.checkFlags(); err != nil {
39		return err
40	}
41	os.Setenv("GO111MODULE", "off")
42	f := os.Stderr
43	if filename != "" {
44		var err error
45		f, err = os.Create(filename)
46		if err != nil {
47			return fmt.Errorf("Could not create report file: %v", err)
48		}
49		defer f.Close()
50	}
51	if err := goenv.runCommandToFile(f, os.Stderr, goenv.goCmd("version")); err != nil {
52		return err
53	}
54	if err := goenv.runCommandToFile(f, os.Stderr, goenv.goCmd("env")); err != nil {
55		return err
56	}
57	return nil
58}
59
60func main() {
61	if err := run(os.Args[1:]); err != nil {
62		log.Fatal(err)
63	}
64}
65