| 1 |
// Copyright 2021 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 |
// Package gocmd provides helers for invoking Go tooling. |
| 16 |
package gocmd |
| 17 |
|
| 18 |
import ( |
| 19 |
"errors" |
| 20 |
"fmt" |
| 21 |
"io/fs" |
| 22 |
"log" |
| 23 |
"os" |
| 24 |
"os/exec" |
| 25 |
"path/filepath" |
| 26 |
"strings" |
| 27 |
|
| 28 |
"cloud.google.com/go/internal/gapicgen/execv" |
| 29 |
) |
| 30 |
|
| 31 |
var ( |
| 32 |
// ErrBuildConstraint is returned when the Go command returns this error. |
| 33 |
ErrBuildConstraint error = errors.New("build constraints exclude all Go files") |
| 34 |
) |
| 35 |
|
| 36 |
// ModTidy tidies go.mod file in the specified directory. |
| 37 |
func ModTidy(dir string) error { |
| 38 |
c := execv.Command("go", "mod", "tidy") |
| 39 |
c.Dir = dir |
| 40 |
c.Env = []string{ |
| 41 |
fmt.Sprintf("PATH=%s", os.Getenv("PATH")), // TODO(deklerk): Why do we need to do this? Doesn't seem to be necessary in other exec.Commands. |
| 42 |
fmt.Sprintf("HOME=%s", os.Getenv("HOME")), // TODO(deklerk): Why do we need to do this? Doesn't seem to be necessary in other exec.Commands. |
| 43 |
} |
| 44 |
return c.Run() |
| 45 |
} |
| 46 |
|
| 47 |
// ModTidyAll tidies all mod files from the specified root directory. |
| 48 |
func ModTidyAll(dir string) error { |
| 49 |
log.Printf("[%s] finding all modules", dir) |
| 50 |
var modDirs []string |
| 51 |
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { |
| 52 |
if err != nil { |
| 53 |
return err |
| 54 |
} |
| 55 |
if d.Name() == "go.mod" { |
| 56 |
modDirs = append(modDirs, filepath.Dir(path)) |
| 57 |
} |
| 58 |
return nil |
| 59 |
}) |
| 60 |
if err != nil { |
| 61 |
return err |
| 62 |
} |
| 63 |
for _, modDir := range modDirs { |
| 64 |
if err := ModTidy(modDir); err != nil { |
| 65 |
return err |
| 66 |
} |
| 67 |
} |
| 68 |
return nil |
| 69 |
} |
| 70 |
|
| 71 |
// ListModName finds a modules name for a given directory. |
| 72 |
func ListModName(dir string) (string, error) { |
| 73 |
modC := execv.Command("go", "list", "-m") |
| 74 |
modC.Dir = dir |
| 75 |
mod, err := modC.Output() |
| 76 |
return string(mod), err |
| 77 |
} |
| 78 |
|
| 79 |
// ListModDirName finds the directory in which the module resides. Returns |
| 80 |
// ErrBuildConstraint if all files in a module are constrained. |
| 81 |
func ListModDirName(dir string) (string, error) { |
| 82 |
var out []byte |
| 83 |
var err error |
| 84 |
c := execv.Command("go", "list", "-f", "'{{.Module.Dir}}'") |
| 85 |
c.Dir = dir |
| 86 |
if out, err = c.Output(); err != nil { |
| 87 |
if ee, ok := err.(*exec.ExitError); ok { |
| 88 |
if strings.Contains(string(ee.Stderr), "build constraints exclude all Go files") { |
| 89 |
return "", ErrBuildConstraint |
| 90 |
} |
| 91 |
} |
| 92 |
return "", err |
| 93 |
} |
| 94 |
return strings.Trim(strings.TrimSpace(string(out)), "'"), nil |
| 95 |
} |
| 96 |
|
| 97 |
// Build attempts to build all packages recursively from the given directory. |
| 98 |
func Build(dir string) error { |
| 99 |
log.Println("building generated code") |
| 100 |
c := execv.Command("go", "build", "./...") |
| 101 |
c.Dir = dir |
| 102 |
if _, err := c.Output(); err != nil { |
| 103 |
if ee, ok := err.(*exec.ExitError); ok { |
| 104 |
log.Printf("Error Output: %s", ee.Stderr) |
| 105 |
} |
| 106 |
return err |
| 107 |
} |
| 108 |
return nil |
| 109 |
} |
| 110 |
|
| 111 |
// Vet runs linters on all .go files recursively from the given directory. |
| 112 |
func Vet(dir string) error { |
| 113 |
log.Println("vetting generated code") |
| 114 |
c := execv.Command("goimports", "-w", ".") |
| 115 |
c.Dir = dir |
| 116 |
if err := c.Run(); err != nil { |
| 117 |
return err |
| 118 |
} |
| 119 |
|
| 120 |
c = execv.Command("gofmt", "-s", "-d", "-w", "-l", ".") |
| 121 |
c.Dir = dir |
| 122 |
return c.Run() |
| 123 |
} |