This repository has been archived by the owner on Jul 7, 2020. It is now read-only.
/
pack.go
100 lines (83 loc) · 1.97 KB
/
pack.go
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
100
package main
import (
"go/build"
"log"
. "github.com/AlekSi/nut"
)
const (
NutFilePerm = 0644
)
var (
cmdPack = &Command{
Run: runPack,
UsageLine: "pack [-nc] [-o filename] [-v]",
Short: "pack package in current directory into nut",
}
packNC bool
packO string
packV bool
)
func init() {
cmdPack.Long = `
Packs package in current directory into nut.
Examples:
nut pack
`
cmdPack.Flag.BoolVar(&packNC, "nc", false, "no check (not recommended)")
cmdPack.Flag.StringVar(&packO, "o", "", "output filename")
cmdPack.Flag.BoolVar(&packV, "v", false, vHelp)
}
func runPack(cmd *Command) {
if !packV {
packV = Config.V
}
if len(cmd.Flag.Args()) != 0 {
log.Fatal("This command does not accept arguments.")
}
/*
packages := build.ImportDir(".", 0)
if len(packages) > 1 {
panic(fmt.Errorf("Multi-package nuts are not supported yet\n%#v", packages))
// implementation will require import overwrites on install to prevent error like
// "local import "./dir/subdir" in non-local package"
}
*/
ctxt := build.Default
ctxt.UseAllFiles = true
pack, err := ctxt.ImportDir(".", 0)
FatalIfErr(err)
if pack.Name == "main" {
log.Fatal(`Binaries (package "main") are not supported yet.`)
}
var fileName string
spec := new(Spec)
err = spec.ReadFile(SpecFileName)
FatalIfErr(err)
nut := Nut{Spec: *spec, Package: *pack}
if packO == "" {
fileName = nut.FileName()
} else {
fileName = packO
}
if !packNC {
errors := nut.Check()
if len(errors) != 0 {
log.Print("Found errors:")
for _, e := range errors {
log.Printf(" %s", e)
}
log.Fatal("Hint: use 'nut check'.")
}
}
var files []string
files = append(files, pack.GoFiles...)
files = append(files, pack.CgoFiles...)
files = append(files, pack.TestGoFiles...)
files = append(files, pack.XTestGoFiles...)
files = append(files, spec.ExtraFiles...)
files = append(files, SpecFileName)
PackNut(fileName, files, packV)
if packV {
log.Printf("%s created.", fileName)
}
}