Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-factor cmd functions to library #524

Merged
merged 2 commits into from
Jun 6, 2024
Merged

Re-factor cmd functions to library #524

merged 2 commits into from
Jun 6, 2024

Conversation

willscott
Copy link
Member

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
Copy link

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
Copy link
Member Author

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

@lordshashank
Copy link

lordshashank commented Jun 5, 2024

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
Copy link
Member Author

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

Copy link
Member

@rvagg rvagg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

@willscott willscott merged commit 9b1fdf2 into master Jun 6, 2024
17 checks passed
@willscott willscott deleted the feat/cmdlib branch June 6, 2024 06:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants