-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcli.go
More file actions
197 lines (184 loc) · 5.32 KB
/
cli.go
File metadata and controls
197 lines (184 loc) · 5.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
package main
/*
#cgo pkg-config: gtk+-3.0 webkit2gtk-4.1
#include <gtk/gtk.h>
#include <webkit2/webkit2.h>
*/
import "C"
import (
"errors"
"fmt"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
"unsafe"
"github.com/mholt/archiver/v3"
"github.com/urfave/cli/v2"
"github.com/wailsapp/wails/v3/pkg/application"
"github.com/wailsapp/wails/v3/pkg/events"
)
const DEFAULT_ASSETS_DIR = "/usr/share/aikadm"
func NewCli() *cli.App {
return &cli.App{
Name: "aikadm",
Action: CmdMain,
Flags: []cli.Flag{
&cli.StringSliceFlag{
Name: "session-dir",
Aliases: []string{"d"},
Value: cli.NewStringSlice("/usr/share/xsessions", "/usr/share/wayland-sessions/"),
Usage: "Session directories to search for",
},
&cli.StringSliceFlag{
Name: "env",
Aliases: []string{"e"},
Usage: "Environment to run in, e.g. -e KEY1=VALUE1 -e KEY2=VALUE2",
},
&cli.StringFlag{
Name: "assets",
Aliases: []string{"a"},
Value: DEFAULT_ASSETS_DIR,
Usage: "Set of assets to serve",
},
},
Commands: []*cli.Command{
{
Name: "install",
Usage: "Install frontend assets to the specified directory, which can be a compressed file, directory or a link to a compressed file. If directory is not exists, it will be created.",
Action: CmdInstall,
},
},
}
}
func CmdMain(ctx *cli.Context) error {
if os.Getenv("GREETD_SOCK") != "" && os.Getenv("HTML_GREET_USE_CAGE") == "" {
os.Setenv("HTML_GREET_USE_CAGE", "1")
exe, _ := os.Executable()
args := append([]string{"-s", "--", exe}, os.Args[1:]...)
cmd := exec.Command("cage", args...)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to run [%s]: %s", strings.Join(cmd.Args, " "), string(out))
}
return nil
}
if os.Getenv("XDG_SESSION_TYPE") == "wayland" {
os.Setenv("GDK_BACKEND", "wayland")
}
var sessionDir []string
var env []string
for _, dir := range ctx.StringSlice("session-dir") {
triped := strings.TrimSpace(dir)
if triped != "" {
sessionDir = append(sessionDir, triped)
}
}
for _, envVar := range ctx.StringSlice("env") {
triped := strings.TrimSpace(envVar)
if triped != "" {
env = append(env, triped)
}
}
aikadm := NewAikadm(sessionDir, env)
app := application.New(application.Options{
Name: "aikadm",
Assets: application.AssetOptions{
Handler: NewAssetServer(ctx.String("assets")),
},
Services: []application.Service{
application.NewService(aikadm),
},
OnShutdown: aikadm.stop,
})
app.OnApplicationEvent(events.Common.ApplicationStarted, func(event *application.ApplicationEvent) {
window := app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{
Frameless: true,
Title: "aikadm",
MaxWidth: 0,
MaxHeight: 0,
Hidden: true,
})
window.OnWindowEvent(events.Common.WindowRuntimeReady, func(event *application.WindowEvent) {
window.Show()
})
impl := reflect.ValueOf(window).Elem().FieldByName("impl").Elem().Elem()
windowPtr := (*C.GtkWindow)(unsafe.Pointer(impl.FieldByName("window").Pointer()))
webviewPtr := (*C.WebKitWebView)(unsafe.Pointer(impl.FieldByName("webview").Pointer()))
C.gtk_window_set_geometry_hints(windowPtr, nil, nil, 0)
// 设置背景透明
rgba := C.GdkRGBA{C.double(0), C.double(0), C.double(0), C.double(0)}
C.webkit_web_view_set_background_color(webviewPtr, &rgba)
})
return app.Run()
}
func CmdInstall(ctx *cli.Context) error {
if ctx.Args().Len() != 1 {
return fmt.Errorf("invalid arguments")
}
src := ctx.Args().First()
if url, err := url.Parse(src); err == nil && url.Scheme != "" {
getFilenameFromHeader := func(resp *http.Response) string {
contentDisposition := resp.Header.Get("Content-Disposition")
if contentDisposition == "" {
return ""
}
parts := strings.Split(contentDisposition, ";")
for _, part := range parts {
part = strings.TrimSpace(part)
if strings.HasPrefix(part, "filename=") {
filename := strings.TrimPrefix(part, "filename=")
filename = strings.Trim(filename, `"`)
return filename
}
}
return ""
}
resp, err := http.DefaultClient.Get(src)
if err != nil {
return fmt.Errorf("failed to download: %s", err)
}
filename := getFilenameFromHeader(resp)
if filename == "" {
filename = filepath.Base(url.Path)
}
f, err := os.CreateTemp("", "aikadm-*"+filename)
if err != nil {
return fmt.Errorf("failed to create temporary file: %s", err)
}
defer os.Remove(f.Name())
_, err = f.ReadFrom(resp.Body)
f.Close()
if err != nil {
return fmt.Errorf("failed to write to temporary file: %s", err)
}
src = f.Name()
}
assets := ctx.String("assets")
if _, err := os.Stat(src); errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("source not found: %s", src)
}
if stat, err := os.Stat(assets); errors.Is(err, os.ErrNotExist) {
if err := os.MkdirAll(assets, 0755); err != nil {
return fmt.Errorf("failed to create assets directory: %s", err)
}
} else if !stat.IsDir() {
return fmt.Errorf("assets path is not a directory: %s", assets)
}
if stat, err := os.Stat(src); err == nil && stat.IsDir() {
srcFs := os.DirFS(src)
err = os.CopyFS(assets, srcFs)
if err != nil {
return fmt.Errorf("failed to copy directory: %s", err)
}
return nil
}
err := archiver.Unarchive(src, assets)
if err != nil {
return fmt.Errorf("failed to unarchive: %s", err)
}
return nil
}