Skip to content

Commit

Permalink
Refactoring the cli and adding docs cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrebodin committed Jul 7, 2018
1 parent 641d48c commit c716017
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 88 deletions.
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@
[[constraint]]
name = "github.com/alecthomas/kingpin"
version = "2.2.6"

[[constraint]]
branch = "master"
name = "github.com/pkg/browser"
2 changes: 1 addition & 1 deletion dir.go → builder/dir.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package builder

import (
"log"
Expand Down
2 changes: 1 addition & 1 deletion pane.go → builder/pane.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package builder

import (
"strconv"
Expand Down
39 changes: 31 additions & 8 deletions session.go → builder/session.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package builder

import (
"errors"
Expand All @@ -8,24 +8,31 @@ import (
"syscall"

"github.com/alexandrebodin/tmuxctl/config"
"github.com/alexandrebodin/tmuxctl/term"
"github.com/alexandrebodin/tmuxctl/tmux"
)

type session struct {
// Session struct represents a tmux session
type Session struct {
TmuxOptions *tmux.Options
Name string
Dir string
Windows []*window
ClearPanes bool
WindowScripts []string
SelectWindow string
SelectPane int
}

func newSession(config config.Session, options *tmux.Options) *session {
sess := &session{
// NewSession create a tmux session instance
func NewSession(config config.Session, options *tmux.Options) *Session {
sess := &Session{
Name: config.Name,
Dir: lookupDir(config.Dir),
ClearPanes: config.ClearPanes,
WindowScripts: config.WindowScripts,
SelectWindow: config.SelectWindow,
SelectPane: config.SelectPane,
TmuxOptions: options,
}

Expand All @@ -37,9 +44,10 @@ func newSession(config config.Session, options *tmux.Options) *session {
return sess
}

func (sess *session) start() error {
// Start starts a tmux sessions
func (sess *Session) Start() error {
// get term size
width, height, err := getTermSize()
width, height, err := term.GetDimensions()
if err != nil {
return err
}
Expand Down Expand Up @@ -86,10 +94,25 @@ func (sess *session) start() error {
}
}

if sess.SelectWindow != "" {
w, err := sess.selectWindow(sess.SelectWindow)
if err != nil {
return err
}

if sess.SelectPane != 0 {
_, err := w.selectPane(sess.SelectPane)
if err != nil {
return err
}
}
}

return nil
}

func (sess *session) attach() error {
// Attach attaches the process to the tmux session
func (sess *Session) Attach() error {
tmux, err := exec.LookPath("tmux")
if err != nil {
return fmt.Errorf("looking up tmux: %v", err)
Expand All @@ -103,7 +126,7 @@ func (sess *session) attach() error {
return nil
}

func (sess *session) selectWindow(name string) (*window, error) {
func (sess *Session) selectWindow(name string) (*window, error) {
for _, w := range sess.Windows {
if w.Name == name {
return w, w.selectWindow()
Expand Down
6 changes: 3 additions & 3 deletions window.go → builder/window.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package builder

import (
"fmt"
Expand All @@ -9,7 +9,7 @@ import (
)

type window struct {
Sess *session
Sess *Session
Name string
Dir string
Layout string
Expand All @@ -20,7 +20,7 @@ type window struct {
Target string
}

func newWindow(sess *session, config config.Window) *window {
func newWindow(sess *Session, config config.Window) *window {
win := &window{
Sess: sess,
Name: config.Name,
Expand Down
17 changes: 17 additions & 0 deletions cli/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cli

import (
"github.com/alecthomas/kingpin"
"github.com/pkg/browser"
)

var url = "https://tmuxctl.netlify.com"

func init() {
var docs = rootCmd.Command("docs", "Open documentation website")

docs.Action(func(ctx *kingpin.ParseContext) error {
browser.OpenURL(url)
return nil
})
}
18 changes: 18 additions & 0 deletions cli/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cli

import (
"fmt"
"os"

"github.com/alecthomas/kingpin"
)

var rootCmd = kingpin.New("tmuxctl", "")

// Run run the tmuxctl cli
func Run(version string) {
rootCmd.Version(fmt.Sprintf("tmuxct %s", version)).Author("Alexandre BODIN")
rootCmd.HelpFlag.Short('h')
rootCmd.VersionFlag.Short('v')
kingpin.MustParse(rootCmd.Parse(os.Args[1:]))
}
68 changes: 68 additions & 0 deletions cli/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cli

import (
"fmt"
"log"
"os"

"github.com/alecthomas/kingpin"
"github.com/alexandrebodin/go-findup"
"github.com/alexandrebodin/tmuxctl/builder"
"github.com/alexandrebodin/tmuxctl/config"
"github.com/alexandrebodin/tmuxctl/tmux"
)

func init() {
var start = rootCmd.Command("start", "Start a tmux instance").Default()
configPath := start.Arg("config", "Tmux config file").Default(".tmuxctlrc").String()

start.Action(func(ctx *kingpin.ParseContext) error {
fmt.Printf("Start tmux with config file: %v\n", *configPath)

filePath, err := findup.Find(*configPath)
if err != nil {
log.Fatalf("Error locating config file %v\n", err)
}

file, err := os.Open(filePath)
if err != nil {
log.Fatalf("Error openning config file %v\n", err)
}

conf, err := config.Parse(file)
if err != nil {
log.Fatalf("Error parsing %s: %v\n", filePath, err)
}

runningSessions, err := tmux.ListSessions()
if err != nil {
log.Fatalf("Error listing running sessions %v\n", err)
}

options, err := tmux.GetOptions()
if err != nil {
log.Fatalf("Error getting tmux options %v\n", err)
}

sess := builder.NewSession(conf, options)
if _, ok := runningSessions[sess.Name]; ok {
log.Fatalf("Session %s is already running\n", sess.Name)
}

checkError := func(err error) {
if err != nil {
log.Println(err)
// kill session if an error occurs after starting it
tmux.Exec("kill-session", "-t", sess.Name)
os.Exit(-1)
}
}

err = sess.Start()
checkError(err)

err = sess.Attach()
checkError(err)
return nil
})
}
75 changes: 3 additions & 72 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,82 +1,13 @@
package main

import (
"fmt"
"log"
"os"

"github.com/alecthomas/kingpin"
"github.com/alexandrebodin/go-findup"
"github.com/alexandrebodin/tmuxctl/config"
"github.com/alexandrebodin/tmuxctl/tmux"
"github.com/alexandrebodin/tmuxctl/cli"
)

var (
version = "development"
start = kingpin.Command("start", "start a tmux instance").Default()
configPath = start.Arg("config", "Tmux config file").Default(".tmuxctlrc").String()
version = "development"
)

func main() {
kingpin.Version(fmt.Sprintf("tmuxct %s", version)).Author("Alexandre BODIN")
kingpin.CommandLine.HelpFlag.Short('h')
kingpin.CommandLine.VersionFlag.Short('v')
kingpin.Parse()

fmt.Printf("Start tmux with config file: %v\n", *configPath)

filePath, err := findup.Find(*configPath)
if err != nil {
log.Fatalf("Error locating config file %v\n", err)
}

file, err := os.Open(filePath)
if err != nil {
log.Fatalf("Error openning config file %v\n", err)
}

conf, err := config.Parse(file)
if err != nil {
log.Fatalf("Error parsing %s: %v\n", filePath, err)
}

runningSessions, err := tmux.ListSessions()
if err != nil {
log.Fatalf("Error listing running sessions %v\n", err)
}

options, err := tmux.GetOptions()
if err != nil {
log.Fatalf("Error getting tmux options %v\n", err)
}

sess := newSession(conf, options)
if _, ok := runningSessions[sess.Name]; ok {
log.Fatalf("Session %s is already running\n", sess.Name)
}

checkError := func(err error) {
if err != nil {
log.Println(err)
// kill session if an error occurs after starting it
tmux.Exec("kill-session", "-t", sess.Name)
os.Exit(-1)
}
}

err = sess.start()
checkError(err)

if conf.SelectWindow != "" {
w, err := sess.selectWindow(conf.SelectWindow)
checkError(err)

if conf.SelectPane != 0 {
_, err := w.selectPane(conf.SelectPane)
checkError(err)
}
}

err = sess.attach()
checkError(err)
cli.Run(version)
}
5 changes: 3 additions & 2 deletions term.go → term/term.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package term

import (
"bytes"
Expand All @@ -8,7 +8,8 @@ import (
"strings"
)

func getTermSize() (string, string, error) {
// GetDimensions returns a term dimensions
func GetDimensions() (string, string, error) {
cmd := exec.Command("stty", "size")
var stderr bytes.Buffer
var stdout bytes.Buffer
Expand Down

0 comments on commit c716017

Please sign in to comment.