ghdl

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2022 License: MIT Imports: 16 Imported by: 0

README

ghdl

Memorize ghdl as github download

ghdl is a fast and simple program (and also a golang module) for downloading and installing executable binary from github releases.

animated demo

The demo above extracts fd execuable to current working directory and give execute permission to it.

Features

  • Auto decompressing and unarchiving the downloaded asset (without any system dependencies like tar or unzip).

    Currently supporting unarchiving `tar` and decompressing `zip` `gzip`.
    Package format `deb` `rpm` `apk` will be downloaded directly
    
  • Setups for executable: ghdl moves executable to specified location and add execute permissions to the file.

  • Auto filtering: multiple assets in one release will be filtered by OS or ARCH. This feature can be disabled using -F flag.

  • Interactive TUI: when auto filtering is failed or returned multiple options, you can select assets in a interactive way, with vim key bindings support.

  • Release tags: ghdl downloads latest release by default, other or old tagged releases can be downloaded by specifying release tag: username/repo#tagname

  • Inspect download status with real-time progress bar.

Installation

If you're going to use ghdl as a go module, ignore the following installation progress.

  • Using Go tools:

    go will download the latest version of ghdl to $GOPATH/bin, please make sure $GOPATH is in the PATH:

    go install github.com/beetcb/ghdl/ghdl@latest
    
  • Download and run executable from release.

  • Run the following shell script(*nix system only):

    curl -fsSL "https://bina.egoist.sh/beetcb/ghdl?dir=/usr/local/bin" | sh
    # feel free to change the `dir` url param to specify the installation directory.
    

Usage

CLI

Run ghdl --help

ghdl --help
    ghdl download binary from github release
    ghdl handles archived or compressed file as well

    Usage:
    ghdl <user/repo[#tagname]> [flags]

    Flags:
    -h, --help          help for ghdl
    -n, --name string   specify binary file name
    -p, --path path     save binary to path (default ".")

It's tedious to specify -p manually, we can alias ghdl -p "$DirInPath" to a shorthand command, then use it as a executable installer.

Go Module

Require ghdl to go.mod

go get github.com/beetcb/ghdl

Use ghdl's out-of-box utilities:

package main

import (
	"fmt"
	"os"

	"github.com/beetcb/ghdl"
	h "github.com/beetcb/ghdl/helper"
)

func main() {
	ghRelease := ghdl.GHRelease{RepoPath: "sharkdp/fd"}
	ghReleaseDl, err := ghRelease.GetGHReleases()
	if err != nil {
		h.Print(fmt.Sprintf("get gh releases failed: %s", err), h.PrintModeErr)
		os.Exit(1)
	}

	if err := ghReleaseDl.DlTo("."); err != nil {
		h.Print(fmt.Sprintf("download failed: %s", err), h.PrintModeErr)
		os.Exit(1)
	}

	if err := ghReleaseDl.ExtractBinary(); err != nil {
		switch err {
		case ghdl.NeedInstallError:
			h.Print(fmt.Sprintf("%s. You can install %s with the appropriate commands", err, ghReleaseDl.BinaryName), h.PrintModeInfo)
			os.Exit(0)
		case ghdl.NoBinError:
			h.Print(fmt.Sprintf("%s. Try to specify binary name flag", err), h.PrintModeInfo)
			os.Exit(0)
		default:
			h.Print(fmt.Sprintf("extract failed: %s", err), h.PrintModeErr)
			os.Exit(1)
		}
	}

	h.Print(fmt.Sprintf("saved executable to %s", ghReleaseDl.BinaryName), h.PrintModeSuccess)
    
	if err := os.Chmod(ghReleaseDl.BinaryName, 0777); err != nil {
		h.Print(fmt.Sprintf("chmod failed: %s", err), h.PrintModeErr)
	}
}

Credit

Inspired by egoist/bina, TUI powered by charmbracelet/bubbletea

License

Licensed under MIT

Author: @beetcb | Email: i@beetcb.com

Documentation

Index

Constants

View Source
const (
	OS   = runtime.GOOS
	ARCH = runtime.GOARCH
)

Variables

View Source
var (
	ErrNeedInstall = errors.New(
		"detected deb/rpm/apk package, download directly")
	ErrNoBin = errors.New("binary file not found")
)

Functions

This section is empty.

Types

type APIReleaseAsset

type APIReleaseAsset struct {
	Name        string `json:"name"`
	DownloadUrl string `json:"browser_download_url"`
	Size        int    `json:"size"`
}

type APIReleaseResp

type APIReleaseResp struct {
	Assets []APIReleaseAsset `json:"assets"`
}

type GHRelease

type GHRelease struct {
	RepoPath string
	TagName  string
}

func (GHRelease) GetGHReleases

func (gr GHRelease) GetGHReleases(filterOff bool) (*GHReleaseDl, error)

type GHReleaseDl

type GHReleaseDl struct {
	BinaryName string
	Url        string
	Size       int64
}

func (*GHReleaseDl) DlTo

func (dl *GHReleaseDl) DlTo(path string) (err error)

Download asset from github release to `path`

dl.BinaryName shall be replaced with absolute path mutably

func (GHReleaseDl) ExtractBinary

func (dl GHReleaseDl) ExtractBinary() error

Extract binary file from the downloaded temporary file.

Currently supporting unarchiving `tar` and decompressing `zip` `gzip`.

Package format `deb` `rpm` `apk` will be downloaded directly

func (GHReleaseDl) UnGzBinary

func (GHReleaseDl) UnGzBinary(r *os.File) (*gzip.Reader, error)

func (GHReleaseDl) UnTargzBinary

func (dl GHReleaseDl) UnTargzBinary(r *os.File) (*tar.Reader, error)

func (GHReleaseDl) UnZipBinary

func (dl GHReleaseDl) UnZipBinary(r *os.File) (*zip.File, error)

Directories

Path Synopsis
pg
sl

Jump to

Keyboard shortcuts

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