package main
import (
)
type workspace struct {
Dir string
Paths []string
}
func ( *workspace) () string {
if .Dir == "" {
return .Paths[0]
}
return .Dir
}
func () (*workspace, error) {
, := goenv("GOMOD", "GOWORK")
if != nil {
return nil,
}
, := ["GOWORK"]
if ! || == "" || == "off" {
, := ["GOMOD"]
if ! || == "" || == os.DevNull {
return nil, errors.New("no go.work or go.mod files found")
}
return &workspace{
Paths: []string{
filepath.Dir(),
},
}, nil
}
:= filepath.Dir()
var bytes.Buffer
:= exec.Command("go", "work", "edit", "-json")
.Stdout = &
.Stderr = os.Stderr
.Dir =
if := .Run(); != nil {
return nil,
}
var struct {
[]struct {
string
}
}
if := json.Unmarshal(.Bytes(), &); != nil {
return nil,
}
:= make([]string, len(.))
for , := range . {
[] = filepath.Join(, filepath.FromSlash(.))
}
return &workspace{
Dir: ,
Paths: ,
}, nil
}
func ( *workspace) error {
for , := range .Paths {
:= exec.Command("go", "mod", "tidy")
.Stderr = os.Stderr
.Dir =
if := .Run(); != nil {
return
}
}
return goworksync()
}
func ( *workspace) error {
if .Dir == "" {
return nil
}
if := os.Remove(filepath.Join(.Dir, "go.work.sum")); != nil && !os.IsNotExist() {
return
}
:= exec.Command("go", "work", "sync")
.Stderr = os.Stderr
.Dir = .Dir
return .Run()
}
func ( ...string) (map[string]string, error) {
var bytes.Buffer
:= exec.Command("go", append([]string{"env", "-json", "--"}, ...)...)
.Stdout = &
.Stderr = os.Stderr
if := .Run(); != nil {
return nil,
}
:= make(map[string]string)
if := json.Unmarshal(.Bytes(), &); != nil {
return nil,
}
return , nil
}