-
Notifications
You must be signed in to change notification settings - Fork 12
/
action.go
123 lines (105 loc) · 2.98 KB
/
action.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
package sunbeam
import (
"encoding/json"
)
type Action struct {
Title string `json:"title,omitempty"`
Key string `json:"key,omitempty"`
Type ActionType `json:"type,omitempty"`
Open *OpenAction `json:"-"`
Copy *CopyAction `json:"-"`
Run *RunAction `json:"-"`
Exec *ExecAction `json:"-"`
Edit *EditAction `json:"-"`
Config *ConfigAction `json:"-"`
Reload *ReloadAction `json:"-"`
}
func (a *Action) UnmarshalJSON(bts []byte) error {
var action struct {
Title string `json:"title,omitempty"`
Key string `json:"key,omitempty"`
Type string `json:"type,omitempty"`
}
if err := json.Unmarshal(bts, &action); err != nil {
return err
}
a.Title = action.Title
a.Key = action.Key
a.Type = ActionType(action.Type)
switch a.Type {
case ActionTypeRun:
a.Run = &RunAction{}
return json.Unmarshal(bts, a.Run)
case ActionTypeOpen:
a.Open = &OpenAction{}
return json.Unmarshal(bts, a.Open)
case ActionTypeCopy:
a.Copy = &CopyAction{}
return json.Unmarshal(bts, a.Copy)
case ActionTypeEdit:
a.Edit = &EditAction{}
return json.Unmarshal(bts, a.Edit)
case ActionTypeExec:
a.Exec = &ExecAction{}
return json.Unmarshal(bts, a.Exec)
case ActionTypeExit:
return nil
case ActionTypeReload:
a.Reload = &ReloadAction{}
return json.Unmarshal(bts, a.Reload)
case ActionTypeConfig:
a.Config = &ConfigAction{}
return json.Unmarshal(bts, a.Config)
}
return nil
}
type ConfigAction struct {
Extension string `json:"extension,omitempty"`
}
type EditAction struct {
Path string `json:"path,omitempty"`
Exit bool `json:"exit,omitempty"`
Reload bool `json:"reload,omitempty"`
}
type ReloadAction struct {
Params map[string]any `json:"params,omitempty"`
}
type RunAction struct {
Extension string `json:"extension,omitempty"`
Command string `json:"command,omitempty"`
Params map[string]any `json:"params,omitempty"`
Reload bool `json:"reload,omitempty"`
Exit bool `json:"exit,omitempty"`
}
type CopyAction struct {
Text string `json:"text,omitempty"`
Exit bool `json:"exit,omitempty"`
}
type ExecAction struct {
Interactive bool `json:"interactive,omitempty"`
Command string `json:"command,omitempty"`
Dir string `json:"dir,omitempty"`
Exit bool `json:"exit,omitempty"`
}
type OpenAction struct {
Url string `json:"url,omitempty"`
Path string `json:"path,omitempty"`
}
type ActionType string
const (
ActionTypeRun ActionType = "run"
ActionTypeOpen ActionType = "open"
ActionTypeCopy ActionType = "copy"
ActionTypeEdit ActionType = "edit"
ActionTypeExec ActionType = "exec"
ActionTypeExit ActionType = "exit"
ActionTypeReload ActionType = "reload"
ActionTypeConfig ActionType = "config"
)
type Payload struct {
Command string `json:"command"`
Preferences map[string]any `json:"preferences"`
Params map[string]any `json:"params"`
Cwd string `json:"cwd"`
Query string `json:"query,omitempty"`
}