gitlin/cmd/gothub/update.go
Odyssium 7fe3c52ac9 The Instance Owner Update
Signed-off-by: Odyssium <hi@odyssey346.dev>
2023-04-01 18:53:44 +02:00

64 行
1.3 KiB
Go

package gothub
import (
"bytes"
"fmt"
"github.com/spf13/cobra"
"os"
"os/exec"
"runtime"
)
var updateCmd = &cobra.Command{
Use: "update",
Short: "Update GotHub.",
Long: `This command checks for updates and re-builds GotHub. Does not work on Docker`,
Run: func(cmd *cobra.Command, args []string) {
update()
},
}
func init() {
rootCmd.AddCommand(updateCmd)
}
func update() {
if os.Getenv("DOCKER") == "true" {
fmt.Println("The GotHub update command does not work on Docker. Please re-create the container with a new image instead.")
os.Exit(1)
}
gitPull := exec.Command("git", "pull")
var out bytes.Buffer
gitPull.Stdout = &out
err := gitPull.Run()
if out.String() == "Already up to date.\r" {
fmt.Println("GotHub is already up to date.")
os.Exit(0)
}
if err != nil {
fmt.Println("Couldn't run Git pull. Are you using Git?", err)
os.Exit(1)
}
if runtime.GOOS == "windows" {
err := exec.Command("go", "build", "-o", "gothub.exe").Run()
if err != nil {
fmt.Println("Couldn't build GotHub.", err)
os.Exit(1)
} else {
fmt.Println("GotHub updated successfully.")
os.Exit(0)
}
} else {
err := exec.Command("go", "build").Run()
if err != nil {
fmt.Println("Couldn't build GotHub.", err)
os.Exit(1)
} else {
fmt.Println("GotHub updated successfully.")
os.Exit(0)
}
}
}