/
app.go
207 lines (172 loc) Β· 4.71 KB
/
app.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package flowser
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path"
"runtime"
"strings"
"github.com/google/go-github/github"
)
const (
darwin = "darwin"
windows = "windows"
)
// New create new Flowser application.
func New() *app {
return &app{}
}
type app struct {
installDir string
}
var errorPlatformNotSupported = errors.New("OS not supported, only supporting Windows and Mac OS")
// Run starts the Flowser application with provided path to the flow project.
//
// Project path if exists should be set to the folder containing flow.json, if no such path exists pass empty value.
func (a *app) Run(installDir string, projectPath string) error {
exe, err := a.executable(installDir)
if err != nil {
return err
}
if projectPath != "" {
projectPath = fmt.Sprintf("--project-path=%s", projectPath)
}
return exec.
Command(exe, projectPath).
Run()
}
// Install Flowser application in the provided install directory.
//
// Install directory is optional, if you want to default to your system location you can provide empty value.
func (a *app) Install(installDir string) error {
downloadDir, err := downloadLatestReleaseAsset()
if err != nil {
return err
}
defer os.Remove(downloadDir)
return a.unzip(downloadDir, installDir)
}
// Installed checks whether the flowser application is already installed on the system.
func (a *app) Installed(installDir string) bool {
executable, err := a.executable(installDir)
if err != nil {
return false
}
_, err = os.Stat(executable)
return err == nil
}
// Remove Flowser application from provide directory.
//
// Install directory is optional, if you don't provide a value default will be used.
func (a *app) Remove(installDir string) error {
a.installDir = installDir
dir, err := a.appDir(installDir)
if err != nil {
return err
}
return os.RemoveAll(dir)
}
// unzip content from source compressed file to a target directory.
func (a *app) unzip(source string, target string) error {
var cmd *exec.Cmd
switch runtime.GOOS {
case darwin:
// Use native unzip tool as it handles the creation of required symbolic links
cmd = exec.Command("unzip", source, "-d", target)
case windows:
appDir, err := a.appDir(target)
if err != nil {
return err
}
if err := os.MkdirAll(appDir, os.ModePerm); err != nil {
return err
}
// tar utility is available from Windows build 17063 consider using other command or a custom implementation
// https://learn.microsoft.com/en-us/virtualization/community/team-blog/2017/20171219-tar-and-curl-come-to-windows
cmd = exec.Command("tar", "-xf", source, "-C", appDir)
default:
return errorPlatformNotSupported
}
return cmd.Run()
}
// appDir returns the location of the executable application inside the installation dir.
func (a *app) appDir(installDir string) (string, error) {
return path.Join(installDir, "Flowser.app"), nil
}
// executable returns the location of application executable.
func (a *app) executable(installDir string) (string, error) {
files := map[string]string{
darwin: "Contents/MacOS/Flowser",
windows: "Flowser.exe",
}
file, ok := files[runtime.GOOS]
if !ok {
return "", errorPlatformNotSupported
}
appDir, err := a.appDir(installDir)
if err != nil {
return "", err
}
return path.Join(appDir, file), nil
}
func downloadLatestReleaseAsset() (string, error) {
release, err := getLatestRelease()
if err != nil {
return "", err
}
resp, err := http.Get(*release.asset.BrowserDownloadURL)
if err != nil {
return "", err
}
defer resp.Body.Close()
out, _ := os.Create(*release.asset.Name)
defer out.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return "", err
}
return out.Name(), nil
}
type flowserRelease struct {
version string
asset github.ReleaseAsset
}
func getLatestRelease() (*flowserRelease, error) {
client := github.NewClient(nil)
release, _, err := client.Repositories.GetLatestRelease(context.Background(), "onflowser", "flowser")
if err != nil {
return nil, err
}
latestVersion := strings.Replace(*release.TagName, "v", "", 1)
targetAssetName, err := getAssetName(latestVersion)
if err != nil {
return nil, err
}
for _, asset := range release.Assets {
if *asset.Name == targetAssetName {
return &flowserRelease{
version: latestVersion,
asset: asset,
}, nil
}
}
return nil, errors.New("no asset found")
}
func getAssetName(version string) (string, error) {
isArm := strings.HasPrefix(runtime.GOARCH, "arm")
switch runtime.GOOS {
case darwin:
if isArm {
return fmt.Sprintf("Flowser-%s-arm64-mac.zip", version), nil
}
return fmt.Sprintf("Flowser-%s-mac.zip", version), nil
case windows:
return fmt.Sprintf("Flowser-%s-win.zip", version), nil
default:
return "", errorPlatformNotSupported
}
}