github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/share-list-main.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  	"fmt"
    22  	"time"
    23  
    24  	"github.com/minio/cli"
    25  	"github.com/minio/mc/pkg/probe"
    26  )
    27  
    28  var shareListFlags = []cli.Flag{}
    29  
    30  // Share documents via URL.
    31  var shareList = cli.Command{
    32  	Name:         "list",
    33  	ShortName:    "ls",
    34  	Usage:        "list previously shared objects",
    35  	Action:       mainShareList,
    36  	OnUsageError: onUsageError,
    37  	Before:       setGlobalsFromContext,
    38  	Flags:        append(shareListFlags, globalFlags...),
    39  	CustomHelpTemplate: `NAME:
    40    {{.HelpName}} COMMAND - {{.Usage}}
    41  
    42  USAGE:
    43    {{.HelpName}} COMMAND
    44  
    45  COMMAND:
    46    upload:   list previously shared access to uploads.
    47    download: list previously shared access to downloads.
    48  
    49  EXAMPLES:
    50    1. List previously shared downloads, that haven't expired yet.
    51        {{.Prompt}} {{.HelpName}} download
    52  
    53    2. List previously shared uploads, that haven't expired yet.
    54        {{.Prompt}} {{.HelpName}} upload
    55  `,
    56  }
    57  
    58  // validate command-line args.
    59  func checkShareListSyntax(ctx *cli.Context) {
    60  	args := ctx.Args()
    61  	if !args.Present() || (args.First() != "upload" && args.First() != "download") {
    62  		showCommandHelpAndExit(ctx, 1) // last argument is exit code.
    63  	}
    64  }
    65  
    66  // doShareList list shared url's.
    67  func doShareList(cmd string) *probe.Error {
    68  	if cmd != "upload" && cmd != "download" {
    69  		return probe.NewError(fmt.Errorf("Unknown argument `%s` passed", cmd))
    70  	}
    71  
    72  	// Fetch defaults.
    73  	uploadsFile := getShareUploadsFile()
    74  	downloadsFile := getShareDownloadsFile()
    75  
    76  	// Load previously saved upload-shares.
    77  	shareDB := newShareDBV1()
    78  
    79  	// if upload - read uploads file.
    80  	if cmd == "upload" {
    81  		if err := shareDB.Load(uploadsFile); err != nil {
    82  			return err.Trace(uploadsFile)
    83  		}
    84  	}
    85  
    86  	// if download - read downloads file.
    87  	if cmd == "download" {
    88  		if err := shareDB.Load(downloadsFile); err != nil {
    89  			return err.Trace(downloadsFile)
    90  		}
    91  	}
    92  
    93  	// Print previously shared entries.
    94  	for shareURL, share := range shareDB.Shares {
    95  		printMsg(shareMessage{
    96  			ObjectURL:   share.URL,
    97  			ShareURL:    shareURL,
    98  			TimeLeft:    share.Expiry - time.Since(share.Date),
    99  			ContentType: share.ContentType,
   100  		})
   101  	}
   102  	return nil
   103  }
   104  
   105  // main entry point for share list.
   106  func mainShareList(ctx *cli.Context) error {
   107  	// validate command-line args.
   108  	checkShareListSyntax(ctx)
   109  
   110  	// Additional command speific theme customization.
   111  	shareSetColor()
   112  
   113  	// Initialize share config folder.
   114  	initShareConfig()
   115  
   116  	// List shares.
   117  	fatalIf(doShareList(ctx.Args().First()).Trace(), "Unable to list previously shared URLs.")
   118  	return nil
   119  }