version

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README

Version

The version package provides utilities to fetch and compare software version information from GitHub releases. It helps check if a newer version is available and generates update notices.

Features

  • Fetch Release Information: Retrieve the latest release details from a GitHub repository.
  • Version Comparison: Compare semantic versions to determine if an update is needed.
  • Update Notifications: Generate user-friendly messages if a newer version is available.

Installation

To include this package in your Go project, use:

go get github.com/raystack/salt/version

Usage

1. Fetching Release Information

You can use the ReleaseInfo function to fetch the latest release details from a GitHub repository.

package main

import (
    "fmt"
    "github.com/raystack/salt/version"
)

func main() {
    releaseURL := "https://api.github.com/repos/raystack/optimus/releases/latest"
    info, err := version.ReleaseInfo(releaseURL)
    if err != nil {
        fmt.Println("Error fetching release info:", err)
        return
    }
    fmt.Printf("Latest Version: %s\nDownload URL: %s\n", info.Version, info.TarURL)
}
2. Comparing Versions

Use IsCurrentLatest to check if the current version is up-to-date with the latest release.

currVersion := "1.2.3"
latestVersion := "1.2.4"
isLatest, err := version.IsCurrentLatest(currVersion, latestVersion)
if err != nil {
    fmt.Println("Error comparing versions:", err)
} else if isLatest {
    fmt.Println("You are using the latest version!")
} else {
    fmt.Println("A newer version is available.")
}
3. Generating Update Notices

UpdateNotice generates a message prompting the user to update if a newer version is available.

notice := version.UpdateNotice("1.0.0", "raystack/optimus")
if notice != "" {
    fmt.Println(notice)
} else {
    fmt.Println("You are up-to-date!")
}

API Reference

Functions
  • ReleaseInfo(releaseURL string) (*Info, error): Fetches the latest release information from the given GitHub API URL.
  • IsCurrentLatest(currVersion, latestVersion string) (bool, error): Compares the current version with the latest version using semantic versioning.
  • UpdateNotice(currentVersion, githubRepo string) string: Returns an update notice if a newer version is available, or an empty string if up-to-date.
Structs
  • type Info: Contains details about a release.
    • Version: The version string (e.g., "v1.2.3").
    • TarURL: The tarball URL for downloading the release.

Environment Variables

  • The User-Agent header in HTTP requests is set to raystack/salt to comply with GitHub's API requirements.

Error Handling

  • Uses github.com/pkg/errors to wrap errors for better error context.
  • Returns errors when HTTP requests fail, or when JSON parsing or version comparison fails.

Dependencies

  • github.com/hashicorp/go-version: For semantic version comparison.
  • github.com/pkg/errors: For enhanced error wrapping.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ReleaseInfoTimeout sets the HTTP client timeout for fetching release info.
	ReleaseInfoTimeout = time.Second * 1

	// Release is the GitHub API URL template to fetch the latest release of a repository.
	Release = "https://api.github.com/repos/%s/releases/latest"
)

Functions

func IsCurrentLatest

func IsCurrentLatest(currVersion, latestVersion string) (bool, error)

IsCurrentLatest compares the current version with the latest version.

Parameters:

  • currVersion: The current version string.
  • latestVersion: The latest version string.

Returns:

  • true if the current version is greater than or equal to the latest version.
  • An error if version parsing fails.

func UpdateNotice

func UpdateNotice(currentVersion, githubRepo string) string

UpdateNotice generates a notice message if a newer version is available.

Parameters:

  • currentVersion: The current version string.
  • githubRepo: The GitHub repository in the format "owner/repo".

Returns:

  • A string message prompting the user to update if a newer version is available.
  • An empty string if there are no updates or if any errors occur.

Types

type Info

type Info struct {
	Version string // Version of the release
	TarURL  string // Tarball URL of the release
}

Info holds information about a software release.

func ReleaseInfo

func ReleaseInfo(releaseURL string) (*Info, error)

ReleaseInfo fetches details related to the latest release from the provided URL.

Parameters:

Returns:

  • An *Info struct containing the version and tarball URL.
  • An error if the HTTP request or response parsing fails.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL