Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: Skip synchronization if IPs are up-to-date. #3

Merged
merged 1 commit into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func Update() *cobra.Command {

// Local variable shared between the closures.
var local *user.Config
var skipSync bool

return &cobra.Command{
Use: "update",
Expand All @@ -36,6 +37,7 @@ func Update() *cobra.Command {
currentIP, _ := user.PublicIP()
_, ipExists := cfg.HasIP(currentIP)
if ipExists {
skipSync = true
return nil
}

Expand All @@ -53,6 +55,10 @@ func Update() *cobra.Command {
return cfg.Write(f)
},
PostRunE: func(cmd *cobra.Command, args []string) error {
if skipSync {
fmt.Println("IPs are up-to-date, skipping sync.")
return nil
}
fmt.Println("syncing firewall rule")
return synchronize(local)
},
Expand Down
18 changes: 14 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,28 @@ connecting from and keeps your development VM firewall rule up to date with that
}
fmt.Println("Operation complete, no errors.")

notifyIfUpdateAvailable()
err := notifyIfUpdateAvailable()
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}

func notifyIfUpdateAvailable() {
cli, _ := ghapi.DefaultRESTClient()
func notifyIfUpdateAvailable() error {
cli, err := ghapi.DefaultRESTClient()
if err != nil {
return err
}
latestTag := struct {
Tag string `json:"tag_name"`
}{}
cli.Get("repos/jharshman/fwsync/releases/latest", &latestTag)
err = cli.Get("repos/jharshman/fwsync/releases/latest", &latestTag)
if err != nil {
return err
}
tag := strings.TrimPrefix(latestTag.Tag, "v")
if tag != version {
fmt.Printf("\n\033[0;32mA new version (%q) is available for fwsync.\033[0m\n", latestTag.Tag)
fmt.Printf("\033[0;32mTo update run:\ncurl https://raw.githubusercontent.com/jharshman/fwsync/master/install.sh | sh\033[0m\n")
}
return nil
}
Loading