libfetch

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2026 License: MIT Imports: 15 Imported by: 0

README

libfetch

A Go library for downloading, comparing, and installing GitHub release assets with a chainable API.

Installation

go get -u github.com/cyberhan123/libfetch

Quick Start

package main

import (
	"fmt"
	"github.com/cyberhan123/libfetch"
)

func main() {
	// Create API instance with default settings
	api := libfetch.NewApi()
	
	// Set installation directory and other options
	api.SetInstallDir("./install")
	api.SetRetryCount(5)
	api.SetRetryTimeDelay(3) // 3 seconds
	api.SetProxy("http://proxy.example.com:8080")
	
	// Download and install the latest release
	err := api.Repo("owner/repo").Latest().Install(func(version string) string {
		// Return asset name based on version
		return fmt.Sprintf("asset-%s.zip", version)
	})
	
	if err != nil {
		fmt.Printf("Error: %v\n", err)
		return
	}
	
	fmt.Println("Installation completed successfully!")
	
	// Check installed version
	versionInfo, err := api.Repo("owner/repo").GetInstalledVersion()
	if err != nil {
		fmt.Printf("Error getting installed version: %v\n", err)
		return
	}
	
	fmt.Printf("Installed version: %s\n", versionInfo.TagName)
}

Configuration

Environment Variables
  • HTTP_PROXY - HTTP proxy URL
  • HTTPS_PROXY - HTTPS proxy URL (used if HTTP_PROXY is not set)

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Development Setup
  1. Fork the repository
  2. Clone your fork
  3. Create a feature branch
  4. Make changes
  5. Run tests
  6. Submit a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

If you encounter any issues or have questions, please open an issue on GitHub.


Made with ❤️ in Go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultProgressTracker

func DefaultProgressTracker() getter.ProgressTracker

DefaultProgressTracker returns the default ProgressTracker that prints download progress to stdout.

Types

type Api

type Api struct {
	// contains filtered or unexported fields
}

Api 结构体用于配置和执行下载安装操作

func NewApi

func NewApi() *Api

NewApi 创建新的 Api 实例,默认读取环境变量获取 HTTP 代理

func (*Api) Repo

func (a *Api) Repo(repo string) *RepoApi

Repo 设置 GitHub 仓库,返回 RepoApi

func (*Api) SetInstallDir

func (a *Api) SetInstallDir(dir string) *Api

SetInstallDir 设置安装目录

func (*Api) SetProgressTracker

func (a *Api) SetProgressTracker(pt getter.ProgressTracker) *Api

SetProgressTracker 设置进度跟踪器

func (*Api) SetProxy

func (a *Api) SetProxy(proxy string) *Api

SetProxy 设置 HTTP 代理

func (*Api) SetRetryCount

func (a *Api) SetRetryCount(count int) *Api

SetRetryCount 设置重试次数

func (*Api) SetRetryTimeDelay

func (a *Api) SetRetryTimeDelay(seconds int) *Api

SetRetryTimeDelay 设置重试延迟时间(秒)

type Downloader

type Downloader struct {
	// RetryCount is how many times the package will retry to obtain the latest version.
	RetryCount int
	// RetryDelay is the delay between retries when obtaining the latest version.
	RetryDelay time.Duration
	// ApiURL is the GitHub API URL for fetching the latest release.
	ApiURL string
	// Repo is the GitHub repository in format "owner/repo".
	Repo string
	// Proxy is the HTTP proxy to use for downloads.
	Proxy string
	// ProgressTracker is the progress tracker to use for downloads.
	ProgressTracker getter.ProgressTracker
}

func NewDownloader

func NewDownloader(repo string) *Downloader

func NewDownloaderWithConfig

func NewDownloaderWithConfig(repo string, retryCount int, retryDelay time.Duration, proxy string, progressTracker getter.ProgressTracker) *Downloader

func (*Downloader) DownloadAsset

func (f *Downloader) DownloadAsset(assetName, version, dest string) error

DownloadAsset downloads a specific asset from the latest release of the repository. assetName is the name of the asset to download. dest is the destination directory for the downloaded asset.

func (*Downloader) DownloadAssetWithContext

func (f *Downloader) DownloadAssetWithContext(ctx context.Context, assetName, version, dest string) error

DownloadAssetWithContext downloads a specific asset from a release using the provided context and progress tracker. assetName is the name of the asset to download. version is the release version to download from (empty string for latest). dest is the destination directory for the downloaded asset.

func (*Downloader) DownloadLatestAsset

func (f *Downloader) DownloadLatestAsset(pattern string, dest string) error

DownloadLatestAsset downloads the latest asset that matches a pattern from the repository. pattern is a regex pattern to match against asset names. dest is the destination directory for the downloaded asset.

func (*Downloader) GetLatestReleaseAssets

func (f *Downloader) GetLatestReleaseAssets() ([]string, error)

GetLatestReleaseAssets returns a list of asset names from the latest release.

func (*Downloader) GetReleaseAssetURL

func (f *Downloader) GetReleaseAssetURL(assetName string) (string, error)

GetReleaseAssetURL returns the download URL for a specific asset in the latest release.

func (*Downloader) GetReleaseAssetURLByVersion

func (f *Downloader) GetReleaseAssetURLByVersion(assetName, version string) string

GetReleaseAssetURLByVersion returns the download URL for a specific asset in a specific release version.

func (*Downloader) LatestVersion

func (f *Downloader) LatestVersion() (string, error)

LatestVersion fetches the latest release tag from the GitHub API for the specified repository.

type Install

type Install struct {
	InstallPath string
	Downloader  *Downloader
	// contains filtered or unexported fields
}

Install struct holds common variables for installation operations

func NewInstall

func NewInstall(repo string, installPath string) *Install

NewInstall creates a new Install instance with default values

func (*Install) CreateVersionFile

func (i *Install) CreateVersionFile(version string) error

CreateVersionFile creates a version info file in the specified directory.

func (*Install) GetInstalledVersion

func (i *Install) GetInstalledVersion() (*VersionInfo, error)

GetInstalledVersion returns the installed version information for the specified path.

func (*Install) InstallAsset

func (i *Install) InstallAsset(assetName string, version string, allowUpgrade bool) error

type RepoApi

type RepoApi struct {
	// contains filtered or unexported fields
}

RepoApi 结构体用于指定 GitHub 仓库

func (*RepoApi) GetInstalledVersion

func (r *RepoApi) GetInstalledVersion() (*VersionInfo, error)

GetInstalledVersion 获取已安装的版本信息

func (*RepoApi) Latest

func (r *RepoApi) Latest() *VersionApi

Latest 设置为下载最新版本,返回 VersionApi

func (*RepoApi) Version

func (r *RepoApi) Version(version string) *VersionApi

Version 设置具体版本,返回 VersionApi

type VersionApi

type VersionApi struct {
	// contains filtered or unexported fields
}

VersionApi 结构体用于指定版本

func (*VersionApi) Install

func (v *VersionApi) Install(assetFunc func(version string) string) error

Install 安装指定的资产 assetFunc 是一个回调函数,根据版本号生成资产文件名

type VersionInfo

type VersionInfo struct {
	TagName string `json:"tag_name"`
	Repo    string `json:"repo"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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