CLI client for Syncthing in Go
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "path"
  6. "git.dtluna.net/dtluna/syncthing-cli/commands"
  7. "git.dtluna.net/dtluna/syncthing-cli/config"
  8. "git.dtluna.net/dtluna/syncthing-cli/constants"
  9. "github.com/OpenPeeDeeP/xdg"
  10. "github.com/Unknwon/com"
  11. "gopkg.in/alecthomas/kingpin.v2"
  12. )
  13. func printToStderr(v interface{}) {
  14. fmt.Fprintf(
  15. os.Stderr,
  16. "%v: error: creating default config file: %v\n",
  17. constants.AppName,
  18. v,
  19. )
  20. }
  21. func fatalIfError(err error) {
  22. if err != nil {
  23. printToStderr(err)
  24. os.Exit(1)
  25. }
  26. }
  27. func main() {
  28. x := xdg.New("", constants.AppName)
  29. configDir := x.ConfigHome()
  30. defaultConfigPath := path.Join(configDir, "config.ini")
  31. if !com.IsExist(defaultConfigPath) {
  32. if !com.IsExist(configDir) {
  33. err := os.MkdirAll(configDir, os.ModePerm)
  34. fatalIfError(err)
  35. }
  36. err := config.CreateBlankConfigFile(defaultConfigPath)
  37. fatalIfError(err)
  38. }
  39. app := kingpin.New(constants.AppName, "CLI client for Syncthing")
  40. app.Version(constants.Version)
  41. configPath := app.Flag("config", "Location of the config file.").
  42. Short('c').
  43. Default(defaultConfigPath).
  44. ExistingFile()
  45. address := app.Flag("address", "Address of the Syncthing daemon.").
  46. Short('a').
  47. TCP()
  48. apiKey := app.Flag("api-key", "API key to access the REST API of the Syncthing daemon.").
  49. Short('k').
  50. String()
  51. version := app.Command("version", "Show the current Syncthing version information.").Alias("v")
  52. configCmd := app.Command("config", "Show the current configuration.").Alias("c").Alias("conf")
  53. device := app.Command("device", "Work with devices.").Alias("d").Alias("dev")
  54. deviceList := device.Command("list", "List devices.").Alias("l").Alias("ls")
  55. deviceStats := device.Command("stats", "Show device stats.").Alias("s").Alias("st")
  56. folder := app.Command("folder", "Work with folders.").Alias("f").Alias("fl").Alias("fold")
  57. folderList := folder.Command("list", "List folders.").Alias("l").Alias("ls")
  58. commandName := kingpin.MustParse(app.Parse(os.Args[1:]))
  59. cfg, err := config.Parse(*configPath)
  60. app.FatalIfError(err, "parsing config")
  61. cfg, err = config.Merge(*cfg, *address, *apiKey)
  62. app.FatalIfError(err, "merging config with arguments")
  63. err = func() error {
  64. switch commandName {
  65. case configCmd.FullCommand():
  66. return commands.Config(cfg)
  67. case version.FullCommand():
  68. return commands.Version(cfg)
  69. case deviceList.FullCommand():
  70. return commands.DeviceList(cfg)
  71. case deviceStats.FullCommand():
  72. return commands.DeviceStats(cfg)
  73. case folderList.FullCommand():
  74. return commands.FolderList(cfg)
  75. }
  76. return nil
  77. }()
  78. app.FatalIfError(err, "")
  79. }