Skip to content

Commit

Permalink
feat: ⬆️ add self update command
Browse files Browse the repository at this point in the history
  • Loading branch information
OnCloud125252 committed Apr 21, 2024
1 parent 3e85be6 commit 5cf74da
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
54 changes: 54 additions & 0 deletions commands/update-self.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package commands

import (
"fmt"
"log"
"os"
"os/exec"
"ui/cli/module"

"github.com/gookit/color"
"github.com/urfave/cli/v2"
)

func UpdateSelf(c *cli.Context) {
var cmd = exec.Command("sudo", "-v")
cmd.Stdout = nil
err := cmd.Run()
if err != nil {
log.Fatal(err)
}

color.Yellowln("Updating ui-cli")
fmt.Println()

filePath := module.DownloadFileToCache("https://github.com/Update-Install/CLI/releases/latest/download/ui-cli_linux_amd64")

module.FullWidthMessage("installation log start", color.Gray)
cmd = exec.Command("sudo", "mv", filePath, "/usr/local/bin/ui")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
log.Fatal(err)
}
fmt.Println("Moved", filePath, "to /usr/local/bin")
cmd = exec.Command("sudo", "chmod", "+x", "/usr/local/bin/ui")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
log.Fatal(err)
}
fmt.Println("Changed permission for /usr/local/bin/ui")
module.FullWidthMessage("installation log end", color.Gray)

color.Greenf("Successfully updated ui-cli to ")
cmd = exec.Command("/usr/local/bin/ui", "version")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
log.Fatal(err)
}
}
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ func main() {
return nil
},
},
{
Name: "update-self",
Aliases: []string{"us"},
Usage: "update ui-cli to the latest version",
Action: func(ctx *cli.Context) error {
commands.UpdateSelf(ctx)
return nil
},
},
},
}

Expand Down

0 comments on commit 5cf74da

Please sign in to comment.