关闭

(go) 上传文件elipse专用

704人阅读 评论(0) 收藏 举报
分类:
package main
import (
    "bufio"
    "io"
    "log"
    "os"
    "os/exec"
    "strings"
    "time"
)
const (
    filename = "c:\\myetc\\remote.etc"
    dotfilename = "c:\\myetc\\.filename"
)
func main() {
    var servicefilepath string
    var serviceIP string
    var passwd string
    var serviceusername string
    var port string
    var spath string
    var sfilename string
    var splitstrings []string
    //打开之前应该判断文件是否存在
    _, err := os.Stat(filename)
    if err != nil {
        log.Println("文件不存在!")
        log.Println("设置的样例:c:\\myetc\\remote.etc")
        log.Println("m_ta/m_ta@192.168.224.128:22:/home/m_ta/src")
        return
    }
    f, err := os.Open(filename) //打开文件
    //打开文件出错处理
    defer func() {
        f.Close()
    }()
    if nil == err {
        buff := bufio.NewReader(f) //读入缓存
        for {
            line, err := buff.ReadString('\n') //以'\n'为结束符读入一行
            if err != nil || io.EOF == err {
                break
            }
            //如果是#开头的则认为是注释符
            strings.Trim(line, " ")
            if ok := strings.Contains(line, "#"); ok {
                continue
            }
            //去回车换行符
            //样例 m_ta/m_ta@192.168.224.128:22:/home/m_ta/src
            splitstrings = strings.FieldsFunc(strings.Replace(line, "\r\n", "", -1), func(char rune) bool {
                switch char {
                case ':', '/', '@':
                    return true
                }
                return false
            })
            passwd, serviceusername, serviceIP, port = splitstrings[0], splitstrings[1], splitstrings[2], splitstrings[3]
        }
        f.Close()
        f, err := os.Open(dotfilename) //打开文件
        //打开文件出错处理
        defer func() {
            f.Close()
        }()
        if nil == err {
            buff := bufio.NewReader(f) //读入缓存
            line, err := buff.ReadString('\n') //以'\n'为结束符读入一行
            if err != nil || io.EOF == err {
                return
            }
            //DOS下文件名要用两个反斜杠
            linetmp := strings.Replace(line, "\\", "\\\\", -1)
            //去了换行符
            linetmp = strings.Replace(line, "\n", "", -1)
            //取文件名
            line = strings.Replace(line, "\\", "/", -1)
            tmpsplitstrings := strings.SplitN(line, "/", strings.Count(line, "/")+1)
            sfilename = tmpsplitstrings[strings.Count(line, "/")]
            //取路径 把文件名替换了
            tmpsplitstrings = strings.Split(line, "src")
            spath = strings.Replace(tmpsplitstrings[1], sfilename, "", -1)
            servicefilepath = "/" + strings.Join(splitstrings[4:], "/") + spath
            parameter := serviceusername + "@" + serviceIP + ":" + servicefilepath
            log.Println(linetmp, "--->", parameter)
            cmd := exec.Command("pscp.exe", "-P", port, "-pw", passwd, linetmp, parameter)
            err = cmd.Run()
            if err != nil {
                log.Print(err)
            } else {
                log.Print("OK!")
            }
        }
        time.Sleep(1 * time.Second)
    }
}

   
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:434649次
    • 积分:3959
    • 等级:
    • 排名:第8392名
    • 原创:93篇
    • 转载:17篇
    • 译文:9篇
    • 评论:37条
    最新评论