|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "go.uber.org/zap" |
| 8 | +) |
| 9 | + |
| 10 | +type RCloneArgs struct { |
| 11 | + AllowOther string `json:"allow-other,omitempty"` |
| 12 | + AllowRoot string `json:"allow-root,omitempty"` |
| 13 | + AsyncRead string `json:"async-read,omitempty"` |
| 14 | + AttrTimeout string `json:"attr-timeout,omitempty"` |
| 15 | + ConfigPath string `json:"config,omitempty"` |
| 16 | + Daemon string `json:"daemon,omitempty"` |
| 17 | + DaemonTimeout string `json:"daemon-timeout,omitempty"` |
| 18 | + DaemonWait string `json:"daemon-wait,omitempty"` |
| 19 | + DirCacheTime string `json:"dir-cache-time,omitempty"` |
| 20 | + DirectIO string `json:"direct-io,omitempty"` |
| 21 | + GID string `json:"gid,omitempty"` |
| 22 | + LogFile string `json:"log-file,omitempty"` |
| 23 | + NoModificationTime string `json:"no-modtime,omitempty"` |
| 24 | + PollInterval string `json:"poll-interval,omitempty"` |
| 25 | + ReadOnly string `json:"read-only,omitempty"` |
| 26 | + UID string `json:"uid,omitempty"` |
| 27 | + UMask string `json:"umask,omitempty"` |
| 28 | + VfsCacheMaxAge string `json:"vfs-cache-max-age,omitempty"` |
| 29 | + VfsCacheMaxSize string `json:"vfs-cache-max-size,omitempty"` |
| 30 | + VfsCacheMinFreeSpace string `json:"vfs-cache-min-free-space,omitempty"` |
| 31 | + VfsCacheMode string `json:"vfs-cache-mode,omitempty"` |
| 32 | + VfsCachePollInterval string `json:"vfs-cache-poll-interval,omitempty"` |
| 33 | + VfsDiskSpaceTotalSize string `json:"vfs-disk-space-total-size,omitempty"` |
| 34 | + VfsReadAhead string `json:"vfs-read-ahead,omitempty"` |
| 35 | + VfsReadChunkSize string `json:"vfs-read-chunk-size,omitempty"` |
| 36 | + VfsReadChunkSizeLimit string `json:"vfs-read-chunk-size-limit,omitempty"` |
| 37 | + VfsReadChunkStreams string `json:"vfs-read-chunk-streams,omitempty"` |
| 38 | + VfsReadWait string `json:"vfs-read-wait,omitempty"` |
| 39 | + VfsRefresh string `json:"vfs-refresh,omitempty"` |
| 40 | + VfsWriteBack string `json:"vfs-write-back,omitempty"` |
| 41 | + VfsWriteWait string `json:"vfs-write-wait,omitempty"` |
| 42 | + WriteBackCache string `json:"write-back-cache,omitempty"` |
| 43 | +} |
| 44 | + |
| 45 | +func (args RCloneArgs) PopulateArgsSlice(bucket, targetPath string) ([]string, error) { |
| 46 | + // Marshal to JSON |
| 47 | + raw, err := json.Marshal(args) |
| 48 | + if err != nil { |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + |
| 52 | + // Unmarshal into map[string]string |
| 53 | + var m map[string]string |
| 54 | + if err := json.Unmarshal(raw, &m); err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + |
| 58 | + // Convert to key=value slice |
| 59 | + result := []string{"mount", bucket, targetPath} |
| 60 | + for k, v := range m { |
| 61 | + result = append(result, fmt.Sprintf("--%s=%v", k, v)) // --key=value |
| 62 | + } |
| 63 | + |
| 64 | + return result, nil // [mount, bucket, path, --key1=value1, --key2=value2, ...] |
| 65 | +} |
| 66 | + |
| 67 | +func (args RCloneArgs) Validate(targetPath string) error { |
| 68 | + if err := pathValidator(targetPath); err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + // Check if value of allow-other parameter is boolean "true" or "false" |
| 73 | + if args.AllowOther != "" { |
| 74 | + if isBool := isBoolString(args.AllowOther); !isBool { |
| 75 | + logger.Error("cannot convert value of allow-other into boolean", zap.Any("allow-other", args.AllowOther)) |
| 76 | + return fmt.Errorf("cannot convert value of allow-other into boolean: %v", args.AllowOther) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // Check if value of allow-root parameter is boolean "true" or "false" |
| 81 | + if args.AllowRoot != "" { |
| 82 | + if isBool := isBoolString(args.AllowRoot); !isBool { |
| 83 | + logger.Error("cannot convert value of allow-root into boolean", zap.Any("allow-root", args.AllowRoot)) |
| 84 | + return fmt.Errorf("cannot convert value of allow-root into boolean: %v", args.AllowRoot) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Check if value of async-read parameter is boolean "true" or "false" |
| 89 | + if args.AsyncRead != "" { |
| 90 | + if isBool := isBoolString(args.AsyncRead); !isBool { |
| 91 | + logger.Error("cannot convert value of async-read into boolean", zap.Any("async-read", args.AsyncRead)) |
| 92 | + return fmt.Errorf("cannot convert value of async-read into boolean: %v", args.AsyncRead) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // Check if rclone config file exists or not |
| 97 | + if exists, err := fileExists(args.ConfigPath); err != nil { |
| 98 | + logger.Error("error checking rclone config file existence") |
| 99 | + return fmt.Errorf("error checking rclone config file existence") |
| 100 | + } else if !exists { |
| 101 | + logger.Error("rclone config file not found") |
| 102 | + return fmt.Errorf("rclone config file not found") |
| 103 | + } |
| 104 | + |
| 105 | + // Check if value of daemon parameter is boolean "true" or "false" |
| 106 | + if args.Daemon != "" { |
| 107 | + if isBool := isBoolString(args.Daemon); !isBool { |
| 108 | + logger.Error("cannot convert value of daemon into boolean", zap.Any("daemon", args.Daemon)) |
| 109 | + return fmt.Errorf("cannot convert value of daemon into boolean: %v", args.Daemon) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Check if value of direct-io parameter is boolean "true" or "false" |
| 114 | + if args.DirectIO != "" { |
| 115 | + if isBool := isBoolString(args.DirectIO); !isBool { |
| 116 | + logger.Error("cannot convert value of direct-io into boolean", zap.Any("direct-io", args.DirectIO)) |
| 117 | + return fmt.Errorf("cannot convert value of direct-io into boolean: %v", args.DirectIO) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // Check if value of no-modtime parameter is boolean "true" or "false" |
| 122 | + if args.NoModificationTime != "" { |
| 123 | + if isBool := isBoolString(args.NoModificationTime); !isBool { |
| 124 | + logger.Error("cannot convert value of no-modtime into boolean", zap.Any("no-modtime", args.NoModificationTime)) |
| 125 | + return fmt.Errorf("cannot convert value of no-modtime into boolean: %v", args.NoModificationTime) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + // Check if value of read-only parameter is boolean "true" or "false" |
| 130 | + if args.ReadOnly != "" { |
| 131 | + if isBool := isBoolString(args.ReadOnly); !isBool { |
| 132 | + logger.Error("cannot convert value of read-only into boolean", zap.Any("read-only", args.ReadOnly)) |
| 133 | + return fmt.Errorf("cannot convert value of read-only into boolean: %v", args.ReadOnly) |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + // Check if value of vfs-refresh parameter is boolean "true" or "false" |
| 138 | + if args.VfsRefresh != "" { |
| 139 | + if isBool := isBoolString(args.VfsRefresh); !isBool { |
| 140 | + logger.Error("cannot convert value of vfs-refresh into boolean", zap.Any("vfs-refresh", args.VfsRefresh)) |
| 141 | + return fmt.Errorf("cannot convert value of vfs-refresh into boolean: %v", args.VfsRefresh) |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + // Check if value of write-back-cache parameter is boolean "true" or "false" |
| 146 | + if args.WriteBackCache != "" { |
| 147 | + if isBool := isBoolString(args.WriteBackCache); !isBool { |
| 148 | + logger.Error("cannot convert value of write-back-cache into boolean", zap.Any("write-back-cache", args.WriteBackCache)) |
| 149 | + return fmt.Errorf("cannot convert value of write-back-cache into boolean: %v", args.WriteBackCache) |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + return nil |
| 154 | +} |
0 commit comments