gopar icon indicating copy to clipboard operation
gopar copied to clipboard

libgopar

Open brenthuisman opened this issue 4 years ago • 3 comments

The second thing I'd like to do is make gopar suitable for use as a library. By that I don't mean all the primitives it contains, but (also?) the interface through main(). The fastest way to to it would be to turn main() into par(args []string), and have main() call it. However I'm certain you would prefer a break up into separate create(), verify() and repair() functions :) In which case, the flagsetting part of main needs to be duplicated. Copy paste or is there a cleaner way? With my C++/Python hat on, I think of hiding all that in a class, but that doesn't seem to be the Go way so I ask.

  • What form would you prefer? Maybe another?
  • Where in the file hierarchy would you put the lib, meant for external use? Is there a convention like /cmd? Maybe just the root?

brenthuisman avatar Jan 12 '21 10:01 brenthuisman

Yeah, I'm open to this, but first I'd like to ask what the use case of this would be. Is the intent to replicate the command-line app exactly in some other form? If so, then a single par(args []string) function might actually be best, but I think that's not what library integrations look like most of the time.

I'd imagine if it were to integrate it in some other app, then each high-level function (create, verify, repair, etc.) would be a single function in a package, and all the flags would be replaced with Options structs.

On the other hand, if the par stuff is meant to be one command in a bigger command-line app (like how create etc. are all different commands in the single par app), then I think then the interface for that can be a single function.

So yeah, it all depends on what you ultimately want to do. :)

akalin avatar Jan 12 '21 21:01 akalin

I forked libpar2cmdline and added a main(args []string) style function as interface because it was such a pile of spaghetti and, similar to gopar, there's a bit of initialization going on before the actual c/v/r functions are called which I didn't want to replicate. It works perfectly (of course), and ultimately that's what I care about, but having three functions would be a bit cleaner, where you pass in a filename (/handle?) and a set of parameters (The Options struct would be a struct of all options with a default set?).

I imagine you'd like to support not just my application, hence the question :)

brenthuisman avatar Jan 13 '21 09:01 brenthuisman

Yeah, having separate functions for the major operations would be the way I'd do it, I think. For example, Create would look like (CreateResult, err) Create(parFile string, inputFiles []string, options CreateOptions, delegate ...);, where CreateOptions would look something like struct { blockSize int; recoveryBlockCount int; ... }, CreateResult would maybe have some info about the created files, and delegate would be called to log events and such.

akalin avatar Jan 16 '21 00:01 akalin