ipld / go-car

A content addressible archive utility
Other
145 stars 44 forks source link

Re-factor cmd functions to library #524

Closed willscott closed 1 month ago

willscott commented 1 month ago

Address all but list as requested in #523

@lordshashank - list seems to be really focused on a bunch of different textual printed output - i'm not really sure what a library variant of it would be vs what's already provided in the main carv2 library interface - can you specify if there's something specific you're after there?

lordshashank commented 1 month ago

yeah I agree list command is pretty straight forward, but with unixfs it's little complex. my aim was to just get all the CIDs in a car as output from function i.e. I provide path and get the result as root cid and rest as array. Can be skipped though as root gives the root cid which is needed mostly. Another point was, could we provide a wrapper function on extract for file and directory which just take in input and output path/locations, other function would also be available if someone wants to customize something. This is cause mostly user would just need a function that could take in their file location and give them carm not concerned about any other things.

willscott commented 1 month ago

I don't understand the extract wrapper you're asking for - can you provide the method signature?

lordshashank commented 1 month ago

I implemented it like this


// ExtractCar pulls files and directories out of a car
func ExtractCar(carPath string, /*outputDir*/outputPath string /*, path*/) error {
    c := context.Background()

    var store storage.ReadableStorage
    var roots []cid.Cid

    carFile, err := os.Open(carPath)
    if err != nil {
        return err
    }
    store, err = carstorage.OpenReadable(carFile)
    if err != nil {
        return err
    }
    roots = store.(carstorage.ReadableCar).Roots()

    ls := cidlink.DefaultLinkSystem()
    ls.TrustedStorage = true
    ls.SetReadStorage(store)

    // path, err := pathSegments(path)
    // if err != nil {
    //  return err
    // }

    var extractedFiles int
    for _, root := range roots {
        count, err := extractRoot(&c, &ls, root,outputPath /*outputDir, path*/)
        if err != nil {
            return err
        }
        extractedFiles += count
    }
    if extractedFiles == 0 {
        return fmt.Errorf("no files extracted")
    } else {
        fmt.Println("Extracted", extractedFiles, "files")
    }

    return nil
}

My need mostly was to extract files thus have commented out the directory functions (kept comments just in case need arises), basically it just takes in input and output file and gives the car, most use cases are like this only. We can keep similar function for files and folders in here as well.

willscott commented 1 month ago

Added a helper - close enough to what you're after?