github.com/creativeprojects/go-selfupdate@v1.2.0/cmd/detect-latest-release/update.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"log"
     8  	"os"
     9  	"runtime"
    10  
    11  	"github.com/creativeprojects/go-selfupdate"
    12  )
    13  
    14  // keep this function here, this is the example from the README
    15  func update(version string) error {
    16  	latest, found, err := selfupdate.DetectLatest(context.Background(), selfupdate.ParseSlug("creativeprojects/resticprofile"))
    17  	if err != nil {
    18  		return fmt.Errorf("error occurred while detecting version: %w", err)
    19  	}
    20  	if !found {
    21  		return fmt.Errorf("latest version for %s/%s could not be found from github repository", runtime.GOOS, runtime.GOARCH)
    22  	}
    23  
    24  	if latest.LessOrEqual(version) {
    25  		log.Printf("Current version (%s) is the latest", version)
    26  		return nil
    27  	}
    28  
    29  	exe, err := os.Executable()
    30  	if err != nil {
    31  		return errors.New("could not locate executable path")
    32  	}
    33  	if err := selfupdate.UpdateTo(context.Background(), latest.AssetURL, latest.AssetName, exe); err != nil {
    34  		return fmt.Errorf("error occurred while updating binary: %w", err)
    35  	}
    36  	log.Printf("Successfully updated to version %s", latest.Version())
    37  	return nil
    38  }