1
0
Fork 0
forked from goffee/goffee

add prod mode

This commit is contained in:
Zeni Kim 2024-10-29 06:49:38 -05:00
parent fc32c09fa4
commit 423e87494b
3 changed files with 177 additions and 23 deletions

View file

@ -14,7 +14,7 @@ var cfgFile string
// rootCmd represents the base command when called without any subcommands // rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "goffee", Use: "goffee",
Version: "v1.6.1", Version: "v1.7.1",
Short: "Goffee is the cli tool", Short: "Goffee is the cli tool",
Long: `Goffee is the cli tool for creating new projects and performing other tasks`, Long: `Goffee is the cli tool for creating new projects and performing other tasks`,

View file

@ -19,10 +19,10 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var pid int var piddev int
// runCmd represents the run command // runCmd represents the run command
var runCmd = &cobra.Command{ var runCmdDev = &cobra.Command{
Use: "run:dev", Use: "run:dev",
Short: "Start the app in hot reloading mode", Short: "Start the app in hot reloading mode",
Long: `To Start the app in hot reloading mode for development run the following command: Long: `To Start the app in hot reloading mode for development run the following command:
@ -67,16 +67,16 @@ goffee run:dev
go func() { go func() {
startAppChan <- true startAppChan <- true
}() }()
go startServerJob(pidChan, startAppChan) go startServerJobDev(pidChan, startAppChan)
go startRestartControllerJob(fileChangeChan, pidChan, startAppChan) go startRestartControllerJobDev(fileChangeChan, pidChan, startAppChan)
signal.Notify(termSigsChan, syscall.SIGINT, syscall.SIGTERM) signal.Notify(termSigsChan, syscall.SIGINT, syscall.SIGTERM)
go func(termSigsChan chan os.Signal) { go func(termSigsChan chan os.Signal) {
for { for {
<-termSigsChan <-termSigsChan
if pid == 0 { if piddev == 0 {
os.Exit(0) os.Exit(0)
} else { } else {
pgid, err := syscall.Getpgid(pid) pgid, err := syscall.Getpgid(piddev)
if err != nil { if err != nil {
fmt.Println("error getting pgid: ", err) fmt.Println("error getting pgid: ", err)
} }
@ -98,21 +98,21 @@ goffee run:dev
}, },
} }
func startRestartControllerJob(fileChangeChan chan bool, pidChan chan int, startAppChan chan bool) { func startRestartControllerJobDev(fileChangeChan chan bool, pidChan chan int, startAppChan chan bool) {
for { for {
fileChanged := <-fileChangeChan fileChanged := <-fileChangeChan
if fileChanged { if fileChanged {
fmt.Println("Restarting...") fmt.Println("Restarting...")
pid := <-pidChan piddev := <-pidChan
if pid != 0 { if piddev != 0 {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
killCmd := exec.Command("taskkill", "/T", "/F", "/PID", strconv.Itoa(pid)) killCmd := exec.Command("taskkill", "/T", "/F", "/PID", strconv.Itoa(piddev))
err := killCmd.Run() err := killCmd.Run()
if err != nil { if err != nil {
fmt.Println("error stopping the dev server", err.Error()) fmt.Println("error stopping the dev server", err.Error())
} }
} else if runtime.GOOS == "darwin" { } else if runtime.GOOS == "darwin" {
pgid, err := syscall.Getpgid(pid) pgid, err := syscall.Getpgid(piddev)
if err != nil { if err != nil {
fmt.Println("error getting pgid: ", err.Error()) fmt.Println("error getting pgid: ", err.Error())
} }
@ -121,7 +121,7 @@ func startRestartControllerJob(fileChangeChan chan bool, pidChan chan int, start
fmt.Println("error stopping process: ", err.Error()) fmt.Println("error stopping process: ", err.Error())
} }
} else if runtime.GOOS == "linux" { } else if runtime.GOOS == "linux" {
pgid, err := syscall.Getpgid(pid) pgid, err := syscall.Getpgid(piddev)
if err != nil { if err != nil {
fmt.Println("error getting pgid: ", err.Error()) fmt.Println("error getting pgid: ", err.Error())
} }
@ -147,12 +147,12 @@ func startRestartControllerJob(fileChangeChan chan bool, pidChan chan int, start
} }
} }
func startServerJob(pidChan chan int, startAppChan chan bool) { func startServerJobDev(pidChan chan int, startAppChan chan bool) {
for { for {
shouldStartApp := <-startAppChan shouldStartApp := <-startAppChan
if shouldStartApp { if shouldStartApp {
fmt.Println("\n\nBuilding...") fmt.Println("\n\nBuilding...")
err := compileApp() err := compileAppDev()
if err != nil { if err != nil {
fmt.Println("error building: ", err.Error()) fmt.Println("error building: ", err.Error())
go func() { go func() {
@ -167,7 +167,7 @@ func startServerJob(pidChan chan int, startAppChan chan bool) {
if lookErr != nil { if lookErr != nil {
panic(lookErr) panic(lookErr)
} }
args := []string{"/bin/sh", "-c", execFile} args := []string{"/bin/sh", "-c", execFile, "dev"}
execSpec := &syscall.ProcAttr{ execSpec := &syscall.ProcAttr{
Dir: pwd, Dir: pwd,
Env: os.Environ(), Env: os.Environ(),
@ -176,7 +176,7 @@ func startServerJob(pidChan chan int, startAppChan chan bool) {
Setpgid: true, Setpgid: true,
}, },
} }
pid, _, err = syscall.StartProcess(binary, args, execSpec) piddev, _, err = syscall.StartProcess(binary, args, execSpec)
if err != nil { if err != nil {
fmt.Println("error starting process: ", err.Error()) fmt.Println("error starting process: ", err.Error())
go func() { go func() {
@ -184,7 +184,7 @@ func startServerJob(pidChan chan int, startAppChan chan bool) {
}() }()
} else { } else {
go func() { go func() {
pidChan <- pid pidChan <- piddev
}() }()
} }
} else if runtime.GOOS == "linux" { } else if runtime.GOOS == "linux" {
@ -193,7 +193,7 @@ func startServerJob(pidChan chan int, startAppChan chan bool) {
if lookErr != nil { if lookErr != nil {
panic(lookErr) panic(lookErr)
} }
args := []string{"/bin/sh", "-c", execFile} args := []string{"/bin/sh", "-c", execFile, "dev"}
execSpec := &syscall.ProcAttr{ execSpec := &syscall.ProcAttr{
Dir: pwd, Dir: pwd,
Env: os.Environ(), Env: os.Environ(),
@ -202,7 +202,7 @@ func startServerJob(pidChan chan int, startAppChan chan bool) {
Setpgid: true, Setpgid: true,
}, },
} }
pid, _, err = syscall.StartProcess(binary, args, execSpec) piddev, _, err = syscall.StartProcess(binary, args, execSpec)
if err != nil { if err != nil {
fmt.Println("error starting process: ", err.Error()) fmt.Println("error starting process: ", err.Error())
go func() { go func() {
@ -210,7 +210,7 @@ func startServerJob(pidChan chan int, startAppChan chan bool) {
}() }()
} else { } else {
go func() { go func() {
pidChan <- pid pidChan <- piddev
}() }()
} }
} else { } else {
@ -221,7 +221,7 @@ func startServerJob(pidChan chan int, startAppChan chan bool) {
} }
} }
func compileApp() error { func compileAppDev() error {
pwd, _ := os.Getwd() pwd, _ := os.Getwd()
var command *exec.Cmd var command *exec.Cmd
command = exec.Command("/bin/sh", "-c", fmt.Sprintf("go build -o %v/tmp/", pwd)) command = exec.Command("/bin/sh", "-c", fmt.Sprintf("go build -o %v/tmp/", pwd))
@ -241,5 +241,5 @@ func compileApp() error {
} }
func init() { func init() {
rootCmd.AddCommand(runCmd) rootCmd.AddCommand(runCmdDev)
} }

154
cmd/runprod.go Normal file
View file

@ -0,0 +1,154 @@
// Use of this source code is governed by MIT-style
// license that can be found in the LICENSE file.
package cmd
import (
"fmt"
"os"
"os/exec"
"os/signal"
"path"
"runtime"
"syscall"
"time"
"github.com/spf13/cobra"
)
var pid int
// runCmd represents the run command
var runCmdPro = &cobra.Command{
Use: "run:prod",
Short: "Start the app in production mode",
Long: `To Start the app in production mode the following command:
goffee run:prod
`,
Run: func(cmd *cobra.Command, args []string) {
startAppChan := make(chan bool, 1)
pidChan := make(chan int, 1)
termSigsChan := make(chan os.Signal, 1)
startAppChan <- true
go startServerJob(pidChan, startAppChan)
signal.Notify(termSigsChan, syscall.SIGINT, syscall.SIGTERM)
go func(termSigsChan chan os.Signal) {
for {
<-termSigsChan
if pid == 0 {
os.Exit(0)
} else {
pgid, err := syscall.Getpgid(pid)
if err != nil {
fmt.Println("error getting pgid: ", err)
}
err = syscall.Kill(-pgid, syscall.SIGKILL)
if err != nil {
fmt.Println("error stopping process: ", err)
}
os.Exit(0)
}
}
}(termSigsChan)
func() {
time.Sleep(time.Second * 5)
}()
},
}
func startServerJob(pidChan chan int, startAppChan chan bool) {
for {
shouldStartApp := <-startAppChan
if shouldStartApp {
fmt.Println("\n\nBuilding...")
err := compileApp()
if err != nil {
fmt.Println("error building: ", err.Error())
go func() {
pidChan <- 0
}()
} else {
fmt.Println("Starting...")
pwd, _ := os.Getwd()
if runtime.GOOS == "darwin" {
execFile := pwd + "/tmp/" + path.Base(pwd)
args := []string{execFile, "prod"}
execSpec := &syscall.ProcAttr{
Dir: pwd,
Env: os.Environ(),
Files: []uintptr{os.Stdin.Fd(), os.Stdout.Fd(), os.Stderr.Fd()},
Sys: &syscall.SysProcAttr{
Setpgid: true,
},
}
pid, _, err = syscall.StartProcess(execFile, args, execSpec)
if err != nil {
fmt.Println("error starting process: ", err.Error())
go func() {
pidChan <- 0
}()
} else {
go func() {
pidChan <- pid
}()
}
} else if runtime.GOOS == "linux" {
execFile := pwd + "/tmp/" + path.Base(pwd)
args := []string{execFile, "prod"}
execSpec := &syscall.ProcAttr{
Dir: pwd,
Env: os.Environ(),
Files: []uintptr{os.Stdin.Fd(), os.Stdout.Fd(), os.Stderr.Fd()},
Sys: &syscall.SysProcAttr{
Setpgid: true,
},
}
pid, _, err = syscall.StartProcess(execFile, args, execSpec)
if err != nil {
fmt.Println("error starting process: ", err.Error())
go func() {
pidChan <- 0
}()
} else {
go func() {
pidChan <- pid
}()
}
} else {
fmt.Println("not implemented for os: ", runtime.GOOS)
}
}
}
}
}
func compileApp() error {
pwd, _ := os.Getwd()
var command *exec.Cmd
command = exec.Command("/bin/sh", "-c", fmt.Sprintf("go build -o %v/tmp/", pwd))
command.Env = os.Environ()
command.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
command.Dir = pwd
o, err := command.CombinedOutput()
if string(o) != "" {
fmt.Println(string(o))
}
if err != nil {
return err
}
return nil
}
func init() {
rootCmd.AddCommand(runCmdPro)
}