main.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package transfer
  3. import (
  4. "fmt"
  5. "io"
  6. "os"
  7. "strconv"
  8. "strings"
  9. "kitty/tools/cli"
  10. "kitty/tools/utils"
  11. )
  12. var _ = fmt.Print
  13. func read_bypass(loc string) (string, error) {
  14. if loc == "" {
  15. return "", nil
  16. }
  17. fdnum, err := strconv.Atoi(loc)
  18. if err == nil && fdnum >= 0 && fdnum < 256 && loc[0] >= '0' && loc[0] <= '9' {
  19. file := os.NewFile(uintptr(fdnum), loc)
  20. defer file.Close()
  21. raw, err := io.ReadAll(file)
  22. return utils.UnsafeBytesToString(raw), err
  23. }
  24. if loc == "-" {
  25. raw, err := io.ReadAll(os.Stdin)
  26. defer os.Stdin.Close()
  27. return utils.UnsafeBytesToString(raw), err
  28. }
  29. switch loc[0] {
  30. case '.', '~', '/':
  31. if loc[0] == '~' {
  32. loc = utils.Expanduser(loc)
  33. }
  34. raw, err := os.ReadFile(loc)
  35. return utils.UnsafeBytesToString(raw), err
  36. default:
  37. return loc, nil
  38. }
  39. }
  40. func main(cmd *cli.Command, opts *Options, args []string) (rc int, err error) {
  41. if opts.PermissionsBypass != "" {
  42. val, err := read_bypass(opts.PermissionsBypass)
  43. if err != nil {
  44. return 1, err
  45. }
  46. opts.PermissionsBypass = strings.TrimSpace(val)
  47. }
  48. if len(args) == 0 {
  49. return 1, fmt.Errorf("Must specify at least one file to transfer")
  50. }
  51. switch opts.Direction {
  52. case "send", "download":
  53. err, rc = send_main(opts, args)
  54. default:
  55. err, rc = receive_main(opts, args)
  56. }
  57. if err != nil {
  58. rc = 1
  59. }
  60. return
  61. }
  62. func EntryPoint(parent *cli.Command) {
  63. create_cmd(parent, main)
  64. }