This repository was archived by the owner on Nov 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
99 lines (80 loc) · 2.48 KB
/
Copy pathmain.go
File metadata and controls
99 lines (80 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// godeps is a small tool to generate a list of dependencies for a set of Go packages.
// The output format can directly be integrated in a Makefile rule.
//
// Usage:
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"golang.org/x/tools/go/packages"
)
var usage = `
Usage:
godeps [-flags FLAGS] [-pkgdir DIR] [-include-tests] PACKAGE [PACKAGE …]
Options:
-flags Build flags to consider (as given to "go build" command)
-pkgdir Root of the package (if multi-module build)
-include-tests Also generate dependencies for test files (usually not required)
The most common error for this package is the "godeps only accepts main package".
If you have given an actual main package name, this is usually because the package cannot
be found. Make sure that pkgdir is set to the right directory, this can be checked by:
go list -f '{{.Name}}' PACKAGE
`
func main() {
buildFlags := []string{}
pkgDir, _ := os.Getwd()
flag.Func("flags", "build flags to include", func(s string) error {
buildFlags = strings.Split(s, ",")
return nil
})
flag.StringVar(&pkgDir, "pkgdir", "", "Load packages from dir instead of current directory")
flag.Bool("include-tests", false, "Include related test packages")
outspec := flag.String("o", "-", "Destination of the dependencies (stdout by default)")
flag.Parse()
ctx := context.Background()
pkgDir, _ = filepath.Abs(pkgDir)
dst := os.Stdout
if *outspec != "-" {
var err error
dst, err = os.Create(*outspec)
if err != nil {
log.Fatalf("creating output %s: %s", *outspec, err)
}
}
cfg := packages.Config{
Context: ctx,
Dir: pkgDir,
Mode: packages.NeedName | packages.NeedFiles | packages.NeedImports | packages.NeedDeps | packages.NeedModule | packages.NeedEmbedFiles,
BuildFlags: buildFlags,
}
pkgs, err := packages.Load(&cfg, flag.Args()...)
if err != nil {
log.Fatal("error loading packages", flag.Args(), err)
}
for _, p := range pkgs {
if p.Name != "main" {
log.Fatalf("godeps only accepts main packages [ran in %s]: got %s", pkgDir, p.Name)
}
}
for _, p := range pkgs {
fmt.Fprint(dst, p.Module.GoMod, " ")
}
cmod := pkgs[0].Module.Path
packages.Visit(pkgs, func(p *packages.Package) bool {
if p.Module == nil || p.Module.Path != cmod {
return false
}
files := [][]string{p.GoFiles, p.EmbedFiles, p.OtherFiles}
for _, fs := range files {
if len(fs) > 0 {
fmt.Fprintf(dst, "%s ", strings.Join(fs, " "))
}
}
return true
}, nil)
}