github.com/minio/mc@v0.0.0-20240507152021-646712d5e5fb/cmd/ilm-rule-export.go (about) 1 // Copyright (c) 2015-2022 MinIO, Inc. 2 // 3 // This file is part of MinIO Object Storage stack 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package cmd 19 20 import ( 21 "context" 22 "errors" 23 "time" 24 25 "github.com/minio/cli" 26 json "github.com/minio/colorjson" 27 "github.com/minio/mc/pkg/probe" 28 "github.com/minio/minio-go/v7/pkg/lifecycle" 29 ) 30 31 var ilmExportCmd = cli.Command{ 32 Name: "export", 33 Usage: "export lifecycle configuration in JSON format", 34 Action: mainILMExport, 35 OnUsageError: onUsageError, 36 Before: setGlobalsFromContext, 37 Flags: globalFlags, 38 CustomHelpTemplate: `NAME: 39 {{.HelpName}} - {{.Usage}} 40 41 USAGE: 42 {{.HelpName}} TARGET 43 44 DESCRIPTION: 45 Exports lifecycle configuration in JSON format to STDOUT. 46 47 EXAMPLES: 48 1. Export lifecycle configuration for 'mybucket' to 'lifecycle.json' file. 49 {{.Prompt}} {{.HelpName}} myminio/mybucket > lifecycle.json 50 51 2. Print lifecycle configuration for 'mybucket' to STDOUT. 52 {{.Prompt}} {{.HelpName}} play/mybucket 53 `, 54 } 55 56 type ilmExportMessage struct { 57 Status string `json:"status"` 58 Target string `json:"target"` 59 Config *lifecycle.Configuration `json:"config"` 60 UpdatedAt time.Time `json:"updatedAt,omitempty"` 61 } 62 63 func (i ilmExportMessage) String() string { 64 msgBytes, e := json.MarshalIndent(i.Config, "", " ") 65 fatalIf(probe.NewError(e), "Unable to export ILM configuration") 66 67 return string(msgBytes) 68 } 69 70 func (i ilmExportMessage) JSON() string { 71 msgBytes, e := json.MarshalIndent(i, "", " ") 72 fatalIf(probe.NewError(e), "Unable to marshal ILM message") 73 74 return string(msgBytes) 75 } 76 77 // checkILMExportSyntax - validate arguments passed by user 78 func checkILMExportSyntax(ctx *cli.Context) { 79 if len(ctx.Args()) != 1 { 80 showCommandHelpAndExit(ctx, globalErrorExitStatus) 81 } 82 } 83 84 func mainILMExport(cliCtx *cli.Context) error { 85 ctx, cancelILMExport := context.WithCancel(globalContext) 86 defer cancelILMExport() 87 88 checkILMExportSyntax(cliCtx) 89 setILMDisplayColorScheme() 90 91 args := cliCtx.Args() 92 urlStr := args.Get(0) 93 94 client, err := newClient(urlStr) 95 fatalIf(err.Trace(args...), "Unable to initialize client for "+urlStr+".") 96 97 ilmCfg, updatedAt, err := client.GetLifecycle(ctx) 98 fatalIf(err.Trace(args...), "Unable to get lifecycle configuration") 99 if len(ilmCfg.Rules) == 0 { 100 fatalIf(probe.NewError(errors.New("lifecycle configuration not set")).Trace(urlStr), 101 "Unable to export lifecycle configuration") 102 } 103 104 printMsg(ilmExportMessage{ 105 Status: "success", 106 Target: urlStr, 107 Config: ilmCfg, 108 UpdatedAt: updatedAt, 109 }) 110 111 return nil 112 }