-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
68 lines (58 loc) · 1.61 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package gifsicle
import (
"bytes"
"embed"
"fmt"
"image/gif"
"io"
"path/filepath"
"runtime"
"strings"
"github.com/munchpass/gifsicle-go/embedbinwrapper"
)
//go:embed bin/*
var binariesFs embed.FS
func createBinWrapper(binaryName string) (*embedbinwrapper.EmbedBinWrapper, error) {
b := embedbinwrapper.NewExecutableBinWrapper()
switch runtime.GOOS {
case "windows":
binPath := fmt.Sprintf("bin/win/x64/%s", binaryName)
ext := strings.ToLower(filepath.Ext(binPath))
if ext != ".exe" {
binPath += ".exe"
}
binary, err := binariesFs.ReadFile(binPath)
if err != nil {
return nil, fmt.Errorf("failed to read embed binary: %s", err)
}
return b.Src(embedbinwrapper.NewSrc().Bin(binary).Os("win32")), nil
case "linux":
binary, err := binariesFs.ReadFile(fmt.Sprintf("bin/linux/x64/%s", binaryName))
if err != nil {
return nil, fmt.Errorf("failed to read embed binary: %s", err)
}
return b.Src(embedbinwrapper.NewSrc().Bin(binary).Os("linux")), nil
case "darwin":
binary, err := binariesFs.ReadFile(fmt.Sprintf("bin/macos/%s", binaryName))
if err != nil {
return nil, fmt.Errorf("failed to read embed binary: %s", err)
}
return b.Src(embedbinwrapper.NewSrc().Bin(binary).Os("darwin")), nil
default:
return nil, fmt.Errorf("unsupported OS %s", runtime.GOOS)
}
}
func createReaderFromGif(g *gif.GIF) (io.Reader, error) {
var buffer bytes.Buffer
err := gif.EncodeAll(&buffer, g)
return &buffer, err
}
func version(b *embedbinwrapper.EmbedBinWrapper) (string, error) {
b.Reset()
err := b.Run("--version")
if err != nil {
return "", err
}
v := string(b.StdOut())
return v, nil
}