Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Only support xbar on darwin #10

Merged
merged 1 commit into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 150 additions & 0 deletions internal/xbar/render_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
//go:build darwin

package xbar

import (
"bufio"
"bytes"
"context"
"fmt"
"os"
"os/exec"
"path"
"sort"
"strings"
"syscall"
"time"

"github.com/jlegrone/tctx/config"
"github.com/jlegrone/xbargo"
"golang.org/x/sys/unix"
)

func Render(ctx context.Context, opts *Options) error {
activeContext := opts.Contexts[opts.ActiveContext]
// Avoid nil pointer exceptions when there is no active context
if activeContext == nil {
activeContext = &config.ClusterConfig{}
}

// Compute menu title based on user settings
var titleMeta []string
if activeContext.Address != "" {
if opts.ShowCluster {
titleMeta = append(titleMeta, opts.ActiveContext)
}
if opts.ShowNamespace {
titleMeta = append(titleMeta, activeContext.Namespace)
}
}

plugin := xbargo.NewPlugin().WithText(strings.Join(titleMeta, ":")).WithIcon(bytes.NewReader(temporalIcon))

activeContextStatus := xbargo.NewMenuItem(activeContext.Address).
WithStyle(xbargo.Style{MaxLength: 60}).
WithShortcut("o", xbargo.CommandKey)
if activeContext.WebAddress != "" {
activeContextStatus = activeContextStatus.WithHref(path.Join(activeContext.WebAddress, "namespaces", activeContext.Namespace))
}

// Get list of namespaces in active cluster
var namespaces []string
if activeContext.Address != "" {
combinedOutput, err := execContext(ctx, opts.TctxPath, "exec", "--", opts.TctlPath,
"--context_timeout", "1",
"namespace",
"list",
)
if err != nil {
activeContextStatus.Icon = bytes.NewReader(statusUnavailable)
// Let the user know if we can't find a binary in PATH
if errMessage := combinedOutput.String(); strings.Contains(errMessage, "not found in $PATH") {
panic(errMessage)
}
// Print error for debugging
_, _ = fmt.Fprintln(os.Stderr, combinedOutput)
} else {
activeContextStatus.Icon = bytes.NewReader(statusAvailable)
scanner := bufio.NewScanner(combinedOutput)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "Name: ") {
namespaces = append(namespaces, strings.TrimPrefix(line, "Name: "))
}
}
sort.Strings(namespaces)
}
} else {
activeContextStatus.Title = "No active context"
}

plugin = plugin.WithElements(activeContextStatus, xbargo.Separator{})

// Get sorted list of context names
var contextNames []string
for k := range opts.Contexts {
contextNames = append(contextNames, k)
}
sort.Strings(contextNames)

var clusterOptions []*xbargo.MenuItem
for i, k := range contextNames {
prefix := " "
if k == opts.ActiveContext {
prefix = "✓ "
}
clusterOptions = append(clusterOptions, xbargo.NewMenuItem(prefix+k).
WithShell(opts.TctxPath, "use", "-c", k).
WithShortcut(fmt.Sprintf("%d", i), xbargo.ControlKey).
WithRefresh(),
)
}
plugin = plugin.WithElements(xbargo.NewMenuItem("Clusters").WithSubMenu(clusterOptions...))

var namespaceOptions []*xbargo.MenuItem
var hasActiveNamespace bool
for i, ns := range namespaces {
prefix := " "
if ns == activeContext.Namespace {
prefix = "✓ "
hasActiveNamespace = true
}
namespaceOptions = append(namespaceOptions, xbargo.NewMenuItem(prefix+ns).
WithShell(opts.TctxPath, "use", "-c", opts.ActiveContext, "--ns", ns).
WithShortcut(fmt.Sprintf("%d", i), xbargo.ShiftKey).
WithRefresh(),
)
}
if !hasActiveNamespace && activeContext.Namespace != "" {
// The namespace currently set in tctx doesn't exist in the cluster
namespaceOptions = append(namespaceOptions, xbargo.NewMenuItem("✓ "+activeContext.Namespace).
WithStyle(xbargo.Style{Color: "red"}))

}
plugin = plugin.WithElements(xbargo.NewMenuItem("Namespaces").WithSubMenu(namespaceOptions...))

return plugin.RunW(os.Stdout)
}

func execContext(ctx context.Context, command string, args ...string) (*bytes.Buffer, error) {
cmd := exec.CommandContext(ctx, command, args...)

// tctx starts its own child process, so we need to create a new process
// group for cmd and manually send a kill signal to this process group id
// after timeout is reached.
//
// This doesn't work on Windows but that's fine because xbar is macOS only.
cmd.SysProcAttr = &unix.SysProcAttr{Setpgid: true}
deadline, ok := ctx.Deadline()
if ok {
time.AfterFunc(deadline.Sub(time.Now()), func() {
unix.Kill(-cmd.Process.Pid, syscall.SIGKILL)
})
}

b := bytes.NewBuffer(nil)
cmd.Stdout = b
cmd.Stderr = b

return b, cmd.Run()
}
12 changes: 12 additions & 0 deletions internal/xbar/render_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//go:build !darwin

package xbar

import (
"context"
"fmt"
)

func Render(ctx context.Context, opts *Options) error {
return fmt.Errorf("xbar not supported on current platform")
}
142 changes: 0 additions & 142 deletions internal/xbar/xbar.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
package xbar

import (
"bufio"
"bytes"
"context"
_ "embed"
"fmt"
"os"
"os/exec"
"path"
"sort"
"strings"
"syscall"
"time"

"github.com/jlegrone/xbargo"
"github.com/urfave/cli/v2"
"golang.org/x/sys/unix"

"github.com/jlegrone/tctx/config"
)
Expand Down Expand Up @@ -45,132 +32,3 @@ type Options struct {
TctxPath, TctlPath string
ShowCluster, ShowNamespace bool
}

func Render(ctx context.Context, opts *Options) error {
activeContext := opts.Contexts[opts.ActiveContext]
// Avoid nil pointer exceptions when there is no active context
if activeContext == nil {
activeContext = &config.ClusterConfig{}
}

// Compute menu title based on user settings
var titleMeta []string
if activeContext.Address != "" {
if opts.ShowCluster {
titleMeta = append(titleMeta, opts.ActiveContext)
}
if opts.ShowNamespace {
titleMeta = append(titleMeta, activeContext.Namespace)
}
}

plugin := xbargo.NewPlugin().WithText(strings.Join(titleMeta, ":")).WithIcon(bytes.NewReader(temporalIcon))

activeContextStatus := xbargo.NewMenuItem(activeContext.Address).
WithStyle(xbargo.Style{MaxLength: 60}).
WithShortcut("o", xbargo.CommandKey)
if activeContext.WebAddress != "" {
activeContextStatus = activeContextStatus.WithHref(path.Join(activeContext.WebAddress, "namespaces", activeContext.Namespace))
}

// Get list of namespaces in active cluster
var namespaces []string
if activeContext.Address != "" {
combinedOutput, err := execContext(ctx, opts.TctxPath, "exec", "--", opts.TctlPath,
"--context_timeout", "1",
"namespace",
"list",
)
if err != nil {
activeContextStatus.Icon = bytes.NewReader(statusUnavailable)
// Let the user know if we can't find a binary in PATH
if errMessage := combinedOutput.String(); strings.Contains(errMessage, "not found in $PATH") {
panic(errMessage)
}
// Print error for debugging
_, _ = fmt.Fprintln(os.Stderr, combinedOutput)
} else {
activeContextStatus.Icon = bytes.NewReader(statusAvailable)
scanner := bufio.NewScanner(combinedOutput)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "Name: ") {
namespaces = append(namespaces, strings.TrimPrefix(line, "Name: "))
}
}
sort.Strings(namespaces)
}
} else {
activeContextStatus.Title = "No active context"
}

plugin = plugin.WithElements(activeContextStatus, xbargo.Separator{})

// Get sorted list of context names
var contextNames []string
for k := range opts.Contexts {
contextNames = append(contextNames, k)
}
sort.Strings(contextNames)

var clusterOptions []*xbargo.MenuItem
for i, k := range contextNames {
prefix := " "
if k == opts.ActiveContext {
prefix = "✓ "
}
clusterOptions = append(clusterOptions, xbargo.NewMenuItem(prefix+k).
WithShell(opts.TctxPath, "use", "-c", k).
WithShortcut(fmt.Sprintf("%d", i), xbargo.ControlKey).
WithRefresh(),
)
}
plugin = plugin.WithElements(xbargo.NewMenuItem("Clusters").WithSubMenu(clusterOptions...))

var namespaceOptions []*xbargo.MenuItem
var hasActiveNamespace bool
for i, ns := range namespaces {
prefix := " "
if ns == activeContext.Namespace {
prefix = "✓ "
hasActiveNamespace = true
}
namespaceOptions = append(namespaceOptions, xbargo.NewMenuItem(prefix+ns).
WithShell(opts.TctxPath, "use", "-c", opts.ActiveContext, "--ns", ns).
WithShortcut(fmt.Sprintf("%d", i), xbargo.ShiftKey).
WithRefresh(),
)
}
if !hasActiveNamespace && activeContext.Namespace != "" {
// The namespace currently set in tctx doesn't exist in the cluster
namespaceOptions = append(namespaceOptions, xbargo.NewMenuItem("✓ "+activeContext.Namespace).
WithStyle(xbargo.Style{Color: "red"}))

}
plugin = plugin.WithElements(xbargo.NewMenuItem("Namespaces").WithSubMenu(namespaceOptions...))

return plugin.RunW(os.Stdout)
}

func execContext(ctx context.Context, command string, args ...string) (*bytes.Buffer, error) {
cmd := exec.CommandContext(ctx, command, args...)

// tctx starts its own child process, so we need to create a new process
// group for cmd and manually send a kill signal to this process group id
// after timeout is reached.
//
// This doesn't work on Windows but that's fine because xbar is macOS only.
cmd.SysProcAttr = &unix.SysProcAttr{Setpgid: true}
deadline, ok := ctx.Deadline()
if ok {
time.AfterFunc(deadline.Sub(time.Now()), func() {
unix.Kill(-cmd.Process.Pid, syscall.SIGKILL)
})
}

b := bytes.NewBuffer(nil)
cmd.Stdout = b
cmd.Stderr = b

return b, cmd.Run()
}