https://github.com/zhiminwen/magetool
sshkit/rake in Golang
https://github.com/zhiminwen/magetool
mage rake sshkit
Last synced: about 2 months ago
JSON representation
sshkit/rake in Golang
- Host: GitHub
- URL: https://github.com/zhiminwen/magetool
- Owner: zhiminwen
- Created: 2018-10-23T06:00:10.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2025-05-11T13:00:19.000Z (10 months ago)
- Last Synced: 2025-05-11T13:19:29.402Z (10 months ago)
- Topics: mage, rake, sshkit
- Language: Go
- Homepage:
- Size: 566 KB
- Stars: 10
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- Changelog: changes.md
Awesome Lists containing this project
README
# A Capistrano/SSHKit Implementation with Golang
## Download and Import
```
go get github.com/zhiminwen/magetool
```
```
import (
"github.com/zhiminwen/magetool/shellkit"
"github.com/zhiminwen/magetool/sshkit"
)
```
## [Medium Story](https://medium.com/@zhimin.wen/implement-capistrano-sshkit-in-golang-32d8d094e65d)
## Synopsis
### Mage
Use [Mage](https://github.com/magefile/mage) to organize the tasks together
```golang
// +build mage
package main
import (
"os"
"github.com/zhiminwen/magetool/shellkit"
)
func init() {
os.Setenv("MAGEFILE_VERBOSE", "true")
}
//Run Cmd
func RunCmd() {
shellkit.Execute("cmd", "/c", "type test.log & sleep 10")
}
//dir
func Dir() {
shellkit.Execute("cmd", "/c", "time /t 1>&2 & exit 1")
}
```
### Shellkit
- Use Shellkit.Execute() to run local command.
- Use Shellkit.Capture to capture command output.
### SSHkit
Init the client
```golang
//Use password
client1 := sshkit.NewSSHClient("192.168.5.10", "22", "ubuntu", "password", "")
//Use private key
s2 := sshkit.NewSSHClient("192.168.5.10", "22", "ubuntu", "", "mykeyfile"
// Use a slice of the client
var servers []*sshkit.SSHClient
servers = append(servers, sshkit.NewSSHClient("192.168.5.10", "22", "ubuntu", "password", ""))
servers = append(servers, sshkit.NewSSHClient("192.168.5.10", "22", "ubuntu2", "password", ""))
```
Execute a command
```golang
// Run against a single client
client1.Execute("id; hostname")
//Run against slice of servers
sshkit.Execute(servers, "id; hostname")
```
Capture a result
``` golang
result, err := client.Capture("hostname")
```
```golang
sshkit.ExecuteFunc(servers, func(t *sshkit.SSHClient) {
id, err := t.Capture("id -u")
if err != nil {
log.Printf("Failed to get id:%v", err)
return
}
if id == "1000" {
return
}
cmd := fmt.Sprintf("echo %s; hostname", id)
t.Execute(cmd)
})
```
Upload and Download
```golang
client1.Upload("example.txt", "/tmp/example.txt")
client1.Download("/etc/hosts", "hosts.txt")
```
```
client1.Put(`This is a test content`, "mytest.txt")
list, err := servers[0].Get("/etc/passwd")
```
Interact with command
```golang
client1.Execute(`rm -rf .ssh/known_hosts`)
client1.ExecuteInteractively("scp ubuntu@ubuntu:/tmp/test.txt /tmp/test.txt.dupped", map[string]string{
`\(yes/no\)`: "yes",
"password": "password",
})
```
## Full example to build a docker image and push to dockerhub
[Source Code](sample/dockerBuild/dockerBuilder.go)