Files
dummy/cmd/list.go
2022-09-11 15:31:16 +02:00

69 lines
1.6 KiB
Go

/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"context"
"dummy/util"
"fmt"
"os"
"github.com/minio/minio-go/v7"
"github.com/spf13/cobra"
)
// listCmd represents the list command
var listCmd = &cobra.Command{
Use: "list <bucket>",
Short: "List bucket contents",
Long: `This command lists the content of specified bucket`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
isps3 := util.S3{
Endpoint: "isp-dev.paasdev.sk:9000",
AccessKeyID: "LHHV6WR7EEMRS1RLX6BW",
SecretAccessKey: "m8PvnzpRB4ORYwcJtzeJpboFUIBAfbZ3wwh3Bkcp",
UseSSL: true,
}
err := isps3.Init()
if err != nil {
return err
}
ctx := context.Background()
found, err := isps3.Client.BucketExists(ctx, args[0])
if err != nil {
return err
}
if !found {
return fmt.Errorf("bucket %q does not exist", args[0])
}
objectCh := isps3.Client.ListObjects(ctx, args[0], minio.ListObjectsOptions{
Prefix: "",
Recursive: true,
})
for object := range objectCh {
if object.Err != nil {
return err
}
fmt.Fprintf(os.Stdout, "Name: %q, size: %v\n", object.Key, object.Size)
}
return nil
},
}
func init() {
rootCmd.AddCommand(listCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// listCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// listCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}