file.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright © 2021 Ettore Di Giacinto <[email protected]>
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program; if not, see <http://www.gnu.org/licenses/>.
  15. package cmd
  16. import (
  17. "context"
  18. "errors"
  19. "time"
  20. "github.com/mudler/edgevpn/pkg/node"
  21. "github.com/mudler/edgevpn/pkg/services"
  22. "github.com/urfave/cli"
  23. )
  24. func cliNamePath(c *cli.Context) (name, path string, err error) {
  25. name = c.Args().Get(0)
  26. path = c.Args().Get(1)
  27. if name == "" && c.String("name") == "" {
  28. err = errors.New("Either a file UUID as first argument or with --name needs to be provided")
  29. return
  30. }
  31. if path == "" && c.String("path") == "" {
  32. err = errors.New("Either a file UUID as first argument or with --name needs to be provided")
  33. return
  34. }
  35. if c.String("name") != "" {
  36. name = c.String("name")
  37. }
  38. if c.String("path") != "" {
  39. path = c.String("path")
  40. }
  41. return name, path, nil
  42. }
  43. func FileSend() cli.Command {
  44. return cli.Command{
  45. Name: "file-send",
  46. Aliases: []string{"fs"},
  47. Usage: "Serve a file to the network",
  48. Description: `Serve a file to the network without connecting over VPN`,
  49. UsageText: "edgevpn file-send unique-id /src/path",
  50. Flags: append(CommonFlags,
  51. cli.StringFlag{
  52. Name: "name",
  53. Required: true,
  54. Usage: `Unique name of the file to be served over the network.
  55. This is also the ID used to refer when receiving it.`,
  56. },
  57. cli.StringFlag{
  58. Name: "path",
  59. Usage: `File to serve`,
  60. Required: true,
  61. },
  62. ),
  63. Action: func(c *cli.Context) error {
  64. name, path, err := cliNamePath(c)
  65. if err != nil {
  66. return err
  67. }
  68. o, _, ll := cliToOpts(c)
  69. opts, err := services.ShareFile(ll, time.Duration(c.Int("ledger-announce-interval"))*time.Second, name, path)
  70. if err != nil {
  71. return err
  72. }
  73. o = append(o, opts...)
  74. e, err := node.New(o...)
  75. if err != nil {
  76. return err
  77. }
  78. displayStart(ll)
  79. // Start the node to the network, using our ledger
  80. if err := e.Start(context.Background()); err != nil {
  81. return err
  82. }
  83. for {
  84. time.Sleep(2 * time.Second)
  85. }
  86. },
  87. }
  88. }
  89. func FileReceive() cli.Command {
  90. return cli.Command{
  91. Name: "file-receive",
  92. Aliases: []string{"fr"},
  93. Usage: "Receive a file which is served from the network",
  94. Description: `Receive a file from the network without connecting over VPN`,
  95. UsageText: "edgevpn file-receive unique-id /dst/path",
  96. Flags: append(CommonFlags,
  97. cli.StringFlag{
  98. Name: "name",
  99. Usage: `Unique name of the file to be received over the network.`,
  100. },
  101. cli.StringFlag{
  102. Name: "path",
  103. Usage: `Destination where to save the file`,
  104. },
  105. ),
  106. Action: func(c *cli.Context) error {
  107. name, path, err := cliNamePath(c)
  108. if err != nil {
  109. return err
  110. }
  111. o, _, ll := cliToOpts(c)
  112. e, err := node.New(o...)
  113. if err != nil {
  114. return err
  115. }
  116. displayStart(ll)
  117. // Start the node to the network, using our ledger
  118. if err := e.Start(context.Background()); err != nil {
  119. return err
  120. }
  121. ledger, _ := e.Ledger()
  122. return services.ReceiveFile(context.Background(), ledger, e, ll, time.Duration(c.Int("ledger-announce-interval"))*time.Second, name, path)
  123. },
  124. }
  125. }