-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
189 lines (158 loc) · 4.69 KB
/
main.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2020 Noval Agung Prayogo. All rights reserved.
// Use of this source code is governed by a CC BY-NC-SA 4.0-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
// this var used to store the shortcut file path,
// taken from argument during `go run`
var shortcutFilePath = ""
// these vars below will have values injected during build time
var isRuntime = ""
var osName = ""
var url = "" // during .exe creation
var icon = "" // during .exe creation
var bashScriptCommand = "" // during linux/unix/mac executable file creation
func main() {
// detect wheter this app is running for run time or build time
if isRuntime == "true" {
runExecutable()
} else {
buildExecutable()
}
}
// exec url/file
func runExecutable() {
if osName == "windows" {
execCommand("rundll32", "url.dll,FileProtocolHandler", url)
} else {
execCommand(bashScriptCommand)
}
}
// build dot exe file
func buildExecutable() {
if len(os.Args) < 2 {
forceExitIfError("first argument should be a path of the shortcut file")
}
shortcutFilePath = os.Args[1]
// construct the executable file namen
filename := filepath.Base(shortcutFilePath)
fileExt := strings.ToLower(filepath.Ext(filename))
shouldBecomeDotExe := true
if fileExt == ".lnk" || fileExt == ".url" || fileExt == ".cda" {
filename = filename[0:len(filename)-len(fileExt)] + ".exe"
} else if fileExt == ".desktop" {
filename = filename[0 : len(filename)-len(fileExt)]
shouldBecomeDotExe = false
} else {
forceExitIfError("unsupported file. file must be one of these: .lnk, .url, .cda, .desktop")
}
// get file metadata
loadShortcutFileMetadata()
// build the executable
// => .lnk, .url, .cda will be converted into .exe
// => .desktop will be converted into linux/unix/mac executable file
if shouldBecomeDotExe {
// fail if gopath is not set
if os.Getenv("GOPATH") == "" {
forceExitIfError("GOPATH env var is required to be set")
}
// fail if go binary is not callable
if _, err := exec.LookPath("go"); err != nil {
forceExitIfError("the go binary is required to be added into PATH env var")
}
// fail if git binary is not calleble, required by `go get` command
if _, err := exec.LookPath("git"); err != nil {
forceExitIfError("the git binary is required to be added into PATH env var")
}
// go get the rsrc lib
execCommand("go", "get", "github.com/akavel/rsrc")
// create the rsrc.syso to set the icon of upcoming executable
gopath := os.Getenv("GOPATH")
rsrcFilename := "rsrc.syso"
rsrcExecFile := filepath.Join(gopath, "bin", strings.Split(rsrcFilename, ".")[0])
execCommand(rsrcExecFile, "-ico", icon)
// remove existing file
os.Remove(filename)
// build the .exe file
execCommand(
"go",
"build",
"-ldflags", fmt.Sprintf(`-X "main.isRuntime=true" -X "main.osName=%s" -X "main.url=%s" -X "main.icon=%s"`, runtime.GOOS, url, icon),
"-o", filename,
)
// remove syso file
os.Remove(rsrcFilename)
} else {
// build the executable file
execCommand(
"go",
"build",
"-ldflags", fmt.Sprintf(`-X "main.isRuntime=true" -X "main.osName=%s" -X "main.bashScriptCommand=%s"`, runtime.GOOS, url, bashScriptCommand),
"-o", filename,
)
}
fmt.Printf(" => result: executable %s is successfully generated", filename)
}
// get metadata of shortcut file
func loadShortcutFileMetadata() {
buf, err := ioutil.ReadFile(shortcutFilePath)
forceExitIfError(err)
lines := make(map[string]string)
for _, o := range strings.Split(string(buf), "\n") {
parts := strings.Split(strings.TrimSpace(o), "=")
key := strings.ToLower(parts[0])
if key == "iconfile" {
key = "icon"
}
if len(parts) == 1 {
lines[key] = ""
} else {
lines[key] = parts[1]
}
}
url = lines["url"]
icon = lines["icon"]
bashScriptCommand = lines["exec"]
}
// utility func to force fail whenever there is error occuring
func forceExitIfError(message interface{}) {
if message == nil {
return
}
messageString := ""
switch message.(type) {
case string:
messageString = message.(string)
case error:
messageString = message.(error).Error()
}
fmt.Println(messageString)
os.Exit(0)
}
func execCommand(command ...string) {
if runtime.GOOS == "windows" {
command = append([]string{"cmd", "/C"}, command...)
} else {
command = append([]string{"/bin/sh", "-c"}, command...)
}
log.Println(strings.Join(command, " "))
cmd := exec.Command(command[0], command[1:]...)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
forceExitIfError(fmt.Sprintf(" => error: %s %s", err.Error(), stderr.String()))
}
}