Files
s3-cli/cmd/retention-set.go
zeekay e395fa9439 Remove MinIO, SUBNET and server admin from the client
Vendor the CLI helpers into pkg/, use hanzos3/go, rename the module
to hanzoai/s3-cli, and gate the names in cmd/names_test.go.

Claude-Session: https://claude.ai/code/session_011i2AG9nwpiKUCVxAdDzYuU
2026-09-29 19:37:40 -07:00

162 lines
5.3 KiB
Go

// Copyright (c) 2015-2022 Hanzo AI, Inc.
//
// This file is part of Hanzo S3 stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"context"
"strings"
"time"
"github.com/fatih/color"
"github.com/hanzoai/s3-cli/pkg/cli"
"github.com/hanzoai/s3-cli/pkg/console"
"github.com/hanzoai/s3-cli/pkg/probe"
s3 "github.com/hanzos3/go"
)
var retentionSetFlags = []cli.Flag{
cli.BoolFlag{
Name: "recursive, r",
Usage: "apply retention recursively",
},
cli.BoolFlag{
Name: "bypass",
Usage: "bypass governance",
},
cli.StringFlag{
Name: "version-id, vid",
Usage: "apply retention to a specific object version",
},
cli.StringFlag{
Name: "rewind",
Usage: "roll back object(s) to current version at specified time",
},
cli.BoolFlag{
Name: "versions",
Usage: "apply retention object(s) and all its versions",
},
cli.BoolFlag{
Name: "default",
Usage: "set bucket default retention mode",
},
}
var retentionSetCmd = cli.Command{
Name: "set",
Usage: "apply retention settings on object(s)",
Action: mainRetentionSet,
OnUsageError: onUsageError,
Before: setGlobalsFromContext,
Flags: append(retentionSetFlags, globalFlags...),
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} [FLAGS] [governance | compliance] VALIDITY TARGET
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
VALIDITY:
This argument must be formatted like Nd or Ny where 'd' denotes days and 'y' denotes years e.g. 10d, 3y.
EXAMPLES:
1. Set object retention for a specific object
$ {{.HelpName}} compliance 30d mys3/mybucket/prefix/obj.csv
2. Set object retention for recursively for all objects at a given prefix
$ {{.HelpName}} governance 30d mys3/mybucket/prefix --recursive
3. Set object retention to a specific version of a specific object
$ {{.HelpName}} governance 30d mys3/mybucket/prefix/obj.csv --version-id "3Jr2x6fqlBUsVzbvPihBO3HgNpgZgAnp"
4. Set object retention for recursively for all versions of all objects
$ {{.HelpName}} governance 30d mys3/mybucket/prefix --recursive --versions
5. Set default lock retention configuration for a bucket
$ {{.HelpName}} --default governance 30d mys3/mybucket/
`,
}
func parseSetRetentionArgs(cliCtx *cli.Context) (target, versionID string, recursive bool, timeRef time.Time, withVersions bool, mode s3.RetentionMode, validity uint64, unit s3.ValidityUnit, bypass, bucketMode bool) {
args := cliCtx.Args()
if len(args) != 3 {
showCommandHelpAndExit(cliCtx, 1)
}
mode = s3.RetentionMode(strings.ToUpper(args[0]))
if !mode.IsValid() {
fatalIf(errInvalidArgument().Trace(args...), "invalid retention mode '%v'", mode)
}
var err *probe.Error
validity, unit, err = parseRetentionValidity(args[1])
fatalIf(err.Trace(args[1]), "invalid validity argument")
target = args[2]
if target == "" {
fatalIf(errInvalidArgument().Trace(), "invalid target url '%v'", target)
}
versionID = cliCtx.String("version-id")
timeRef = parseRewindFlag(cliCtx.String("rewind"))
withVersions = cliCtx.Bool("versions")
recursive = cliCtx.Bool("recursive")
bypass = cliCtx.Bool("bypass")
bucketMode = cliCtx.Bool("default")
if bucketMode && (versionID != "" || !timeRef.IsZero() || withVersions || recursive || bypass) {
fatalIf(errDummy(), "--default cannot be specified with any of --version-id, --rewind, --versions, --recursive, --bypass.")
}
return
}
// Set Retention for one object/version or many objects within a given prefix.
func setRetention(ctx context.Context, target, versionID string, timeRef time.Time, withVersions, isRecursive bool,
mode s3.RetentionMode, validity uint64, unit s3.ValidityUnit, bypassGovernance bool,
) error {
return applyRetention(ctx, lockOpSet, target, versionID, timeRef, withVersions, isRecursive, mode, validity, unit, bypassGovernance)
}
func setBucketLock(urlStr string, mode s3.RetentionMode, validity uint64, unit s3.ValidityUnit) error {
return applyBucketLock(lockOpSet, urlStr, mode, validity, unit)
}
// main for retention set command.
func mainRetentionSet(cliCtx *cli.Context) error {
ctx, cancelSetRetention := context.WithCancel(globalContext)
defer cancelSetRetention()
console.SetColor("RetentionSuccess", color.New(color.FgGreen, color.Bold))
console.SetColor("RetentionFailure", color.New(color.FgYellow))
target, versionID, recursive, rewind, withVersions, mode, validity, unit, bypass, bucketMode := parseSetRetentionArgs(cliCtx)
if bucketMode {
return setBucketLock(target, mode, validity, unit)
}
fatalIfBucketLockNotSupported(ctx, target)
if withVersions && rewind.IsZero() {
rewind = time.Now().UTC()
}
return setRetention(ctx, target, versionID, rewind, withVersions, recursive, mode, validity, unit, bypass)
}