github.com/markbates/grift@v1.5.0/cli/run.go (about) 1 package cli 2 3 import ( 4 "context" 5 "html/template" 6 "os" 7 "os/exec" 8 "path" 9 "path/filepath" 10 11 "github.com/gobuffalo/here" 12 ) 13 14 func Run(ctx context.Context, args []string) error { 15 h := here.New() 16 17 info, err := h.Current() 18 if err != nil { 19 return err 20 } 21 22 k := struct { 23 Pkg string 24 Command string 25 Dir string 26 }{ 27 Pkg: path.Join(info.ImportPath, "grifts"), 28 Command: "grift", 29 Dir: info.Dir, 30 } 31 32 if s, ok := ctx.Value("command").(string); ok { 33 k.Command = s 34 } 35 36 od := filepath.Join(info.Dir, ".grifter") 37 out := filepath.Join(od, "main.go") 38 39 os.MkdirAll(od, 0755) 40 defer os.RemoveAll(od) 41 defer func() { 42 if err := recover(); err != nil { 43 os.RemoveAll(od) 44 } 45 }() 46 47 f, err := os.Create(out) 48 if err != nil { 49 return err 50 } 51 defer f.Close() 52 53 mpl, err := template.New("main.go").Parse(tmpl) 54 if err != nil { 55 return err 56 } 57 58 if err := mpl.Execute(f, k); err != nil { 59 return err 60 } 61 62 cargs := []string{"run", "-tags", "sqlite", out} 63 cargs = append(cargs, args...) 64 65 c := exec.CommandContext(ctx, "go", cargs...) 66 c.Stdin = Stdin(ctx) 67 c.Stdout = Stdout(ctx) 68 c.Stderr = Stderr(ctx) 69 70 if err := c.Run(); err != nil { 71 return err 72 } 73 return nil 74 }