|
| 1 | +# flop |
| 2 | +flop is a Golang file operations library concentrating on safety and feature parity with |
| 3 | +[GNU cp](https://www.gnu.org/software/coreutils/manual/html_node/cp-invocation.html). |
| 4 | +Most administrators and engineers interact with GNU utilities every day, so it makes sense to utilize |
| 5 | +that knowledge and expectations for a library that does the same operation in code. flop strategically |
| 6 | +diverges from cp where it is advantageous for the programmer to explicitly define the behavior, like |
| 7 | +cp assuming that copying from a file path to a directory path means the file should be created inside the directory. |
| 8 | +This behavior must be explicitly defined in flop by passing the option AppendNameToPath, otherwise |
| 9 | +an error will be returned. |
| 10 | + |
| 11 | +### Usage |
| 12 | +Basic file copy. |
| 13 | +```go |
| 14 | +err := flop.SimpleCopy("src_path", "dst_path") |
| 15 | +handle(err) |
| 16 | +``` |
| 17 | + |
| 18 | +Advanced file copy with options. |
| 19 | +```go |
| 20 | +options := flop.Options{ |
| 21 | + Recursive: true, |
| 22 | + MkdirAll: true, |
| 23 | +} |
| 24 | +err := flop.Copy("src_path", "dst_path", options) |
| 25 | +handle(err) |
| 26 | +``` |
| 27 | + |
| 28 | +### Logging |
| 29 | +flop won't throw logs at you for no reason, but if you want to follow along with what's going on giving it a logger |
| 30 | +can help expose the behavior, or aid in debugging if you are generous enough to contribute. |
| 31 | +```go |
| 32 | +// the logger just takes a string so format your favorite logger to accept one |
| 33 | +import ( |
| 34 | + "flop" |
| 35 | + "github.com/rs/zerolog" |
| 36 | + zlog "github.com/rs/zerolog/log" |
| 37 | + llog "github.com/sirupsen/logrus" |
| 38 | +) |
| 39 | + |
| 40 | +func logDebug(msg string) { |
| 41 | + llog.WithFields(llog.Fields{ |
| 42 | + "application": "stuffcopy", |
| 43 | + }).Info(msg) |
| 44 | +} |
| 45 | + |
| 46 | +func main() { |
| 47 | + zlog.Logger = zlog.Output(zerolog.ConsoleWriter{Out: os.Stderr}) |
| 48 | + err := flop.Copy(src.Name(), dst.Name(), flop.Options{ |
| 49 | + InfoLogFunc: zlog.Info().Msg, // Msg already accepts a string so we can just pass it directly |
| 50 | + DebugLogFunc: logDebug, // logrus Debug takes ...interface{} so we need to wrap it |
| 51 | + }) |
| 52 | + handle(err) |
| 53 | +} |
| 54 | +``` |
0 commit comments