-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncthing-map.go
141 lines (127 loc) · 3.37 KB
/
syncthing-map.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
package main
import (
"fmt"
"io"
"os"
"runtime"
"strings"
"time"
"github.com/rs/zerolog"
"github.com/urfave/cli/v2"
)
// version will be added from tag during compilation
var compiledVersion string
type Device struct {
ID string `xml:"id,attr" json:"id"`
Name string `xml:"name,attr" json:"name"`
}
type Folder struct {
ID string `xml:"id,attr" json:"id"`
Label string `xml:"label,attr" json:"label"`
Type string `xml:"type,attr" json:"type"`
Device []Device `xml:"device" json:"device"`
}
type Configuration struct {
Folder []Folder `xml:"folder"`
Device []Device `xml:"device"`
}
type dataJsonT map[string][]Folder
func readFile(filename string) (content []byte, err error) {
handler, err := os.Open(filename)
if err != nil {
return nil, err
}
content, err = io.ReadAll(handler)
if err != nil {
return nil, err
}
handler.Close()
return content, nil
}
var log zerolog.Logger
func init() {
output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}
output.FormatLevel = func(i interface{}) string {
return strings.ToUpper(fmt.Sprintf("%s", i))
}
output.FormatMessage = func(i interface{}) string {
return fmt.Sprintf("%s", i)
}
log = zerolog.New(output).With().Timestamp().Logger()
}
func main() {
if compiledVersion == "" {
log.Error().Msgf("compiledVersion not set at compile time")
compiledVersion = "(missing from compilation)"
}
app := &cli.App{
Name: "syncthing-map",
Usage: "Syncthing devices and shared folders mapped in your browser",
Version: fmt.Sprintf("%s %s/%s", compiledVersion, runtime.GOOS, runtime.GOARCH),
Authors: []*cli.Author{
{
Name: "wsw70",
Email: "[email protected]",
},
},
Copyright: "WTFPL (http://www.wtfpl.net)",
HideHelp: true,
UsageText: "syncthing-map clean\nsyncthing-map add --device <device name> --file <configuration file> | graph\nsyncthing-map server",
Commands: []*cli.Command{
{
Name: "add",
Aliases: []string{"a"},
Usage: "add a new config file",
Action: func(cCtx *cli.Context) error {
if cCtx.String("device") == "" || cCtx.String("file") == "" {
cli.ShowAppHelpAndExit(cCtx, 1)
}
readConfigXml(cCtx.String("device"), cCtx.String("file"), "data-cli.json")
return nil
},
Flags: []cli.Flag{
&cli.StringFlag{Name: "device", Aliases: []string{"d"}},
&cli.StringFlag{Name: "file", Aliases: []string{"f"}},
},
},
{
Name: "graph",
Aliases: []string{"g"},
Usage: "create the graph in syncthing-map.html",
Action: func(cCtx *cli.Context) error {
writeGraph("data-cli.json", "syncthing-map-cli.html")
return nil
},
},
{
Name: "clean",
Aliases: []string{"c"},
Usage: "remove working files (*.json, *.html)",
Action: func(cCtx *cli.Context) error {
filesToRemove := []string{
"syncthing-map-cli.html",
"syncthing-map-server.html",
"data-cli.json",
"data-server.json",
}
for _, file := range filesToRemove {
os.Remove(file)
}
return nil
},
},
{
Name: "server",
Aliases: []string{"s"},
Usage: "HTTP server to automatically generate a map",
Action: func(cCtx *cli.Context) error {
httpServer()
return nil
},
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal().Msgf("error running the application: %v", err)
}
}