Chore: Add go workspace scripts (#91707)

This commit is contained in:
Todd Treece
2024-08-08 16:51:17 -04:00
committed by GitHub
parent 54177ca619
commit 7f155b2b6f
17 changed files with 1576 additions and 46 deletions
+5
View File
@@ -0,0 +1,5 @@
module github.com/grafana/grafana/scripts/go-workspace
go 1.22.4
require golang.org/x/mod v0.20.0
+2
View File
@@ -0,0 +1,2 @@
golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0=
golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
+9
View File
@@ -0,0 +1,9 @@
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")/../..
go run scripts/go-workspace/main.go list-submodules --path "${REPO_ROOT}/go.work" --delimiter '/... ' --skip golangci-lint
+93
View File
@@ -0,0 +1,93 @@
package main
import (
"flag"
"fmt"
"os"
"strings"
"golang.org/x/mod/modfile"
)
func main() {
if len(os.Args) < 2 {
printUsage()
os.Exit(1)
}
var err error
switch os.Args[1] {
case "list-submodules":
err = listSubmodules()
default:
printUsage()
}
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
func printUsage() {
println("Usage: go-workspace <command> [args]")
println("Commands:")
println(" list-submodules - List submodules in go.work")
}
func listSubmodules() error {
fs := flag.NewFlagSet("list-submodules", flag.ExitOnError)
workPath := fs.String("path", "go.work", "Path to go.work")
delimiter := fs.String("delimiter", "\n", "Delimiter to use when printing paths")
skip := fs.String("skip", "", "Skip submodules with this comment tag")
help := fs.Bool("help", false, "Print help message")
fs.Parse(os.Args[2:])
if *help {
fs.Usage()
return nil
}
workfile, err := parseGoWork(*workPath)
if err != nil {
return err
}
paths := getSubmodulePaths(workfile, *skip)
for _, p := range paths {
fmt.Printf("%s%s", p, *delimiter)
}
return nil
}
func getSubmodulePaths(wf *modfile.WorkFile, skip string) []string {
var paths []string
for _, d := range wf.Use {
if hasSkipTag(d, skip) {
continue
}
paths = append(paths, d.Path)
}
return paths
}
func hasSkipTag(d *modfile.Use, tag string) bool {
if tag == "" {
return false
}
for _, c := range d.Syntax.Comments.Suffix {
if strings.Contains(c.Token, fmt.Sprintf("skip:%s", tag)) {
return true
}
}
return false
}
func parseGoWork(p string) (*modfile.WorkFile, error) {
contents, err := os.ReadFile(p)
if err != nil {
return nil, err
}
return modfile.ParseWork(p, contents, nil)
}
+21
View File
@@ -0,0 +1,21 @@
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")/../..
for mod in $(go run scripts/go-workspace/main.go list-submodules --path "${REPO_ROOT}/go.work"); do
pushd "${mod}"
echo "Running go mod tidy in ${mod}"
go mod tidy
popd
done
pushd "${REPO_ROOT}"
echo "running go mod download"
go mod download
echo "running go work sync"
go work sync