github.com/puellanivis/breton@v0.2.16/lib/files/option.go (about) 1 package files 2 3 import ( 4 "os" 5 "time" 6 ) 7 8 // Option is a function that applies a specific option to a files.File, it 9 // returns an Option and and error. If error is not nil, then the Option 10 // returned will revert the option that was set. Since errors returned by 11 // Option arguments are discarded by Open(), and Create(), if you 12 // care about the error status of an Option you must apply it yourself 13 // after Open() or Create() 14 type Option func(File) (Option, error) 15 16 // WithFileMode returns an Option that will set the files.File.Stat().FileMode() to the given os.FileMode. 17 func WithFileMode(mode os.FileMode) Option { 18 type chmoder interface { 19 Chmod(os.FileMode) error 20 } 21 22 return func(f File) (Option, error) { 23 fi, err := f.Stat() 24 if err != nil { 25 return nil, err 26 } 27 28 save := fi.Mode() 29 30 switch f := f.(type) { 31 case chmoder: 32 if err := f.Chmod(mode); err != nil { 33 return nil, err 34 } 35 36 default: 37 return nil, ErrNotSupported 38 } 39 40 return WithFileMode(save), nil 41 } 42 } 43 44 type observer interface { 45 Observe(float64) 46 } 47 48 type copyConfig struct { 49 runningTimeout time.Duration 50 buffer []byte 51 52 bwScale float64 53 bwCount int 54 bwInterval time.Duration 55 bwRunning observer 56 bwLifetime observer 57 } 58 59 // CopyOption defines a function that applies a value or setting for a specific files.Copy operation. 60 type CopyOption func(c *copyConfig) CopyOption 61 62 // WithWatchdogTimeout sets a running interval timeout, 63 // where if no copy progress is made during that time, 64 // the files.Copy will fail with a timeout error. 65 func WithWatchdogTimeout(timeout time.Duration) CopyOption { 66 return func(c *copyConfig) CopyOption { 67 save := c.runningTimeout 68 69 c.runningTimeout = timeout 70 71 return WithWatchdogTimeout(save) 72 } 73 } 74 75 // WithBuffer sets which buffer a files.Copy should use internally as temporary storage between the Read and Write. 76 func WithBuffer(buf []byte) CopyOption { 77 return func(c *copyConfig) CopyOption { 78 save := c.buffer 79 80 c.buffer = buf 81 82 return WithBuffer(save) 83 } 84 } 85 86 // WithBufferSize makes a new buffer of size bytes, which is used as temporary intermediate storage for the files.Copy. 87 func WithBufferSize(size int) CopyOption { 88 if size < 0 { 89 panic("cannot use a negative buffer size!") 90 } 91 92 return WithBuffer(make([]byte, size)) 93 } 94 95 // WithMetricsScale sets the scale of reported Metrics, otherwise it is reported in bytes/second. 96 func WithMetricsScale(scale float64) CopyOption { 97 return func(c *copyConfig) CopyOption { 98 save := c.bwScale 99 100 c.bwScale = scale 101 102 return WithMetricsScale(save) 103 } 104 } 105 106 // WithBandwidthMetrics establishes a lifetime bandwidth metric for the files.Copy. 107 func WithBandwidthMetrics(total interface{ Observe(float64) }) CopyOption { 108 return func(c *copyConfig) CopyOption { 109 save := c.bwLifetime 110 111 c.bwLifetime = total 112 113 return WithBandwidthMetrics(save) 114 } 115 } 116 117 // WithIntervalBandwidthMetrics keeps a running list of n intervals and reports the bandwidth over this window. 118 func WithIntervalBandwidthMetrics(running interface{ Observe(float64) }, n int, interval time.Duration) CopyOption { 119 return func(c *copyConfig) CopyOption { 120 saveOb := c.bwRunning 121 saveCount := c.bwCount 122 saveDur := c.bwInterval 123 124 c.bwRunning = running 125 c.bwCount = n 126 c.bwInterval = interval 127 128 return WithIntervalBandwidthMetrics(saveOb, saveCount, saveDur) 129 } 130 }