file.go 3.3 KB

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