1
0
mirror of https://github.com/minio/mc.git synced 2025-11-13 12:22:45 +03:00

config bug fixes and removed default-host support

This commit is contained in:
Anand Babu (AB) Periasamy
2015-04-09 02:39:33 -07:00
parent 0850aedd0c
commit 99240bfb2d
5 changed files with 99 additions and 56 deletions

View File

@@ -17,6 +17,7 @@
package main package main
import ( import (
"fmt"
"path" "path"
"time" "time"
@@ -58,12 +59,7 @@ func getTraceTransport() s3.RoundTripTrace {
// NewClient - get new client // NewClient - get new client
func getNewClient(debug bool, urlStr string) (clnt client.Client, err error) { func getNewClient(debug bool, urlStr string) (clnt client.Client, err error) {
config, err := getMcConfig() hostCfg, err := getHostConfig(urlStr)
if err != nil {
return nil, iodine.New(err, nil)
}
hostCfg, err := getHostConfig(config.DefaultHost)
if err != nil { if err != nil {
return nil, iodine.New(err, nil) return nil, iodine.New(err, nil)
} }
@@ -71,7 +67,7 @@ func getNewClient(debug bool, urlStr string) (clnt client.Client, err error) {
var auth s3.Auth var auth s3.Auth
auth.AccessKeyID = hostCfg.Auth.AccessKeyID auth.AccessKeyID = hostCfg.Auth.AccessKeyID
auth.SecretAccessKey = hostCfg.Auth.SecretAccessKey auth.SecretAccessKey = hostCfg.Auth.SecretAccessKey
fmt.Println(urlStr)
uType, err := getURLType(urlStr) uType, err := getURLType(urlStr)
if err != nil { if err != nil {
return nil, iodine.New(err, nil) return nil, iodine.New(err, nil)

View File

@@ -36,11 +36,10 @@ type hostConfig struct {
} }
type mcConfig struct { type mcConfig struct {
Version uint Version uint
MCVersion string MCVersion string
DefaultHost string Hosts map[string]hostConfig
Hosts map[string]hostConfig Aliases map[string]string
Aliases map[string]string
} }
const ( const (
@@ -74,6 +73,8 @@ func getMcConfigFilename() string {
return path.Join(getMcConfigDir(), configFile) return path.Join(getMcConfigDir(), configFile)
} }
// getMcConfig returns the config data from file. Subsequent calls are
// cached in a private global variable
func getMcConfig() (cfg *mcConfig, err error) { func getMcConfig() (cfg *mcConfig, err error) {
if _config != nil { if _config != nil {
return _config, nil return _config, nil
@@ -87,6 +88,17 @@ func getMcConfig() (cfg *mcConfig, err error) {
return _config, nil return _config, nil
} }
// getMcConfig returns the config data from file. Subsequent calls are
// cached in a private global variable
func isMcConfigExist() bool {
configFile := getMcConfigFilename()
_, err := os.Stat(configFile)
if err != nil {
return false
}
return true
}
// chechMcConfig checks for errors in config file // chechMcConfig checks for errors in config file
func checkMcConfig(config *mcConfig) (err error) { func checkMcConfig(config *mcConfig) (err error) {
// check for version // check for version
@@ -177,8 +189,7 @@ func saveConfig(ctx *cli.Context) error {
// Reload and cache new config // Reload and cache new config
_, err = getMcConfig() _, err = getMcConfig()
err = iodine.ToError(err) if os.IsNotExist(iodine.ToError(err)) {
if os.IsNotExist(err) {
return iodine.New(err, nil) return iodine.New(err, nil)
} }
@@ -226,9 +237,8 @@ func parseConfigInput(c *cli.Context) (config *mcConfig, err error) {
switch true { switch true {
case len(alias) == 0: case len(alias) == 0:
config = &mcConfig{ config = &mcConfig{
Version: currentConfigVersion, Version: currentConfigVersion,
MCVersion: "0.9", MCVersion: "0.9",
DefaultHost: "https://s3.amazonaws.com",
Hosts: map[string]hostConfig{ Hosts: map[string]hostConfig{
"http*://s3*.amazonaws.com": { "http*://s3*.amazonaws.com": {
Auth: auth{ Auth: auth{
@@ -237,8 +247,8 @@ func parseConfigInput(c *cli.Context) (config *mcConfig, err error) {
}}, }},
}, },
Aliases: map[string]string{ Aliases: map[string]string{
"s3": "https://s3.amazonaws.com/", "s3": "https://s3.amazonaws.com",
"localhost": "http://localhost:9000/", "localhost": "http://localhost:9000",
}, },
} }
return config, nil return config, nil
@@ -252,8 +262,8 @@ func parseConfigInput(c *cli.Context) (config *mcConfig, err error) {
return nil, iodine.New(errors.New("invalid url type only supports http{s}"), nil) return nil, iodine.New(errors.New("invalid url type only supports http{s}"), nil)
} }
config = &mcConfig{ config = &mcConfig{
Version: currentConfigVersion, Version: currentConfigVersion,
DefaultHost: "https://s3.amazonaws.com", MCVersion: "0.9",
Hosts: map[string]hostConfig{ Hosts: map[string]hostConfig{
"http*://s3*.amazonaws.com": { "http*://s3*.amazonaws.com": {
Auth: auth{ Auth: auth{
@@ -262,8 +272,8 @@ func parseConfigInput(c *cli.Context) (config *mcConfig, err error) {
}}, }},
}, },
Aliases: map[string]string{ Aliases: map[string]string{
"s3": "https://s3.amazonaws.com/", "s3": "https://s3.amazonaws.com",
"localhost": "http://localhost:9000/", "localhost": "http://localhost:9000",
aliasName: url, aliasName: url,
}, },
} }
@@ -301,24 +311,62 @@ func getHostConfig(hostURL string) (*hostConfig, error) {
return nil, iodine.New(errors.New("No matching host config found"), nil) return nil, iodine.New(errors.New("No matching host config found"), nil)
} }
//getBashCompletionCmd generates bash completion file.
func getBashCompletionCmd() {
var b bytes.Buffer
if os.Getenv("SHELL") != "/bin/bash" {
fatal("Unsupported shell for bash completion detected.. exiting")
}
b.WriteString(mcBashCompletion)
f := getMcBashCompletionFilename()
fl, err := os.OpenFile(f, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
defer fl.Close()
if err != nil {
if globalDebugFlag {
log.Debug.Println(iodine.New(err, nil))
}
fatal(err)
}
_, err = fl.Write(b.Bytes())
if err != nil {
if globalDebugFlag {
log.Debug.Println(iodine.New(err, nil))
}
fatal(err)
}
msg := "\nConfiguration written to " + f
msg = msg + "\n\n$ source ${HOME}/.mc/mc.bash_completion\n"
msg = msg + "$ echo 'source ${HOME}/.mc/mc.bash_completion' >> ${HOME}/.bashrc"
info(msg)
}
// saveConfigCmd writes config file to disk
func saveConfigCmd(ctx *cli.Context) {
err := saveConfig(ctx)
if os.IsExist(iodine.ToError(err)) {
if globalDebugFlag {
log.Debug.Println(iodine.New(err, nil))
}
msg := fmt.Sprintf("mc: Configuration file [%s] already exists.", getMcConfigFilename())
fatal(msg)
}
if err != nil {
if globalDebugFlag {
log.Debug.Println(iodine.New(err, nil))
}
msg := fmt.Sprintf("mc: Unable to generate config file [%s]. \nError: %v.", getMcConfigFilename(), err)
fatal(msg)
}
info("Configuration written to " + getMcConfigFilename() + ". Please update your access credentials.")
}
// doConfigCmd is the handler for "mc config" sub-command. // doConfigCmd is the handler for "mc config" sub-command.
func doConfigCmd(ctx *cli.Context) { func doConfigCmd(ctx *cli.Context) {
switch true { switch true {
case ctx.Bool("completion") == true: case ctx.Bool("completion") == true:
getBashCompletion() getBashCompletionCmd()
default: default:
err := saveConfig(ctx) saveConfigCmd(ctx)
if os.IsExist(err) {
log.Debug.Println(iodine.New(err, nil))
msg := fmt.Sprintf("mc: Please rename your current configuration file [%s]\n", getMcConfigFilename())
fatal(msg)
}
if err != nil {
log.Debug.Println(iodine.New(err, nil))
msg := fmt.Sprintf("mc: Unable to generate config file [%s]. \nError: %v\n", getMcConfigFilename(), err)
fatal(msg)
}
info("Configuration written to " + getMcConfigFilename() + "\n")
} }
} }

View File

@@ -72,6 +72,7 @@ func doUpdateCmd(ctx *cli.Context) {
log.Debug.Println(iodine.New(err, nil)) log.Debug.Println(iodine.New(err, nil))
fatal(err) fatal(err)
} }
/* FIXME: Config file should not hold versin info (MCVersion). Use commit-id or build-date for the check */
config, err := getMcConfig() config, err := getMcConfig()
if err != nil { if err != nil {
log.Debug.Println(iodine.New(err, nil)) log.Debug.Println(iodine.New(err, nil))

23
main.go
View File

@@ -39,12 +39,13 @@ func checkConfig() {
fatal("Unable to obtain user's home directory") fatal("Unable to obtain user's home directory")
} }
// Ensures config file is sane and cached to _config private variable. if !isMcConfigExist() {
config, err := getMcConfig() // Handled properly in main.app.Before
err = iodine.ToError(err)
if os.IsNotExist(err) {
return return
} }
// Ensures config file is sane and cached to _config private variable.
config, err := getMcConfig()
if err != nil { if err != nil {
log.Debug.Println(iodine.New(err, nil)) log.Debug.Println(iodine.New(err, nil))
fatal("Unable to read config file") fatal("Unable to read config file")
@@ -91,9 +92,9 @@ func main() {
app.Flags = flags app.Flags = flags
app.Author = "Minio.io" app.Author = "Minio.io"
app.EnableBashCompletion = true app.EnableBashCompletion = true
app.Before = func(c *cli.Context) error { app.Before = func(ctx *cli.Context) error {
globalQuietFlag = c.GlobalBool("quiet") globalQuietFlag = ctx.GlobalBool("quiet")
globalDebugFlag = c.GlobalBool("debug") globalDebugFlag = ctx.GlobalBool("debug")
if globalDebugFlag { if globalDebugFlag {
app.ExtraInfo = getSystemData() app.ExtraInfo = getSystemData()
} else { } else {
@@ -102,5 +103,13 @@ func main() {
checkConfig() checkConfig()
return nil return nil
} }
app.After = func(ctx *cli.Context) error {
if !isMcConfigExist() && ctx.Command.Name != "config" {
fatal("Error: mc is not configured. Please run \"mc config\".")
}
return nil
}
app.RunAndExitOnError() app.RunAndExitOnError()
} }

View File

@@ -154,19 +154,8 @@ func url2Bucket(urlStr string) (bucketName string, err error) {
// parseURL extracts URL string from a single cmd-line argument // parseURL extracts URL string from a single cmd-line argument
func parseURL(arg string) (urlStr string, err error) { func parseURL(arg string) (urlStr string, err error) {
urlStr = arg
// Use default host if no argument is passed
if urlStr == "" {
// Load config file
config, err := getMcConfig()
if err != nil {
return "", iodine.New(err, nil)
}
urlStr = config.DefaultHost
}
// Check and expand Alias // Check and expand Alias
urlStr, err = aliasExpand(urlStr) urlStr, err = aliasExpand(arg)
if err != nil { if err != nil {
return "", iodine.New(err, nil) return "", iodine.New(err, nil)
} }