binwrapper

package module
v0.0.0-...-4b8ff14 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2024 License: MIT Imports: 16 Imported by: 0

README

Golang Binary Wrapper

Codacy Badge

Inspired by and partially ported from npm package bin-wrapper

Install

go get -u github.com/hi-solutions/go-binwrapper

Example of usage

Create wrapper for cwebp

package main

import (
	"github.com/hi-solutions/go-binwrapper"
	"fmt"
)

func main() {
  base := "https://storage.googleapis.com/downloads.webmproject.org/releases/webp/"

  bin := binwrapper.NewBinWrapper().
    Src(
    binwrapper.NewSrc().
      Url(base + "libwebp-0.6.0-mac-10.12.tar.gz").
      Os("darwin")).
    Src(
    binwrapper.NewSrc().
      Url(base + "libwebp-0.6.0-linux-x86-32.tar.gz").
      Os("linux").
      Arch("x86")).
    Src(
    binwrapper.NewSrc().
      Url(base + "libwebp-0.6.0-linux-x86-64.tar.gz").
      Os("linux").
      Arch("x64")).
    Src(
    binwrapper.NewSrc().
      Url(base + "libwebp-0.6.0-windows-x64.zip").
      Os("win32").
      Arch("x64").
      ExecPath("cwebp.exe")).
    Src(
    binwrapper.NewSrc().
      Url(base + "libwebp-0.6.0-windows-x86.zip").
      Os("win32").
      Arch("x86").
      ExecPath("cwebp.exe")).
    Strip(2).
    Dest("vendor/cwebp").
    ExecPath("cwebp")

  err := bin.Run("-version")

  fmt.Printf("stdout: %s\n", string(bin.StdOut))
  fmt.Printf("stderr: %s\n", string(bin.StdErr))
  fmt.Printf("err: %v\n", err)
}

It downloads cwebp distribution according to current platform and runs cwebp with -version argument.

Important note: Many vendors don't provide binaries for some specific platforms. For instance, common linux binaries won't work on alpine linux or arm-based linux. In that case you need to have prebuilt binaries on target platform and use SkipDownload. The example above will look like:

bin = binwrapper.NewBinWrapper().
		SkipDownload().
		ExecPath("cwebp")

Now binwrapper will run cwebp located in PATH

Use Dest to specify directory with binary:

bin = binwrapper.NewBinWrapper().
    SkipDownload().
    Dest("/path/to/directory").
    ExecPath("cwebp")

Documentation

Overview

Package binwrapper provides executable wrapper that makes command line tools seamlessly available as local golang dependencies. Inspired by and partially ported from npm package bin-wrapper: https://github.com/kevva/bin-wrapper

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BinWrapper

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

BinWrapper wraps executable and provides convenient methods to interact with

func NewBinWrapper

func NewBinWrapper() *BinWrapper

NewBinWrapper creates BinWrapper instance

Example

Example of wrapping cwebp command line tool

base := "https://storage.googleapis.com/downloads.webmproject.org/releases/webp/"

bin := binwrapper.NewBinWrapper().
	Src(
		binwrapper.NewSrc().
			URL(base + "libwebp-0.6.0-mac-10.12.tar.gz").
			Os("darwin")).
	Src(
		binwrapper.NewSrc().
			URL(base + "libwebp-0.6.0-linux-x86-32.tar.gz").
			Os("linux").
			Arch("x86")).
	Src(
		binwrapper.NewSrc().
			URL(base + "libwebp-0.6.0-linux-x86-64.tar.gz").
			Os("linux").
			Arch("x64")).
	Src(
		binwrapper.NewSrc().
			URL(base + "libwebp-0.6.0-windows-x64.zip").
			Os("win32").
			Arch("x64").
			ExecPath("cwebp.exe")).
	Src(
		binwrapper.NewSrc().
			URL(base + "libwebp-0.6.0-windows-x86.zip").
			Os("win32").
			Arch("x86").
			ExecPath("cwebp.exe")).
	Strip(2).
	Dest("vendor/cwebp").
	ExecPath("cwebp")

err := bin.Run("-version")

fmt.Printf("stdout: %s\n", string(bin.StdOut()))
fmt.Printf("stderr: %s\n", string(bin.StdErr()))
fmt.Printf("err: %v\n", err)
Output:

func (*BinWrapper) Arg

func (b *BinWrapper) Arg(name string, values ...string) *BinWrapper

Arg adds command line argument to run the binary with.

func (*BinWrapper) Args

func (b *BinWrapper) Args() []string

Args returns arguments were added with Arg method

func (*BinWrapper) AutoExe

func (b *BinWrapper) AutoExe() *BinWrapper

AutoExe adds .exe extension for windows executable path

func (*BinWrapper) CombinedOutput

func (b *BinWrapper) CombinedOutput() []byte

CombinedOutput returns combined executable's stdout and stderr

func (*BinWrapper) Debug

func (b *BinWrapper) Debug() *BinWrapper

Debug enables debug output

func (*BinWrapper) Dest

func (b *BinWrapper) Dest(dest string) *BinWrapper

Dest accepts a path which the files will be downloaded to

func (*BinWrapper) Env

func (b *BinWrapper) Env(env []string) *BinWrapper

Env specifies the environment of the executable. If Env is nil, Run uses the current process's environment. Elements of env should be in form: "ENV_VARIABLE_NAME=value"

func (*BinWrapper) ExecPath

func (b *BinWrapper) ExecPath(execPath string) *BinWrapper

ExecPath define a file to use as the binary

func (*BinWrapper) Kill

func (b *BinWrapper) Kill() error

Kill terminates the process

func (*BinWrapper) Path

func (b *BinWrapper) Path() string

Path returns the full path to the binary

func (*BinWrapper) Reset

func (b *BinWrapper) Reset() *BinWrapper

Reset removes all arguments set with Arg method, cleans StdOut and StdErr

func (*BinWrapper) Run

func (b *BinWrapper) Run(arg ...string) error

Run runs the binary with provided arg list. Arg list is appended to args set through Arg method Returns context.DeadlineExceeded in case of timeout

func (*BinWrapper) SetStdOut

func (b *BinWrapper) SetStdOut(writer io.Writer) *BinWrapper

SetStdOut set writer to write executable's stdout

func (*BinWrapper) SkipDownload

func (b *BinWrapper) SkipDownload() *BinWrapper

SkipDownload skips downloading a file

func (*BinWrapper) Src

func (b *BinWrapper) Src(src *Src) *BinWrapper

Src adds a Src to BinWrapper

func (*BinWrapper) StdErr

func (b *BinWrapper) StdErr() []byte

StdErr returns the executable's stderr after Run was called

func (*BinWrapper) StdIn

func (b *BinWrapper) StdIn(reader io.Reader) *BinWrapper

StdIn sets reader to read executable's stdin from

func (*BinWrapper) StdOut

func (b *BinWrapper) StdOut() []byte

StdOut returns the binary's stdout after Run was called

func (*BinWrapper) Strip

func (b *BinWrapper) Strip(value int) *BinWrapper

Strip strips a number of leading paths from file names on extraction.

func (*BinWrapper) Timeout

func (b *BinWrapper) Timeout(timeout time.Duration) *BinWrapper

Timeout sets timeout for the command. By default it's 0 (binary will run till end).

type Src

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

Src defines executable source

func NewSrc

func NewSrc() *Src

NewSrc creates new Src instance

func (*Src) Arch

func (s *Src) Arch(value string) *Src

Arch tie the source to a specific arch. Possible values are same as runtime.GOARCH

func (*Src) ExecPath

func (s *Src) ExecPath(value string) *Src

ExecPath tie the src to a specific binary file

func (*Src) Os

func (s *Src) Os(value string) *Src

Os tie the source to a specific OS. Possible values are same as runtime.GOOS

func (*Src) URL

func (s *Src) URL(value string) *Src

URL sets a url pointing to a file to download.

Jump to

Keyboard shortcuts

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