github.com/creativeprojects/go-selfupdate@v1.2.0/package.go (about)

     1  package selfupdate
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  	"net/http"
     8  )
     9  
    10  // DetectLatest detects the latest release from the repository.
    11  // This function is a shortcut version of updater.DetectLatest with the DefaultUpdater.
    12  func DetectLatest(ctx context.Context, repository Repository) (*Release, bool, error) {
    13  	return DefaultUpdater().DetectLatest(ctx, repository)
    14  }
    15  
    16  // DetectVersion detects the given release from the repository.
    17  func DetectVersion(ctx context.Context, repository Repository, version string) (*Release, bool, error) {
    18  	return DefaultUpdater().DetectVersion(ctx, repository, version)
    19  }
    20  
    21  // UpdateTo downloads an executable from assetURL and replaces the current binary with the downloaded one.
    22  // This function is low-level API to update the binary. Because it does not use a source provider and downloads asset directly from the URL via HTTP,
    23  // this function is not available to update a release for private repositories.
    24  // cmdPath is a file path to command executable.
    25  func UpdateTo(ctx context.Context, assetURL, assetFileName, cmdPath string) error {
    26  	up := DefaultUpdater()
    27  	src, err := downloadReleaseAssetFromURL(ctx, assetURL)
    28  	if err != nil {
    29  		return err
    30  	}
    31  	defer src.Close()
    32  	return up.decompressAndUpdate(src, assetFileName, assetURL, cmdPath)
    33  }
    34  
    35  // UpdateCommand updates a given command binary to the latest version.
    36  // This function is a shortcut version of updater.UpdateCommand using a DefaultUpdater()
    37  func UpdateCommand(ctx context.Context, cmdPath string, current string, repository Repository) (*Release, error) {
    38  	return DefaultUpdater().UpdateCommand(ctx, cmdPath, current, repository)
    39  }
    40  
    41  // UpdateSelf updates the running executable itself to the latest version.
    42  // This function is a shortcut version of updater.UpdateSelf using a DefaultUpdater()
    43  func UpdateSelf(ctx context.Context, current string, repository Repository) (*Release, error) {
    44  	return DefaultUpdater().UpdateSelf(ctx, current, repository)
    45  }
    46  
    47  func downloadReleaseAssetFromURL(ctx context.Context, url string) (rc io.ReadCloser, err error) {
    48  	client := http.DefaultClient
    49  	req, err := http.NewRequest("GET", url, nil)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  	req = req.WithContext(ctx)
    54  	req.Header.Set("Accept", "*/*")
    55  	resp, err := client.Do(req)
    56  	if err != nil {
    57  		return nil, fmt.Errorf("failed to download a release file from %s: %w", url, err)
    58  	}
    59  	if resp.StatusCode >= 300 {
    60  		resp.Body.Close()
    61  		return nil, fmt.Errorf("failed to download a release file from %s: HTTP %d", url, resp.StatusCode)
    62  	}
    63  	return resp.Body, nil
    64  }