file.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package cmd
  2. import (
  3. "errors"
  4. "github.com/mudler/edgevpn/pkg/edgevpn"
  5. "github.com/urfave/cli"
  6. )
  7. func cliNamePath(c *cli.Context) (name, path string, err error) {
  8. name = c.Args().Get(0)
  9. path = c.Args().Get(1)
  10. if name == "" && c.String("name") == "" {
  11. err = errors.New("Either a file UUID as first argument or with --name needs to be provided")
  12. return
  13. }
  14. if path == "" && c.String("path") == "" {
  15. err = errors.New("Either a file UUID as first argument or with --name needs to be provided")
  16. return
  17. }
  18. if c.String("name") != "" {
  19. name = c.String("name")
  20. }
  21. if c.String("path") != "" {
  22. path = c.String("path")
  23. }
  24. return name, path, nil
  25. }
  26. func FileSend() cli.Command {
  27. return cli.Command{
  28. Name: "file-send",
  29. Aliases: []string{"fs"},
  30. Usage: "Serve a file to the network",
  31. Description: `Serve a file to the network without connecting over VPN`,
  32. UsageText: "edgevpn file-send unique-id /src/path",
  33. Flags: append(CommonFlags,
  34. cli.StringFlag{
  35. Name: "name",
  36. Required: true,
  37. Usage: `Unique name of the file to be served over the network.
  38. This is also the ID used to refer when receiving it.`,
  39. },
  40. cli.StringFlag{
  41. Name: "path",
  42. Usage: `File to serve`,
  43. Required: true,
  44. },
  45. ),
  46. Action: func(c *cli.Context) error {
  47. name, path, err := cliNamePath(c)
  48. if err != nil {
  49. return err
  50. }
  51. e := edgevpn.New(cliToOpts(c)...)
  52. displayStart(e)
  53. ledger, err := e.Ledger()
  54. if err != nil {
  55. return err
  56. }
  57. // Join the node to the network, using our ledger
  58. e.SendFile(ledger, name, path)
  59. // Join the node to the network, using our ledger
  60. if err := e.Join(); err != nil {
  61. return err
  62. }
  63. for {
  64. }
  65. },
  66. }
  67. }
  68. func FileReceive() cli.Command {
  69. return cli.Command{
  70. Name: "file-receive",
  71. Aliases: []string{"fr"},
  72. Usage: "Receive a file which is served from the network",
  73. Description: `Receive a file from the network without connecting over VPN`,
  74. UsageText: "edgevpn file-receive unique-id /dst/path",
  75. Flags: append(CommonFlags,
  76. cli.StringFlag{
  77. Name: "name",
  78. Usage: `Unique name of the file to be received over the network.`,
  79. },
  80. cli.StringFlag{
  81. Name: "path",
  82. Usage: `Destination where to save the file`,
  83. },
  84. ),
  85. Action: func(c *cli.Context) error {
  86. name, path, err := cliNamePath(c)
  87. if err != nil {
  88. return err
  89. }
  90. e := edgevpn.New(cliToOpts(c)...)
  91. displayStart(e)
  92. // Join the node to the network, using our ledger
  93. if err := e.Join(); err != nil {
  94. return err
  95. }
  96. ledger, _ := e.Ledger()
  97. return e.ReceiveFile(ledger, name, path)
  98. },
  99. }
  100. }