This repository was archived by the owner on Jul 14, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
301 lines (252 loc) · 7.32 KB
/
Copy pathapp.go
File metadata and controls
301 lines (252 loc) · 7.32 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"os/user"
"path/filepath"
"runtime/debug"
"sync"
"time"
"github.com/go-logr/logr"
api "github.com/tiny-systems/platform-api"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
var (
BuildTime = "dev"
Version = "unknown"
)
const sdkModulePath = "github.com/tiny-systems/module"
type BuildInfo struct {
BuildTime string `json:"buildTime"`
Version string `json:"version"`
SdkVersion string `json:"sdkVersion"`
}
func (a *App) GetBuildInfo() BuildInfo {
sdkVersion := "unknown"
if info, ok := debug.ReadBuildInfo(); ok {
for _, dep := range info.Deps {
if dep.Path == sdkModulePath {
sdkVersion = dep.Version
break
}
}
}
return BuildInfo{
BuildTime: BuildTime,
Version: Version,
SdkVersion: sdkVersion,
}
}
// App struct
type App struct {
ctx context.Context
logger logr.Logger
// watchMu protects watchCancel
watchMu sync.Mutex
watchCancel context.CancelFunc
}
// Preferences stores user preferences
type Preferences struct {
LastContext string `json:"lastContext"`
LastNamespace string `json:"lastNamespace"`
}
// getPreferencesPath returns the path to the preferences file
func getPreferencesPath() (string, error) {
usr, err := user.Current()
if err != nil {
return "", err
}
configDir := filepath.Join(usr.HomeDir, ".config", "tinysystems")
if err := os.MkdirAll(configDir, 0755); err != nil {
return "", err
}
return filepath.Join(configDir, "preferences.json"), nil
}
// NewApp creates a new App application struct
func NewApp(l logr.Logger) *App {
return &App{
logger: l,
}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
// Set PATH to include common locations for gcloud and other CLI tools
// This is necessary because GUI apps on macOS don't inherit shell PATH
if err := setupPATH(); err != nil {
a.logger.Error(err, "Failed to setup PATH")
}
// Enable direct deep link event emission for URLs arriving while app is running
deepLinkStartup(a.ctx)
}
// setupPATH adds common CLI tool locations to PATH environment variable
func setupPATH() error {
currentPath := os.Getenv("PATH")
// Get user home directory
usr, err := user.Current()
if err != nil {
return err
}
// Common paths where gke-gcloud-auth-plugin and other tools might be located
additionalPaths := []string{
filepath.Join(usr.HomeDir, "google-cloud-sdk", "bin"),
filepath.Join(usr.HomeDir, ".local", "bin"),
filepath.Join(usr.HomeDir, "go", "bin"),
filepath.Join(usr.HomeDir, ".krew", "bin"),
"/usr/local/bin",
"/opt/homebrew/bin",
"/usr/local/go/bin",
}
// Build new PATH with additional paths prepended
newPath := currentPath
for _, p := range additionalPaths {
// Only add if directory exists
if _, err := os.Stat(p); err == nil {
newPath = p + ":" + newPath
}
}
// Set the updated PATH
return os.Setenv("PATH", newPath)
}
func (a *App) shutdown(ctx context.Context) {
}
// ShowAbout displays the About dialog with company and version info.
func (a *App) ShowAbout() {
bi := a.GetBuildInfo()
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.InfoDialog,
Title: "About Tiny Systems",
Message: fmt.Sprintf("Tiny Systems Desktop Client\nVersion: %s\nSDK: %s\n\n© 2026 Tiny Systems Limited\nCompany No. 14302894\n71-75 Shelton Street, Covent Garden\nLondon, WC2H 9JQ\n\nhello@tinysystems.io\nhttps://tinysystems.io", bi.Version, bi.SdkVersion),
})
}
// GetPreferences returns saved user preferences
func (a *App) GetPreferences() (*Preferences, error) {
path, err := getPreferencesPath()
if err != nil {
return &Preferences{}, nil
}
data, err := os.ReadFile(path)
if err != nil {
// File doesn't exist yet - return empty preferences
return &Preferences{}, nil
}
var prefs Preferences
if err := json.Unmarshal(data, &prefs); err != nil {
return &Preferences{}, nil
}
return &prefs, nil
}
// SavePreferences saves user preferences
func (a *App) SavePreferences(contextName, namespace string) error {
path, err := getPreferencesPath()
if err != nil {
return err
}
prefs := Preferences{
LastContext: contextName,
LastNamespace: namespace,
}
data, err := json.MarshalIndent(prefs, "", " ")
if err != nil {
return err
}
return os.WriteFile(path, data, 0644)
}
// SaveFile opens a save dialog and writes content to the selected file
func (a *App) SaveFile(defaultFilename, content string) (string, error) {
filepath, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{
DefaultFilename: defaultFilename,
Filters: []runtime.FileFilter{
{DisplayName: "JSON Files", Pattern: "*.json"},
{DisplayName: "All Files", Pattern: "*"},
},
})
if err != nil {
return "", err
}
if filepath == "" {
// User cancelled
return "", nil
}
if err := os.WriteFile(filepath, []byte(content), 0644); err != nil {
return "", err
}
return filepath, nil
}
// OpenFile opens a file dialog and returns the file content
func (a *App) OpenFile() (string, error) {
filepath, err := runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{
Filters: []runtime.FileFilter{
{DisplayName: "JSON Files", Pattern: "*.json"},
{DisplayName: "All Files", Pattern: "*"},
},
})
if err != nil {
return "", err
}
if filepath == "" {
// User cancelled
return "", nil
}
data, err := os.ReadFile(filepath)
if err != nil {
return "", err
}
return string(data), nil
}
// GetPendingDeepLink returns a deep link URL that arrived before the frontend was ready.
// Called by the frontend on mount to catch URLs from cold-start launches.
func (a *App) GetPendingDeepLink() string {
deepLinkState.mu.Lock()
defer deepLinkState.mu.Unlock()
url := deepLinkState.pendingURL
deepLinkState.pendingURL = "" // consume it
fmt.Println("[DEEPLINK] GetPendingDeepLink called, returning:", url)
return url
}
// FetchSolutionJSON downloads solution JSON from the given URL (legacy deep links).
func (a *App) FetchSolutionJSON(url string) (string, error) {
client := &http.Client{Timeout: 30 * time.Second}
resp, err := client.Get(url)
if err != nil {
return "", fmt.Errorf("failed to fetch solution: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("server returned %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read response: %w", err)
}
if !json.Valid(body) {
return "", fmt.Errorf("response is not valid JSON")
}
return string(body), nil
}
// FetchSolutionExport downloads solution export JSON using a one-time token via the platform-api client.
func (a *App) FetchSolutionExport(token, apiBase string) (string, error) {
client, err := api.NewClientWithResponses(apiBase)
if err != nil {
return "", fmt.Errorf("failed to create API client: %w", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
resp, err := client.ExportSolutionWithResponse(ctx, &api.ExportSolutionParams{Token: token})
if err != nil {
return "", fmt.Errorf("failed to fetch solution: %w", err)
}
if resp.StatusCode() != http.StatusOK {
return "", fmt.Errorf("server returned %d", resp.StatusCode())
}
body := resp.Body
if !json.Valid(body) {
return "", fmt.Errorf("response is not valid JSON")
}
return string(body), nil
}