Files
mc/mb-main.go
T
Harshavardhana a769ce6439 mc: Vendorize to new API changes.
This vendorization uses the new set of API changes which
we are brought in.

With this new change Bucket regions are handled transparently
without any region lookup or local map. This makes overall code
seamless and easy to use.

Fixes - #1541 (cp corruption while uploading large files to s3)
Fixes - #1542 (cp checkCopySyntax failed)
Fixes - #1537 (regions will be handled seamelessly now for all buckets)
Fixes - #1538 (fixes access control policy for google cloud storage)
Fixes - #1547 (session resume completes without copying all the files)
Fixes - #1559 (poor network connectivity, causes timing out)
2016-01-23 23:53:27 -08:00

128 lines
3.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* Minio Client (C) 2014, 2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"encoding/json"
"github.com/fatih/color"
"github.com/minio/cli"
"github.com/minio/mc/pkg/console"
"github.com/minio/minio-xl/pkg/probe"
)
var (
mbFlags = []cli.Flag{
cli.BoolFlag{
Name: "help, h",
Usage: "Help of mb.",
},
cli.StringFlag{
Name: "region",
Value: "us-east-1",
Usage: "Specify bucket region. Defaults to ‘us-east-1’.",
},
}
)
// make a bucket or folder.
var mbCmd = cli.Command{
Name: "mb",
Usage: "Make a bucket or folder.",
Action: mainMakeBucket,
Flags: append(mbFlags, globalFlags...),
CustomHelpTemplate: `NAME:
mc {{.Name}} - {{.Usage}}
USAGE:
mc {{.Name}} [FLAGS] TARGET [TARGET...]
FLAGS:
{{range .Flags}}{{.}}
{{end}}
EXAMPLES:
1. Create a bucket on Amazon S3 cloud storage.
$ mc {{.Name}} s3/mynewbucket
2. Create a new bucket on Google Cloud Storage.
$ mc {{.Name}} gcs/miniocloud
4. Create a new bucket on Amazon S3 cloud storage in region ‘us-west-2’.
$ mc {{.Name}} --region=us-west-2 s3/myregionbucket
5. Create a new directory including its missing parents (equivalent to ‘mkdir -p’).
$ mc {{.Name}} /tmp/this/new/dir1
`,
}
// makeBucketMessage is container for make bucket success and failure messages.
type makeBucketMessage struct {
Status string `json:"status"`
Bucket string `json:"bucket"`
Region string `json:"region"`
}
// String colorized make bucket message.
func (s makeBucketMessage) String() string {
return console.Colorize("MakeBucket", "Bucket created successfully ‘"+s.Bucket+"’.")
}
// JSON jsonified make bucket message.
func (s makeBucketMessage) JSON() string {
makeBucketJSONBytes, err := json.Marshal(s)
fatalIf(probe.NewError(err), "Unable to marshal into JSON.")
return string(makeBucketJSONBytes)
}
// Validate command line arguments.
func checkMakeBucketSyntax(ctx *cli.Context) {
if !ctx.Args().Present() {
cli.ShowCommandHelpAndExit(ctx, "mb", 1) // last argument is exit code
}
if len(ctx.Args()) != 1 {
fatalIf(errInvalidArgument().Trace(ctx.Args()...), "Incorrect number of arguments for mb command.")
}
}
// mainMakeBucket is entry point for mb command.
func mainMakeBucket(ctx *cli.Context) {
// Set global flags from context.
setGlobalsFromContext(ctx)
// check 'mb' cli arguments.
checkMakeBucketSyntax(ctx)
// Additional command speific theme customization.
console.SetColor("MakeBucket", color.New(color.FgGreen, color.Bold))
// save region.
region := ctx.String("region")
targetURL := ctx.Args().First()
// Instantiate client for URL.
clnt, err := newClient(targetURL)
fatalIf(err.Trace(targetURL), "Invalid target ‘"+targetURL+"’.")
// Make bucket.
err = clnt.MakeBucket(region)
fatalIf(err.Trace(targetURL), "Unable to make bucket ‘"+targetURL+"’.")
// Successfully created a bucket.
printMsg(makeBucketMessage{Status: "success", Bucket: targetURL})
}