-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
34 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
|
@@ -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", | ||
|
@@ -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()) | ||
} | ||
} |