From de39918aa854b67317a3abb9e35d6b3ad7470502 Mon Sep 17 00:00:00 2001 From: Odyssey Date: Sat, 11 Feb 2023 10:54:35 +0100 Subject: [PATCH 1/2] Add update command. Signed-off-by: Odyssey --- .gitignore | 3 --- cmd/gothub/update.go | 62 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 cmd/gothub/update.go diff --git a/.gitignore b/.gitignore index 61891e2..6c174c7 100644 --- a/.gitignore +++ b/.gitignore @@ -23,8 +23,5 @@ go.work tmp/ -# Gothub binary. -gothub - # Fiber compressed files *.fiber.gz \ No newline at end of file diff --git a/cmd/gothub/update.go b/cmd/gothub/update.go new file mode 100644 index 0000000..9536289 --- /dev/null +++ b/cmd/gothub/update.go @@ -0,0 +1,62 @@ +package gothub + +import ( + "bytes" + + "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" { + println("The GotHub update command does not work on Docker. Just re-create the container with a new image.") + 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" { + println("GotHub is already up to date.") + os.Exit(0) + } + if err != nil { + 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 { + println("Couldn't build GotHub. ", err) + os.Exit(1) + } else { + println("GotHub updated successfully.") + os.Exit(0) + } + } else { + err := exec.Command("go", "build").Run() + if err != nil { + println("Couldn't build GotHub. ", err) + os.Exit(1) + } else { + println("GotHub updated successfully.") + os.Exit(0) + } + } +} From ef0648cf9ea4b466858c364370cc20c7127465ba Mon Sep 17 00:00:00 2001 From: Odyssey Date: Sat, 11 Feb 2023 12:48:17 +0100 Subject: [PATCH 2/2] Do suggestions by Midou Signed-off-by: Odyssey --- cmd/gothub/update.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/gothub/update.go b/cmd/gothub/update.go index 9536289..a9ed0c9 100644 --- a/cmd/gothub/update.go +++ b/cmd/gothub/update.go @@ -25,7 +25,7 @@ func init() { func update() { if os.Getenv("DOCKER") == "true" { - println("The GotHub update command does not work on Docker. Just re-create the container with a new image.") + 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") @@ -43,7 +43,7 @@ func update() { if runtime.GOOS == "windows" { err := exec.Command("go", "build", "-o", "gothub.exe").Run() if err != nil { - println("Couldn't build GotHub. ", err) + println("Couldn't build GotHub.", err) os.Exit(1) } else { println("GotHub updated successfully.") @@ -52,7 +52,7 @@ func update() { } else { err := exec.Command("go", "build").Run() if err != nil { - println("Couldn't build GotHub. ", err) + println("Couldn't build GotHub.", err) os.Exit(1) } else { println("GotHub updated successfully.")