diff --git a/common.go b/common.go index 8e61ebf9..f7552412 100644 --- a/common.go +++ b/common.go @@ -15,69 +15,3 @@ */ package main - -import ( - "encoding/json" - "net/http" - "os" - "os/user" - "path" - - "github.com/minio-io/mc/pkg/s3" -) - -// Common authentication file -const ( - auth = "auth.json" - mcDir = "/.minio/mc/" -) - -func getAuthFilePath() (string, error) { - u, err := user.Current() - if err != nil { - return "", err - } - return path.Join(u.HomeDir+mcDir, auth), nil -} - -func getNewClient(auth *s3.Auth) (*s3.Client, error) { - return &s3.Client{ - Auth: auth, - Transport: http.DefaultTransport, - }, nil -} - -func getEnvironment() (auth *s3.Auth, err error) { - var s3Auth *os.File - var accessKey, secretKey, endpoint string - - authFile, err := getAuthFilePath() - if err != nil { - return nil, err - } - s3Auth, err = os.Open(authFile) - defer s3Auth.Close() - - if err != nil { - accessKey = os.Getenv("AWS_ACCESS_KEY_ID") - secretKey = os.Getenv("AWS_SECRET_ACCESS_KEY") - endpoint = os.Getenv("S3_ENDPOINT") - if endpoint == "" { - endpoint = "s3.amazonaws.com" - } - auth = &s3.Auth{ - AccessKey: accessKey, - SecretAccessKey: secretKey, - Endpoint: endpoint, - } - } else { - var n int - s3Authbytes := make([]byte, 256) - n, err = s3Auth.Read(s3Authbytes) - err = json.Unmarshal(s3Authbytes[:n], &auth) - if err != nil { - return nil, err - } - } - return auth, nil -} diff --git a/configure.go b/configure.go index 313ce88e..f7b0a795 100644 --- a/configure.go +++ b/configure.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "log" + "net/http" "os" "os/user" "path" @@ -12,66 +13,107 @@ import ( "github.com/minio-io/mc/pkg/s3" ) -func parseConfigureInput(c *cli.Context) (auth *s3.Auth, err error) { +const ( + mcConfigDir = ".minio/mc" + mcConfigFilename = "config.json" +) + +type s3Config struct { + Auth s3.Auth +} +type mcConfig struct { + Version string + S3 s3Config +} + +func getMcConfigDir() string { + u, err := user.Current() + if err != nil { + log.Fatalf("mc: Unable to obtain user's home directory. \nERROR[%v]\n", err) + } + + return path.Join(u.HomeDir + "/" + mcConfigDir) +} + +func getMcConfigFilename() string { + return path.Join(getMcConfigDir() + "/" + mcConfigFilename) +} + +func getMcConfig() (config *mcConfig, err error) { + configFile, err := os.Open(getMcConfigFilename()) + defer configFile.Close() + if err != nil { + return nil, err + } + + var n int + configBytes := make([]byte, 512) + n, err = configFile.Read(configBytes) + err = json.Unmarshal(configBytes[:n], &config) + if err != nil { + return nil, err + } + + if config.S3.Auth.Endpoint == "" { + config.S3.Auth.Endpoint = "s3.amazonaws.com" + } + + return config, nil +} + +func parseConfigureInput(c *cli.Context) (config *mcConfig, err error) { accessKey := c.String("accesskey") secretKey := c.String("secretkey") endpoint := c.String("endpoint") pathstyle := c.Bool("pathstyle") - if accessKey == "" { - return nil, errAccess - } - if secretKey == "" { - return nil, errSecret - } - if endpoint == "" { - return nil, errEndpoint + config = &mcConfig{ + Version: "0.1.0", + S3: s3Config{ + Auth: s3.Auth{ + AccessKey: accessKey, + SecretAccessKey: secretKey, + Endpoint: endpoint, + S3ForcePathStyle: pathstyle, + }, + }, } - auth = &s3.Auth{ - AccessKey: accessKey, - SecretAccessKey: secretKey, - Endpoint: endpoint, - S3ForcePathStyle: pathstyle, - } - - return auth, nil + return config, nil } func doConfigure(c *cli.Context) { - var err error - var jAuth []byte - var auth *s3.Auth - auth, err = parseConfigureInput(c) + configData, err := parseConfigureInput(c) if err != nil { log.Fatal(err) } - jAuth, err = json.MarshalIndent(auth, "", " ") + jsonConfig, err := json.MarshalIndent(configData, "", "\t") if err != nil { log.Fatal(err) } - var s3File *os.File - u, _ := user.Current() - err = os.MkdirAll(path.Join(u.HomeDir, mcDir), 0755) + err = os.MkdirAll(getMcConfigDir(), 0755) if err != nil { log.Fatal(err) } - var authFile string - authFile, err = getAuthFilePath() + + configFile, err := os.OpenFile(getMcConfigFilename(), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) + defer configFile.Close() if err != nil { log.Fatal(err) } - s3File, err = os.OpenFile(authFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) - defer s3File.Close() + + _, err = configFile.Write(jsonConfig) if err != nil { log.Fatal(err) } - _, err = s3File.Write(jAuth) - if err != nil { - log.Fatal(err) - } - fmt.Println("") - fmt.Println("Configuration written to", authFile) + fmt.Println("\nConfiguration written to ", getMcConfigFilename()) +} + +func getNewClient(config *mcConfig) (*s3.Client, error) { + return &s3.Client{ + Auth: &config.S3.Auth, + Transport: http.DefaultTransport, + }, nil } diff --git a/main.go b/main.go index d02155d9..bbea2016 100644 --- a/main.go +++ b/main.go @@ -17,18 +17,35 @@ package main import ( + "log" "os" + "os/user" "github.com/codegangsta/cli" ) +func init() { + // Check for the environment early on and gracefuly report. + _, err := user.Current() + if err != nil { + log.Fatalf("mc: Unable to obtain user's home directory. \nERROR[%v]\n", err) + } +} + func main() { app := cli.NewApp() + app.Flags = []cli.Flag{ + cli.StringFlag{ + Name: "quiet, q", + Usage: "disable progress bar and other chatty console output", + }, + } + app.Name = "mc" - app.Usage = "unified command line interface for Minio and S3" + app.Usage = "Minio Client for S3 Compatible Object Storage" app.Version = "0.1.0" app.Commands = options - app.Author = "Minio Community" + app.Author = "Minio.io" app.EnableBashCompletion = true app.Run(os.Args) } diff --git a/s3-errors.go b/s3-errors.go index 6cc1d3ea..9bc413ef 100644 --- a/s3-errors.go +++ b/s3-errors.go @@ -22,11 +22,6 @@ import "errors" var errFspath = errors.New("Arguments missing or ") var errFskey = errors.New("Key is needed to get the file") -// configure -var errAccess = errors.New("accesskey is mandatory") -var errSecret = errors.New("secretkey is mandatory") -var errEndpoint = errors.New("endpoint is mandatory") - // common var errMissingaccess = errors.New("Partial credentials found in the env, missing : AWS_ACCESS_KEY_ID") var errMissingsecret = errors.New("Partial credentials found in the env, missing : AWS_SECRET_ACCESS_KEY") diff --git a/s3-fs-cp.go b/s3-fs-cp.go index 9bd7ee89..43a9ab79 100644 --- a/s3-fs-cp.go +++ b/s3-fs-cp.go @@ -28,7 +28,6 @@ import ( "github.com/cheggaaa/pb" "github.com/codegangsta/cli" - "github.com/minio-io/mc/pkg/s3" ) // TODO @@ -87,15 +86,11 @@ func startBar(size int64) *pb.ProgressBar { } func doFsCopy(c *cli.Context) { - var auth *s3.Auth - var s3c *s3.Client - var err error - var bodyFile *os.File - auth, err = getEnvironment() + mcConfig, err := getMcConfig() if err != nil { log.Fatal(err) } - s3c, err = getNewClient(auth) + s3c, err := getNewClient(mcConfig) if err != nil { log.Fatal(err) } @@ -115,7 +110,7 @@ func doFsCopy(c *cli.Context) { log.Fatal("Is a directory") } size := stat.Size() - bodyFile, err = os.Open(fsoptions.body) + bodyFile, err := os.Open(fsoptions.body) defer bodyFile.Close() if err != nil { log.Fatal(err) @@ -134,7 +129,7 @@ func doFsCopy(c *cli.Context) { if err != nil { log.Fatal(err) } - bodyFile, err = os.Create(fsoptions.body) + bodyFile, err := os.Create(fsoptions.body) defer bodyFile.Close() // start progress bar diff --git a/s3-fs-ls.go b/s3-fs-ls.go index f67ac1e8..688af2f8 100644 --- a/s3-fs-ls.go +++ b/s3-fs-ls.go @@ -91,19 +91,15 @@ func getBucketAndObject(p string) (bucket, object string) { } func doFsList(c *cli.Context) { - var err error - var auth *s3.Auth - var s3c *s3.Client var items []*s3.Item var prefixes []*s3.Prefix - auth, err = getEnvironment() + config, err := getMcConfig() if err != nil { log.Fatal(err) } - var buckets []*s3.Bucket - s3c, err = getNewClient(auth) + s3c, err := getNewClient(config) if err != nil { log.Fatal(err) } @@ -137,7 +133,7 @@ func doFsList(c *cli.Context) { } } default: - buckets, err = s3c.Buckets() + buckets, err := s3c.Buckets() if err != nil { log.Fatal(err) } diff --git a/s3-fs-mb.go b/s3-fs-mb.go index bdf3e191..24095742 100644 --- a/s3-fs-mb.go +++ b/s3-fs-mb.go @@ -34,13 +34,11 @@ func doFsMb(c *cli.Context) { } bucketName := c.Args().Get(0) var err error - var auth *s3.Auth - var s3c *s3.Client - auth, err = getEnvironment() + auth, err := getMcConfig() if err != nil { log.Fatal(err) } - s3c, err = getNewClient(auth) + s3c, err := getNewClient(auth) err = s3c.PutBucket(bucketName) if err != nil { log.Fatal(err) diff --git a/s3-fs-options.go b/s3-fs-options.go index e0d0c108..4167dc90 100644 --- a/s3-fs-options.go +++ b/s3-fs-options.go @@ -49,8 +49,9 @@ var sync = cli.Command{ } var configure = cli.Command{ - Name: "configure", - Usage: "", + Name: "configure", + ShortName: "config", + Usage: "Generate configuration \"" + getMcConfigFilename() + "\" file.", Description: `Configure minio client configuration data. If your config file does not exist (the default location is ~/.auth), it will be automatically created for you. Note that the configure command only writes diff --git a/s3-fs-sync.go b/s3-fs-sync.go index 3a00cbd6..68db40cc 100644 --- a/s3-fs-sync.go +++ b/s3-fs-sync.go @@ -66,14 +66,13 @@ func (w *walk) putWalk(p string, info os.FileInfo, err error) error { } func doFsSync(c *cli.Context) { - var auth *s3.Auth var s3c *s3.Client var err error - auth, err = getEnvironment() + config, err := getMcConfig() if err != nil { log.Fatal(err) } - s3c, err = getNewClient(auth) + s3c, err = getNewClient(config) if err != nil { log.Fatal(err) }