-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
112 lines (98 loc) · 1.87 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
package main
import (
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/urfave/cli/v2"
)
const (
defaultAddr = "127.0.0.1:1234"
)
var HistoryFile string
func main() {
app := cli.NewApp()
app.Usage = "The high-performance file transfer for Golang"
app.Version = Version
app.EnableBashCompletion = true
app.CustomAppHelpTemplate = Summary()
app.Action = baseCmd
app.Flags = []cli.Flag{}
app.Commands = []*cli.Command{
{
Name: "send",
Usage: "send network",
Action: sender,
Flags: []cli.Flag{
&cli.StringFlag{Name: "ip", Usage: "Enter target address", Value: defaultAddr},
},
},
{
Name: "receive",
Usage: "receive network",
Action: receiver,
},
}
app.Before = func(context *cli.Context) (err error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return
}
HistoryFile = filepath.Join(homeDir, ".ftf")
if err = os.MkdirAll(HistoryFile, 0755); err != nil {
return err
}
return
}
err := app.Run(os.Args)
if err != nil {
log.Panic(err)
}
}
func baseCmd(c *cli.Context) (err error) {
//if c.NArg() == 0 {
// print(Summary())
// return
//}
if err := runApp(c); err != nil {
return cli.Exit(err, 1)
}
return
}
func runApp(c *cli.Context) (err error) {
opts := &startOpts{}
opts.addr = c.String("addr")
opts.path = c.String("path")
a := newApp(opts)
service := c.Command.Name
if len(service) != 0 {
switch service {
case Sender:
a.isSender = true
a.hasService = true
case Receiver:
a.isReceiver = true
a.hasService = true
}
}
err = a.Start()
if err != nil {
return err
}
return err
}
func readMessageFromStdin() ([]byte, error) {
var message []byte
s, err := os.Stdin.Stat()
if err != nil {
return nil, err
}
if s.Mode()&os.ModeNamedPipe != 0 {
bytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return nil, err
}
message = bytes
}
return message, nil
}