-
Notifications
You must be signed in to change notification settings - Fork 90
/
add.go
63 lines (46 loc) · 1.02 KB
/
add.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
package main
import (
"fmt"
"os"
"os/exec"
"github.com/clipperhouse/typewriter"
)
// add adds a new typewriter import to the current package, by creating (or appending) a _gen.go file.
func add(c config, args ...string) error {
if len(args) == 0 {
return fmt.Errorf("please specify the import path of the typewriter you wish to add")
}
imports, err := getTypewriterImports(c)
if err != nil {
return err
}
for _, arg := range args {
imp := typewriter.ImportSpec{Name: "_", Path: arg}
// try to go get it
cmd := exec.Command("go", "get", imp.Path)
cmd.Stdout = c.out
if err := cmd.Run(); err != nil {
return err
}
imports.Add(imp)
}
if createCustomFile(c, imports); err != nil {
return err
}
return nil
}
func createCustomFile(c config, imports typewriter.ImportSpecSet) error {
w, err := os.Create(c.customName)
if err != nil {
return err
}
defer w.Close()
p := pkg{
Name: "main",
Imports: imports,
}
if err := tmpl.Execute(w, p); err != nil {
return err
}
return nil
}