1
0
mirror of https://github.com/minio/mc.git synced 2025-11-12 01:02:26 +03:00
Files
mc/main.go
Anand Babu (AB) Periasamy e7a2699ab2 minio client becomes modern copy
2015-04-11 17:11:38 -07:00

117 lines
3.1 KiB
Go

/*
* Modern Copy, (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 (
"fmt"
"os"
"os/user"
"runtime"
"strconv"
"io/ioutil"
"github.com/cheggaaa/pb"
"github.com/minio-io/cli"
"github.com/minio-io/mc/pkg/console"
"github.com/minio-io/minio/pkg/iodine"
"github.com/minio-io/minio/pkg/utils/log"
)
func checkConfig() {
// Check for the environment early on and gracefuly report.
_, err := user.Current()
if err != nil {
log.Debug.Println(iodine.New(err, nil))
console.Fatalln("Unable to obtain user's home directory")
}
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))
console.Fatalf("Unable to read config file: %s\n", mustGetMcConfigPath())
}
err = checkMcConfig(config)
if err != nil {
log.Debug.Println(iodine.New(err, nil))
console.Fatalf("Error in config file: %s\n", mustGetMcConfigPath())
}
}
// Tries to get os/arch/platform specific information
// Returns a map of current os/arch/platform/memstats
func getSystemData() map[string]string {
host, err := os.Hostname()
if err != nil {
host = ""
}
memstats := &runtime.MemStats{}
runtime.ReadMemStats(memstats)
mem := fmt.Sprintf("Used: %s | Allocated: %s | Used-Heap: %s | Allocated-Heap: %s",
pb.FormatBytes(int64(memstats.Alloc)),
pb.FormatBytes(int64(memstats.TotalAlloc)),
pb.FormatBytes(int64(memstats.HeapAlloc)),
pb.FormatBytes(int64(memstats.HeapSys)))
platform := fmt.Sprintf("Host: %s | OS: %s | Arch: %s",
host,
runtime.GOOS,
runtime.GOARCH)
goruntime := fmt.Sprintf("Version: %s | CPUs: %s", runtime.Version(), strconv.Itoa(runtime.NumCPU()))
return map[string]string{
"PLATFORM": platform,
"RUNTIME": goruntime,
"MEM": mem,
}
}
func main() {
app := cli.NewApp()
app.Usage = "Modern Copy for object storage and filesystems"
app.Version = mcGitCommitHash
app.Commands = options
app.Flags = flags
app.Author = "Minio.io"
app.EnableBashCompletion = true
app.Before = func(ctx *cli.Context) error {
globalQuietFlag = ctx.GlobalBool("quiet")
globalDebugFlag = ctx.GlobalBool("debug")
if globalDebugFlag {
app.ExtraInfo = getSystemData()
} else {
log.Debug = log.New(ioutil.Discard, "", 0)
}
checkConfig()
return nil
}
app.After = func(ctx *cli.Context) error {
if !isMcConfigExist() && ctx.Command.Name != "config" {
console.Fatalln("Error: mc is not configured. Please run \"mc config\".")
}
return nil
}
app.RunAndExitOnError()
}