-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring the cli and adding docs cmd
- Loading branch information
1 parent
641d48c
commit c716017
Showing
11 changed files
with
156 additions
and
88 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package builder | ||
|
||
import ( | ||
"log" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package builder | ||
|
||
import ( | ||
"strconv" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:])) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters