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

View File

@@ -38,7 +38,6 @@ type hostConfig struct {
type mcConfig struct {
Version uint
MCVersion string
DefaultHost string
Hosts map[string]hostConfig
Aliases map[string]string
}
@@ -74,6 +73,8 @@ func getMcConfigFilename() string {
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) {
if _config != nil {
return _config, nil
@@ -87,6 +88,17 @@ func getMcConfig() (cfg *mcConfig, err error) {
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
func checkMcConfig(config *mcConfig) (err error) {
// check for version
@@ -177,8 +189,7 @@ func saveConfig(ctx *cli.Context) error {
// Reload and cache new config
_, err = getMcConfig()
err = iodine.ToError(err)
if os.IsNotExist(err) {
if os.IsNotExist(iodine.ToError(err)) {
return iodine.New(err, nil)
}
@@ -228,7 +239,6 @@ func parseConfigInput(c *cli.Context) (config *mcConfig, err error) {
config = &mcConfig{
Version: currentConfigVersion,
MCVersion: "0.9",
DefaultHost: "https://s3.amazonaws.com",
Hosts: map[string]hostConfig{
"http*://s3*.amazonaws.com": {
Auth: auth{
@@ -237,8 +247,8 @@ func parseConfigInput(c *cli.Context) (config *mcConfig, err error) {
}},
},
Aliases: map[string]string{
"s3": "https://s3.amazonaws.com/",
"localhost": "http://localhost:9000/",
"s3": "https://s3.amazonaws.com",
"localhost": "http://localhost:9000",
},
}
return config, nil
@@ -253,7 +263,7 @@ func parseConfigInput(c *cli.Context) (config *mcConfig, err error) {
}
config = &mcConfig{
Version: currentConfigVersion,
DefaultHost: "https://s3.amazonaws.com",
MCVersion: "0.9",
Hosts: map[string]hostConfig{
"http*://s3*.amazonaws.com": {
Auth: auth{
@@ -262,8 +272,8 @@ func parseConfigInput(c *cli.Context) (config *mcConfig, err error) {
}},
},
Aliases: map[string]string{
"s3": "https://s3.amazonaws.com/",
"localhost": "http://localhost:9000/",
"s3": "https://s3.amazonaws.com",
"localhost": "http://localhost:9000",
aliasName: url,
},
}
@@ -301,24 +311,62 @@ func getHostConfig(hostURL string) (*hostConfig, error) {
return nil, iodine.New(errors.New("No matching host config found"), nil)
}
// doConfigCmd is the handler for "mc config" sub-command.
func doConfigCmd(ctx *cli.Context) {
switch true {
case ctx.Bool("completion") == true:
getBashCompletion()
default:
err := saveConfig(ctx)
if os.IsExist(err) {
//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))
msg := fmt.Sprintf("mc: Please rename your current configuration file [%s]\n", getMcConfigFilename())
}
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\n", getMcConfigFilename(), err)
}
msg := fmt.Sprintf("mc: Unable to generate config file [%s]. \nError: %v.", getMcConfigFilename(), err)
fatal(msg)
}
info("Configuration written to " + getMcConfigFilename() + "\n")
info("Configuration written to " + getMcConfigFilename() + ". Please update your access credentials.")
}
// doConfigCmd is the handler for "mc config" sub-command.
func doConfigCmd(ctx *cli.Context) {
switch true {
case ctx.Bool("completion") == true:
getBashCompletionCmd()
default:
saveConfigCmd(ctx)
}
}

View File

@@ -72,6 +72,7 @@ func doUpdateCmd(ctx *cli.Context) {
log.Debug.Println(iodine.New(err, nil))
fatal(err)
}
/* FIXME: Config file should not hold versin info (MCVersion). Use commit-id or build-date for the check */
config, err := getMcConfig()
if 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")
}
// Ensures config file is sane and cached to _config private variable.
config, err := getMcConfig()
err = iodine.ToError(err)
if os.IsNotExist(err) {
if !isMcConfigExist() {
// Handled properly in main.app.Before
return
}
// Ensures config file is sane and cached to _config private variable.
config, err := getMcConfig()
if err != nil {
log.Debug.Println(iodine.New(err, nil))
fatal("Unable to read config file")
@@ -91,9 +92,9 @@ func main() {
app.Flags = flags
app.Author = "Minio.io"
app.EnableBashCompletion = true
app.Before = func(c *cli.Context) error {
globalQuietFlag = c.GlobalBool("quiet")
globalDebugFlag = c.GlobalBool("debug")
app.Before = func(ctx *cli.Context) error {
globalQuietFlag = ctx.GlobalBool("quiet")
globalDebugFlag = ctx.GlobalBool("debug")
if globalDebugFlag {
app.ExtraInfo = getSystemData()
} else {
@@ -102,5 +103,13 @@ func main() {
checkConfig()
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()
}

View File

@@ -154,19 +154,8 @@ func url2Bucket(urlStr string) (bucketName string, err error) {
// parseURL extracts URL string from a single cmd-line argument
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
urlStr, err = aliasExpand(urlStr)
urlStr, err = aliasExpand(arg)
if err != nil {
return "", iodine.New(err, nil)
}