-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmain.go
99 lines (90 loc) · 2.25 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
package main
import (
"bufio"
"flag"
"github.com/nikhil1raghav/kindle-send/config"
"github.com/nikhil1raghav/kindle-send/epubgen"
"github.com/nikhil1raghav/kindle-send/mail"
"github.com/nikhil1raghav/kindle-send/util"
"os"
)
func extractLinks(filename string) (links []string) {
links = make([]string, 0)
file, err := os.Open(filename)
if err != nil {
util.Red.Println("Error opening link file", err)
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
links = append(links, scanner.Text())
}
return
}
func flagPassed(name string) bool{
visited:=false
flag.Visit(func(f *flag.Flag) {
if f.Name==name{
visited=true
}
})
return visited
}
func main() {
DefaultConfig, err := config.DefaultConfigPath()
if err != nil {
util.Red.Println(err)
return
}
configPath := flag.String("config", DefaultConfig, "Path to the configuration file (optional)")
pageUrl := flag.String("url", "", "URL of webpage to send")
title := flag.String("title", "", "Title of the epub (optional)")
linkfile := flag.String("linkfile", "", "Path to a text file containing multiple links separated by newline")
filePath := flag.String("file", "", "Mail a file to kindle, use kindle-send as a simple mailer")
mailTimeout :=flag.Int("mail-timeout",120, "Timeout for sending mail in Seconds" )
_ =flag.Bool("version", false, "Print version information")
flag.Parse()
passed := 0
flag.Visit(func(f *flag.Flag) {
passed++
})
if passed == 0 {
flag.PrintDefaults()
}
if flagPassed("version"){
util.PrintVersion()
}
urls := make([]string, 0)
if len(*pageUrl) != 0 {
urls = append(urls, *pageUrl)
}
if len(*linkfile) != 0 {
urls = append(urls, extractLinks(*linkfile)...)
}
_, err = config.Load(*configPath)
if err != nil {
util.Red.Println(err)
return
}
filesToSend := make([]string, 0)
if len(*filePath) != 0 {
filesToSend = append(filesToSend, *filePath)
}
if len(urls) != 0 {
book, err := epubgen.Make(urls, *title)
if err != nil {
util.Red.Println(err)
} else {
filesToSend = append(filesToSend, book)
}
}
if len(filesToSend) != 0 {
timeout:=config.DefaultTimeout
if flagPassed("mail-timeout"){
timeout=*mailTimeout
}
mail.Send(filesToSend, timeout)
}
}