Skip to content

Commit

Permalink
Implement check for updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoisaiah committed Feb 22, 2021
1 parent a48e9c8 commit 249db54
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions cmd/f2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,37 @@ package main

import (
"fmt"
"net/http"
"os"
"time"

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

func checkForUpdates(app *cli.App) {
fmt.Println("Checking for updates...")

c := http.Client{Timeout: 20 * time.Second}
resp, err := c.Get("https://github.com/ayoisaiah/f2/releases/latest")
if err != nil {
fmt.Println("HTTP Error: Failed to check for update")
return
}
var version string
_, err = fmt.Sscanf(resp.Request.URL.String(), "https://github.com/ayoisaiah/f2/releases/tag/%s", &version)
if err != nil {
fmt.Println("Failed to get latest version")
return
}
version = "v" + version

if version == app.Version {
fmt.Printf("Congratulations, you are using the latest version of %s\n", app.Name)
} else {
fmt.Printf("%s: %s at %s\n", green("Update available"), version, resp.Request.URL.String())
}
}

func getApp() *cli.App {
return &cli.App{
Name: "F2",
Expand All @@ -16,9 +42,10 @@ func getApp() *cli.App {
Email: "[email protected]",
},
},
Usage: "F2 is a command-line tool for batch renaming multiple files and directories quickly and safely",
UsageText: "FLAGS [OPTIONS] [PATHS...]",
Version: "v1.0.0",
Usage: "F2 is a command-line tool for batch renaming multiple files and directories quickly and safely",
UsageText: "FLAGS [OPTIONS] [PATHS...]",
Version: "v1.0.0",
EnableBashCompletion: true,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "find",
Expand Down Expand Up @@ -131,4 +158,8 @@ func main() {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

if contains(os.Args, "-v") || contains(os.Args, "--version") {
checkForUpdates(getApp())
}
}

0 comments on commit 249db54

Please sign in to comment.