From 0a1ce197ac089cfb81a4af0a31937e09b8ec2236 Mon Sep 17 00:00:00 2001 From: tanlang Date: Thu, 23 Jun 2022 15:41:15 +0800 Subject: [PATCH] feat: add new subcommand addS3PieceStorageCmd --- cli/piece-storage.go | 72 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/cli/piece-storage.go b/cli/piece-storage.go index 2aaa25f4..626f40f1 100644 --- a/cli/piece-storage.go +++ b/cli/piece-storage.go @@ -13,6 +13,7 @@ var PieceStorageCmd = &cli.Command{ Description: "The piece storage will decide where to store pieces and how to store them", Subcommands: []*cli.Command{ addFsPieceStorageCmd, + addS3PieceStorageCmd, PieceStorageListCmd, PieceStorageRemoveCmd, }, @@ -59,6 +60,77 @@ var addFsPieceStorageCmd = &cli.Command{ }, } +var addS3PieceStorageCmd = &cli.Command{ + Name: "add-s3", + Usage: "add a object storage for piece storage", + Flags: []cli.Flag{ + // read only + &cli.BoolFlag{ + Name: "readonly", + Aliases: []string{"r"}, + Usage: "set true if you want the piece storage only fro reading", + DefaultText: "false", + }, + // Endpoint + &cli.StringFlag{ + Name: "endpoint", + Aliases: []string{"e"}, + Usage: "endpoint of the S3 bucket", + }, + // access key + &cli.StringFlag{ + Name: "access-key", + Aliases: []string{"a"}, + Usage: "access key of the S3 bucket", + }, + // secret key + &cli.StringFlag{ + Name: "secret-key", + Aliases: []string{"s"}, + Usage: "secret key of the S3 bucket", + }, + // token + &cli.StringFlag{ + Name: "token", + Aliases: []string{"t"}, + Usage: "token of the S3 bucket", + }, + }, + Action: func(cctx *cli.Context) error { + nodeApi, closer, err := NewMarketNode(cctx) + if err != nil { + return err + } + defer closer() + + ctx := ReqContext(cctx) + + readOnly := cctx.Bool("readonly") + endpoint := cctx.String("endpoint") + accessKey := cctx.String("access-key") + secretKey := cctx.String("secret-key") + token := cctx.String("token") + + if endpoint == "" { + return fmt.Errorf("endpoint are required") + } + if accessKey == "" { + return fmt.Errorf("access key are required") + } + if secretKey == "" { + return fmt.Errorf("secret key are required") + } + + err = nodeApi.AddS3PieceStorage(ctx, readOnly, endpoint, accessKey, secretKey, token) + if err != nil { + return err + } + fmt.Println("Adding S3 piece storage:", endpoint) + + return nil + }, +} + var PieceStorageListCmd = &cli.Command{ Name: "list", Usage: "list piece storages",