-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
65 lines (55 loc) · 1.68 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"context"
"fmt"
"os"
"strings"
"github.com/google/go-github/v53/github"
"github.com/jharshman/fwsync/cmd"
"github.com/jharshman/fwsync/internal/auth"
"github.com/spf13/cobra"
)
var version string
func main() {
rootCmd := &cobra.Command{
Use: "fwsync",
Short: "A CLI utility to keep your development VM firewall up to date.",
Long: `fwsync uses a local file to keep track of the latest IP addresses you've been
connecting from and keeps your development VM firewall rule up to date with that list.`,
PersistentPreRunE: auth.Auth(),
}
versionCmd := &cobra.Command{
Use: "version",
Short: "Display version information",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(version)
},
}
rootCmd.AddCommand(cmd.Initialize())
rootCmd.AddCommand(cmd.Update())
rootCmd.AddCommand(cmd.List())
rootCmd.AddCommand(cmd.Sync())
rootCmd.AddCommand(versionCmd)
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Println("Operation complete, no errors.")
err := notifyIfUpdateAvailable()
if err != nil {
fmt.Fprintf(os.Stderr, "Error checking for updates to fwsync: %q", err)
}
}
func notifyIfUpdateAvailable() error {
cli := github.NewClient(nil)
repoRelease, _, err := cli.Repositories.GetLatestRelease(context.Background(), "jharshman", "fwsync")
if err != nil {
return err
}
latest := strings.TrimPrefix(repoRelease.GetTagName(), "v")
if latest != version {
fmt.Printf("\n\033[0;32mA new version (%q) is available for fwsync.\033[0m\n", latest)
fmt.Printf("\033[0;32mTo update run:\ncurl https://raw.githubusercontent.com/jharshman/fwsync/master/install.sh | sh\033[0m\n")
}
return nil
}