Repository: rclone/rclone
File: backend/b2/b2.go (Go)

Code before:
		var response api.Bucket
		err = f.pacer.Call(func() (bool, error) {
			resp, err := f.srv.CallJSON(ctx, &opts, &request, &response)
			return f.shouldRetry(ctx, resp, err)
		})
		if err != nil {
			return nil, err
		}
		bucket = &response
	} else {
		err = f.listBucketsToFn(ctx, bucketName, func(b *api.Bucket) error {
			bucket = b
			return nil
		})
		if err != nil {
			return nil, err
		}
	}
	if bucket == nil {
		return nil, fs.ErrorDirNotFound
	}
	return bucket.LifecycleRules, nil
}

var commandHelp = []fs.CommandHelp{{
	Name:  "lifecycle",
	Short: "Read or set the lifecycle for a bucket",
	Long: `This command can be used to read or set the lifecycle for a bucket.

Usage Examples:

To show the current lifecycle rules:

    rclone backend lifecycle b2:bucket

This will dump something like this showing the lifecycle rules.

    [
        {
            "daysFromHidingToDeleting": 1,
            "daysFromUploadingToHiding": null,
            "fileNamePrefix": ""
        }
    ]

If there are no lifecycle rules (the default) then it will just return [].

To reset the current lifecycle rules:

    rclone backend lifecycle b2:bucket -o daysFromHidingToDeleting=30
    rclone backend lifecycle b2:bucket -o daysFromUploadingToHiding=5 -o daysFromHidingToDeleting=1

Review comment (suggestion):
I suggest you leave the `lifecycleHelp` where it was - it makes it easier to maintain having the help immediately before the code.

You might want to split the cleanup/cleanup-hidden into seperate functions so you can do the same there too.


Code after:

    rclone backend lifecycle b2:bucket -o daysFromHidingToDeleting=1

See: https://www.backblaze.com/docs/cloud-storage-lifecycle-rules
`,
	Opts: map[string]string{
		"daysFromHidingToDeleting":  "After a file has been hidden for this many days it is deleted. 0 is off.",
		"daysFromUploadingToHiding": "This many days after uploading a file is hidden",
	},
}

func (f *Fs) lifecycleCommand(ctx context.Context, name string, arg []string, opt map[string]string) (out interface{}, err error) {
	var newRule api.LifecycleRule
	if daysStr := opt["daysFromHidingToDeleting"]; daysStr != "" {
		days, err := strconv.Atoi(daysStr)
		if err != nil {
			return nil, fmt.Errorf("bad daysFromHidingToDeleting: %w", err)
		}
		newRule.DaysFromHidingToDeleting = &days
	}
	if daysStr := opt["daysFromUploadingToHiding"]; daysStr != "" {
		days, err := strconv.Atoi(daysStr)
		if err != nil {
			return nil, fmt.Errorf("bad daysFromUploadingToHiding: %w", err)
		}
		newRule.DaysFromUploadingToHiding = &days
	}
	bucketName, _ := f.split("")
	if bucketName == "" {
		return nil, errors.New("bucket required")

	}

	var bucket *api.Bucket
	if newRule.DaysFromHidingToDeleting != nil || newRule.DaysFromUploadingToHiding != nil {
		bucketID, err := f.getBucketID(ctx, bucketName)
		if err != nil {
			return nil, err
		}
		opts := rest.Opts{
			Method: "POST",
			Path:   "/b2_update_bucket",
		}
		var request = api.UpdateBucketRequest{
			ID:             bucketID,
			AccountID:      f.info.AccountID,
			LifecycleRules: []api.LifecycleRule{newRule},
		}
		var response api.Bucket
		err = f.pacer.Call(func() (bool, error) {
			resp, err := f.srv.CallJSON(ctx, &opts, &request, &response)