👷 drone ci + tests (#10)
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Closes #2 Reviewed-on: #10
This commit is contained in:
parent
b22583edca
commit
4c54f1d449
12
.drone.yml
Normal file
12
.drone.yml
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
---
|
||||||
|
kind: pipeline
|
||||||
|
type: docker
|
||||||
|
name: default
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: tests
|
||||||
|
image: golang:1.17
|
||||||
|
commands:
|
||||||
|
- apt-get update && apt-get install -y tmux
|
||||||
|
- go build -v
|
||||||
|
- go test -v ./...
|
@ -227,7 +227,10 @@ func getConfigFilePath(name string) string {
|
|||||||
func sessionExists(name string) bool {
|
func sessionExists(name string) bool {
|
||||||
t := Tmux{}
|
t := Tmux{}
|
||||||
|
|
||||||
sessions := t.ListSessions()
|
sessions, err := t.ListSessions()
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
for _, s := range sessions {
|
for _, s := range sessions {
|
||||||
if s == name {
|
if s == name {
|
||||||
return true
|
return true
|
||||||
|
34
tmux/tmux.go
34
tmux/tmux.go
@ -1,6 +1,7 @@
|
|||||||
package tmux
|
package tmux
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
@ -18,7 +19,10 @@ func CreateTmux(debug bool) *Tmux {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tmux) Exec(args ...string) ([]byte, error) {
|
func (t *Tmux) Exec(args ...string) ([]byte, error) {
|
||||||
bin := t.getBinary()
|
bin, err := t.getBinary()
|
||||||
|
if err != nil {
|
||||||
|
return []byte{}, err
|
||||||
|
}
|
||||||
if t.Debug {
|
if t.Debug {
|
||||||
fmt.Println(bin, strings.Join(args, " "))
|
fmt.Println(bin, strings.Join(args, " "))
|
||||||
}
|
}
|
||||||
@ -32,26 +36,32 @@ func (t *Tmux) Run(args ...string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tmux) Attach(name string) {
|
func (t *Tmux) Attach(name string) error {
|
||||||
args := []string{}
|
args := []string{}
|
||||||
args = append(args, "-u", "attach-session", "-t", name)
|
args = append(args, "-u", "attach-session", "-t", name)
|
||||||
|
|
||||||
err := syscall.Exec(t.getBinary(), args, os.Environ())
|
bin, err := t.getBinary()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error:", err)
|
return err
|
||||||
}
|
}
|
||||||
|
err = syscall.Exec(bin, args, os.Environ())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tmux) ListSessions() []string {
|
func (t *Tmux) ListSessions() ([]string, error) {
|
||||||
sessions := []string{}
|
sessions := []string{}
|
||||||
result, err := t.Exec("ls", "-F", "#{session_name}")
|
result, err := t.Exec("ls", "-F", "#{session_name}")
|
||||||
if err != nil {
|
if errors.Is(err, exec.ErrNotFound) {
|
||||||
// No active sessions returns as an error.
|
// No active sessions returns as an error.
|
||||||
return sessions
|
return sessions, err
|
||||||
}
|
}
|
||||||
|
|
||||||
lines := strings.Trim(string(result), "\n")
|
lines := strings.Trim(string(result), "\n")
|
||||||
return strings.Split(lines, "\n")
|
fmt.Println("Sessions", lines)
|
||||||
|
return strings.Split(lines, "\n"), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tmux) KillSession(name string) error {
|
func (t *Tmux) KillSession(name string) error {
|
||||||
@ -59,15 +69,15 @@ func (t *Tmux) KillSession(name string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tmux) getBinary() string {
|
func (t *Tmux) getBinary() (string, error) {
|
||||||
if t.BinPath != "" {
|
if t.BinPath != "" {
|
||||||
return t.BinPath
|
return t.BinPath, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
tmux, err := exec.LookPath("tmux")
|
tmux, err := exec.LookPath("tmux")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error:", err)
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return tmux
|
return tmux, nil
|
||||||
}
|
}
|
||||||
|
12
tmux/tmux_test.go
Normal file
12
tmux/tmux_test.go
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package tmux
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestListSessions(t *testing.T) {
|
||||||
|
m := CreateTmux(true)
|
||||||
|
if _, err := m.ListSessions(); err != nil {
|
||||||
|
t.Fatalf("ListSessions: %s", err)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user