CLI client for Syncthing in Go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config.go 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. package api
  2. import (
  3. "fmt"
  4. "strings"
  5. "git.dtluna.net/dtluna/syncthing-cli/config"
  6. )
  7. const ConfigPath = "/rest/system/config"
  8. func indent(s string, depth int) string {
  9. sep := strings.Repeat(" ", depth)
  10. return "\n" + sep + strings.Join(
  11. strings.Split(s, "\n"),
  12. "\n"+sep,
  13. )
  14. }
  15. func indentStringSlice(slice []string, depth int) string {
  16. if len(slice) == 0 {
  17. return "None"
  18. }
  19. sep := strings.Repeat(" ", depth)
  20. lines := []string{}
  21. for i, el := range slice {
  22. lines = append(lines, sep+fmt.Sprintf("%v. %s", i+1, el))
  23. }
  24. return "\n" + strings.Join(lines, "\n")
  25. }
  26. type FolderDevice struct {
  27. DeviceID string
  28. }
  29. func (fd FolderDevice) String() string {
  30. return fmt.Sprint("ID: ", fd.DeviceID)
  31. }
  32. func indentFolderDevices(fds []FolderDevice, depth int) string {
  33. if len(fds) == 0 {
  34. return "None"
  35. }
  36. sep := strings.Repeat(" ", depth)
  37. lines := []string{}
  38. for i, el := range fds {
  39. line := strings.ReplaceAll(fmt.Sprintf("%v. %s", i+1, el), "\n", "\n"+sep)
  40. lines = append(lines, line)
  41. }
  42. return "\n" + strings.Join(lines, "\n")
  43. }
  44. type VersioningInfoParams map[string]interface{}
  45. func (vip VersioningInfoParams) String() string {
  46. if len(vip) == 0 {
  47. return "None"
  48. }
  49. slice := []string{}
  50. for key, value := range vip {
  51. slice = append(slice, fmt.Sprintf("%v: %v", key, value))
  52. }
  53. return strings.Join(slice, "\n")
  54. }
  55. type VersioningInfo struct {
  56. Type string
  57. Params VersioningInfoParams
  58. }
  59. func (vi VersioningInfo) String() string {
  60. return fmt.Sprintf(
  61. `Type: %v
  62. Params: %v`,
  63. vi.Type,
  64. indent(vi.Params.String(), 2),
  65. )
  66. }
  67. type Folder struct {
  68. ID string
  69. Label string
  70. Path string
  71. Type string
  72. Devices []FolderDevice
  73. RescanIntervalS int
  74. IgnorePerms bool
  75. AutoNormalize bool
  76. MinDiskFreePct int
  77. Versioning VersioningInfo
  78. Copiers int
  79. Pullers int
  80. Hashers int
  81. Order string
  82. IgnoreDelete bool
  83. ScanProgressIntervalS int
  84. PullerSleepS int
  85. PullerPauseS int
  86. MaxConflicts int
  87. DisableSparseFiles bool
  88. DisableTempIndexes bool
  89. Fsync bool
  90. Invalid string
  91. }
  92. func (f Folder) String() string {
  93. var viString string
  94. if f.Versioning.Type == "" {
  95. viString = "None"
  96. } else {
  97. viString = indent(f.Versioning.String(), 2)
  98. }
  99. return fmt.Sprintf(
  100. `ID: %v
  101. Label: %v
  102. Path: %v
  103. Type: %v
  104. Devices: %v
  105. Versioning: %v`,
  106. f.ID,
  107. f.Label,
  108. f.Path,
  109. f.Type,
  110. indentFolderDevices(f.Devices, 2),
  111. viString,
  112. )
  113. }
  114. type Device struct {
  115. DeviceID string
  116. Name string
  117. Addresses []string
  118. Compression string
  119. CertName string
  120. Introducer bool
  121. }
  122. func (d Device) String() string {
  123. return fmt.Sprintf(
  124. `ID: %v
  125. Name: %v
  126. Addresses: %v
  127. Compression: %v
  128. Certificate name: %v
  129. Introducer: %v`,
  130. d.DeviceID,
  131. d.Name,
  132. indentStringSlice(d.Addresses, 2),
  133. d.Compression,
  134. d.CertName,
  135. d.Introducer,
  136. )
  137. }
  138. type GUIConfig struct {
  139. Enabled bool
  140. Address string
  141. User string
  142. UseTLS bool
  143. InsecureAdminAccess bool
  144. Theme string
  145. }
  146. func (gc GUIConfig) String() string {
  147. return fmt.Sprintf(
  148. `Enabled: %v
  149. Address: %v
  150. User: %v
  151. TLS used: %v
  152. Insecure admin access: %v
  153. Theme: %v`,
  154. gc.Enabled,
  155. gc.Address,
  156. gc.User,
  157. gc.UseTLS,
  158. gc.InsecureAdminAccess,
  159. gc.Theme,
  160. )
  161. }
  162. type Options struct {
  163. ListenAddresses []string
  164. GlobalAnnounceServers []string
  165. GlobalAnnounceEnabled bool
  166. LocalAnnounceEnabled bool
  167. LocalAnnouncePort int
  168. LocalAnnounceMCAddr string
  169. MaxSendKbps int
  170. MaxRecvKbps int
  171. ReconnectionIntervalS int
  172. RelaysEnabled bool
  173. RelayReconnectIntervalM int
  174. StartBrowser bool
  175. NatEnabled bool
  176. NatLeaseMinutes int
  177. NatRenewalMinutes int
  178. NatTimeoutSeconds int
  179. UrAccepted int
  180. UrUniqueID string
  181. UrURL string
  182. UrPostInsecurely bool
  183. UrInitialDelayS int
  184. RestartOnWakeup bool
  185. AutoUpgradeIntervalH int
  186. KeepTemporariesH int
  187. CacheIgnoredFiles bool
  188. ProgressUpdateIntervalS int
  189. LimitBandwidthInLan bool
  190. MinHomeDiskFreePct int
  191. ReleasesURL string
  192. AlwaysLocalNets []string
  193. OverwriteRemoteDeviceNamesOnConnect bool
  194. TempIndexMinBlocks int
  195. }
  196. func (o Options) String() string {
  197. return fmt.Sprintf(
  198. `Listen addresses: %v
  199. Global announce servers: %v
  200. Global announce enabled: %v
  201. Local announce enabled: %v
  202. Local announce port: %v
  203. Local announce MAC address: %v
  204. Max sending speed: %v kbit/s
  205. Max receiving speed: %v kbit/s
  206. Reconnection interval: %v s
  207. Relays enabled: %v
  208. Relay reconnect interval: %v min
  209. Start browser: %v
  210. NAT enabled: %v
  211. NAT lease: %v min
  212. NAT renewal: %v min
  213. NAT timeout: %v s
  214. Usage reports accepted: %v
  215. Usage report unique ID: %v
  216. Usage report URL: %v
  217. Post usage reports insecurely: %v
  218. Usage report initial delay: %v s
  219. Restart on wakeup: %v
  220. Auto upgrade interval: %v h
  221. Keep temporary failed transfers: %v h
  222. Cache ignored files: %v
  223. Progress update interval: %v s
  224. Limit bandwidth in LAN: %v
  225. Minimal home disk free space: %v%%
  226. Releases URL: %v
  227. Always local networks: %v
  228. Overwrite remote device names on connect: %v
  229. `,
  230. indentStringSlice(o.ListenAddresses, 2),
  231. indentStringSlice(o.GlobalAnnounceServers, 2),
  232. o.GlobalAnnounceEnabled,
  233. o.LocalAnnounceEnabled,
  234. o.LocalAnnouncePort,
  235. o.LocalAnnounceMCAddr,
  236. o.MaxSendKbps,
  237. o.MaxRecvKbps,
  238. o.ReconnectionIntervalS,
  239. o.RelaysEnabled,
  240. o.RelayReconnectIntervalM,
  241. o.StartBrowser,
  242. o.NatEnabled,
  243. o.NatLeaseMinutes,
  244. o.NatRenewalMinutes,
  245. o.NatTimeoutSeconds,
  246. o.UrAccepted,
  247. o.UrUniqueID,
  248. o.UrURL,
  249. o.UrPostInsecurely,
  250. o.UrInitialDelayS,
  251. o.RestartOnWakeup,
  252. o.AutoUpgradeIntervalH,
  253. o.KeepTemporariesH,
  254. o.CacheIgnoredFiles,
  255. o.ProgressUpdateIntervalS,
  256. o.LimitBandwidthInLan,
  257. o.MinHomeDiskFreePct,
  258. o.ReleasesURL,
  259. indentStringSlice(o.AlwaysLocalNets, 2),
  260. o.OverwriteRemoteDeviceNamesOnConnect,
  261. )
  262. }
  263. type STConfig struct {
  264. Version int
  265. Folders []Folder
  266. Devices []Device
  267. GUI GUIConfig
  268. Options Options
  269. IgnoredDevices []string
  270. IgnoredFolders []string
  271. }
  272. func (c STConfig) String() string {
  273. return fmt.Sprintf(
  274. `Version: %v
  275. GUI: %v
  276. Options: %v
  277. Ignored devices: %v
  278. Ignored folders: %v`,
  279. c.Version,
  280. indent(c.GUI.String(), 2),
  281. indent(c.Options.String(), 2),
  282. indentStringSlice(c.IgnoredDevices, 2),
  283. indentStringSlice(c.IgnoredFolders, 2),
  284. )
  285. }
  286. func GetConfig(cfg *config.Config) (*STConfig, error) {
  287. req := NewClient(cfg).Request().Path(ConfigPath)
  288. resp, err := req.Send()
  289. if err != nil {
  290. return nil, err
  291. }
  292. err = checkResponseOK(resp)
  293. if err != nil {
  294. return nil, err
  295. }
  296. stc := new(STConfig)
  297. err = resp.JSON(stc)
  298. if err != nil {
  299. return nil, err
  300. }
  301. return stc, nil
  302. }