gotools/cmd/golicenses/input.go

41 lines
814 B
Go

package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
)
type ModuleDependency struct {
Path string
Version string
Dir string
Indirect bool
License string
}
func pareseGoMod() []ModuleDependency {
// Get list of all modules
cmd := exec.Command("go", "list", "-m", "-json", "all")
output, err := cmd.Output()
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting module list: %v\n", err)
os.Exit(1)
}
// Split the JSON objects (one per line)
modules := []ModuleDependency{}
decoder := json.NewDecoder(strings.NewReader(string(output)))
for decoder.More() {
var mod ModuleDependency
if err := decoder.Decode(&mod); err != nil {
fmt.Fprintf(os.Stderr, "Error decoding module: %v\n", err)
continue
}
modules = append(modules, mod)
}
return modules
}