-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
268 lines (255 loc) · 6.71 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
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
package main
import (
"embed"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/urfave/cli/v2"
"github.com/wailsapp/wails/v2"
"github.com/wailsapp/wails/v2/pkg/logger"
"github.com/wailsapp/wails/v2/pkg/options"
"github.com/wailsapp/wails/v2/pkg/options/linux"
"github.com/wailsapp/wails/v2/pkg/options/mac"
"github.com/wailsapp/wails/v2/pkg/options/windows"
)
var wailsapp = NewApp()
//go:embed frontend/dist
var assets embed.FS
//go:embed build/appicon.png
var icon []byte
var cliemail HttpEmailMsg
// Function: getRequest
//
// Description: This method will issue a get request with the data sent
//
// as json in the body.
//
// Inputs:
//
// url The url to send the request
// data An io.Reader pointing to a json string
func getRequest(url string, data io.Reader) string {
client := &http.Client{}
req, err := http.NewRequest(http.MethodGet, url, data)
if err != nil {
// handle error
log.Fatal(err)
}
// set the request header Content-Type for json
req.Header.Set("Content-Type", "application/json; charset=utf-8")
resp, err2 := client.Do(req)
if err2 != nil {
//
// handle error. Most likely not running.
//
log.Fatal(err2)
}
body, err3 := ioutil.ReadAll(resp.Body)
if err3 != nil {
log.Fatal(err3)
}
resp.Body.Close()
return string(body)
}
func main() {
//
// Process the Command Line.
//
cliemail.Account = "Default" // Set the default value for account.
app := &cli.App{
Name: "EmailIt",
Usage: "A program for sending emails, working with text, and scripts.",
Version: "v2.1.0",
Compiled: time.Now(),
Authors: []*cli.Author{
{
Name: "Richard Guay",
Email: "[email protected]",
},
},
Copyright: "(c) 2022 Richard Guay",
HelpName: "EmailIt",
UsageText: "",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "a",
Value: "",
Usage: "Address to send an email",
Destination: &cliemail.To,
},
&cli.StringFlag{
Name: "t",
Value: "",
Usage: "Attachment to the email",
Destination: &cliemail.Attachment,
},
&cli.StringFlag{
Name: "s",
Value: "",
Usage: "Subject for the email",
Destination: &cliemail.Subject,
},
&cli.StringFlag{
Name: "b",
Value: "",
Usage: "Body of the email",
Destination: &cliemail.Body,
},
},
Action: func(cCtx *cli.Context) error {
if cCtx.Args().Len() == 0 {
//
// Only run the main if there isn't any arguments. Miss typing an argument will run here.
//
runFrontEnd()
} else {
//
// We have a mailto protocal to process.
//
mailto := cCtx.Args().First()
mailto = mailto[7:] // remove the 'mailto:'
mailto = strings.ReplaceAll(mailto, "?", "&") // Change al ? To & - There should only be one.
urimsg := fmt.Sprintf("http://localhost:9978/api/emailit/mailto?to=%s", mailto)
result := getRequest(urimsg, strings.NewReader(""))
fmt.Printf("\nThe server returned: %s\n", result[1:len(result)-1])
}
return nil
},
Commands: []*cli.Command{
{
Name: "mkemail",
Aliases: []string{"me"},
Usage: "Create an email using a TUI",
Action: func(cCtx *cli.Context) error {
BuildEmail()
return (nil)
},
},
{
Name: "notes",
Aliases: []string{"n"},
Usage: "Open the notes.",
Action: func(cCtx *cli.Context) error {
result := getRequest("http://localhost:9978/api/notes/open", strings.NewReader(""))
fmt.Printf("\nThe server returned: %s\n", result[1:len(result)-1])
return (nil)
},
},
{
Name: "emailit",
Aliases: []string{"e"},
Usage: "Open the EmailIt email sending application.",
Action: func(cCtx *cli.Context) error {
result := getRequest("http://localhost:9978/api/emailit/open", strings.NewReader(""))
fmt.Printf("\nThe server returned: %s\n", result[1:len(result)-1])
return (nil)
},
},
{
Name: "scriptline",
Aliases: []string{"sl"},
Usage: "Open the ScriptLine application.",
Action: func(cCtx *cli.Context) error {
result := getRequest("http://localhost:9978/api/scriptline/open", strings.NewReader(""))
fmt.Printf("\nThe server returned: %s\n", result[1:len(result)-1])
return (nil)
},
},
{
Name: "sendemail",
Aliases: []string{"se"},
Usage: "Send the email directly. No GUI or TUI.",
Action: func(cCtx *cli.Context) error {
//
// Create and send the email. Then quit.
//
bodyJson := HttpEmailMsg{
Account: cliemail.Account,
From: "Default",
To: cliemail.To,
Subject: cliemail.Subject,
Body: cliemail.Body,
Attachment: cliemail.Attachment,
}
body, err := json.Marshal(bodyJson)
bodyStr := string(body[:])
if err == nil {
//
// Send the email then!
//
SendHTTPQuery("PUT", "http://localhost:9978/api/emailit/send", bodyStr)
}
return (nil)
},
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func runFrontEnd() {
//
// Run the different server locations.
//
go dirserver(wailsapp, "Archive", "/Users/raguay/Dropbox/Richard/Notes/TheArchive")
// Create application with options
err := wails.Run(&options.App{
Title: "EmailIt",
Width: 1022,
Height: 608,
MinWidth: 1022,
MinHeight: 608,
MaxWidth: 1022,
MaxHeight: 608,
DisableResize: true,
Fullscreen: false,
Frameless: true,
StartHidden: false,
HideWindowOnClose: false,
BackgroundColour: &options.RGBA{R: 0, G: 0, B: 0, A: 0},
Assets: assets,
LogLevel: logger.DEBUG,
OnStartup: wailsapp.startup,
OnDomReady: wailsapp.domReady,
OnShutdown: wailsapp.shutdown,
CSSDragProperty: "--wails-draggable",
CSSDragValue: "drag",
Bind: []interface{}{
wailsapp,
},
// Windows platform specific options
Windows: &windows.Options{
WebviewIsTransparent: true,
WindowIsTranslucent: false,
DisableWindowIcon: false,
},
// macOS platform specific items.
Mac: &mac.Options{
TitleBar: mac.TitleBarHiddenInset(),
Appearance: mac.NSAppearanceNameDarkAqua,
WebviewIsTransparent: true,
WindowIsTranslucent: false,
About: &mac.AboutInfo{
Title: "EmailIt",
Message: "Version 2.0.0 © 2022 Richard Guay <[email protected]>",
Icon: icon,
},
},
// Linux platform specific items.
Linux: &linux.Options{
Icon: icon,
WindowIsTranslucent: true,
WebviewGpuPolicy: linux.WebviewGpuPolicyAlways,
},
})
if err != nil {
log.Fatal(err)
}
}