-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
89 lines (76 loc) · 2 KB
/
main.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
// Based on gonew experimental implementation
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"github.com/gophers-latam/boiler/internal"
)
func main() {
log.SetPrefix("boiler: ")
log.SetFlags(0)
flag.Usage = internal.Help
flag.Parse()
args := flag.Args()
if len(args) < 1 || len(args) > 3 {
// Mostrar salida de ayuda
internal.Help()
}
// Comprobar fuente
srcMod := args[0]
srcModVers := srcMod
if !strings.Contains(srcModVers, "@") {
srcModVers += "@latest"
}
srcMod, _, _ = strings.Cut(srcMod, "@")
internal.VerifyPath(srcMod, "src")
// Comprobar destino
dstMod := srcMod
if len(args) >= 2 {
dstMod = args[1]
internal.VerifyPath(dstMod, "dst")
}
// Comprobar directorio en ruta
var dir string
if len(args) == 3 {
dir = args[2]
} else {
dir = "." + string(filepath.Separator) + path.Base(dstMod)
}
// "dir" no debe existir o debe ser un directorio vacío.
dest, err := os.ReadDir(dir)
if err == nil && len(dest) > 0 {
msg := fmt.Sprintf("directorio destino %s existe y no está vacío", dir)
internal.CheckErr(msg+"\n%v", err)
}
needMkdir := err != nil
// Descargar fuente
var stdOut, stdErr bytes.Buffer
cmd := exec.Command("go", "mod", "download", "-json", srcModVers)
cmd.Stdout, cmd.Stderr = &stdOut, &stdErr
if err := cmd.Run(); err != nil {
msg := fmt.Sprintf("go mod download -json %s: %v\n%s%s", srcModVers, err, stdErr.Bytes(), stdOut.Bytes())
internal.CheckErr(msg+"\n%v", err)
}
info := &internal.Info{}
if err := json.Unmarshal(stdOut.Bytes(), &info); err != nil {
msg := fmt.Sprintf("go mod download -json %s: salida JSON no válida: %v\n%s%s", srcMod, err, stdErr.Bytes(), stdOut.Bytes())
internal.CheckErr(msg+"\n%v", err)
}
if needMkdir {
err := os.MkdirAll(dir, 0o777)
internal.CheckErr("", err)
}
// Editar destino
err = internal.Edit(info, dir, srcMod, dstMod)
internal.CheckErr("", err)
// Salida exitosa
log.Printf("Se inicializó: %s en %s", dstMod, dir)
}