1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- package main
- import (
- "fmt"
- "os"
- "os/exec"
- "path/filepath"
- "strings"
- )
- func main(){
- // 获取当前路径
- path := GetCurrentDirectory()
- //if len(os.Args) < 2 {
- // fmt.Println("参数输入错误:例 : " )
- // return
- //}
- paraMap := make(map[string]string, 0)
- paraMap["-h"] = "localhost"
- paraMap["-port"] = "3306"
- paraMap["-u"] = "root"
- paraMap["-p"] = "root123456"
- paraMap["-path"] = path+"/1.sql"
- paraMap["-database"] = "test"
- for i := 1; i < len(os.Args); i++ {
- if len(os.Args) > i+1 {
- paraMap[strings.ToLower(os.Args[i])] = strings.Trim(strings.ToLower(os.Args[i+1]), " ")
- } else {
- paraMap[strings.ToLower(os.Args[i])] = ""
- }
- }
- out, err := Exec_Shell(paraMap)
- if err != nil{
- fmt.Println(string(out))
- fmt.Println(err.Error())
- }
- }
- func GetCurrentDirectory() string {
- //返回绝对路径 filepath.Dir(os.Args[0])去除最后一个元素的路径
- dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
- if err != nil {
- os.Exit(0)
- }
- //将\替换成/
- return strings.Replace(dir, "\\", "/", -1)
- }
- func Exec_Shell(paraMap map[string]string) (string, error) {
- command := fmt.Sprintf("mysql -P %s -h %s -u%s -p%s %s < %s",paraMap["-port"],paraMap["-h"],paraMap["-u"],paraMap["-p"],paraMap["-database"],paraMap["-path"])
- fmt.Println(command)
- cmd := exec.Command("/bin/sh", "-c", command)
- out, err := cmd.CombinedOutput()
- if err != nil{
- fmt.Println(string(out))
- fmt.Println(err.Error())
- }
- return string(out), err
- }
|