github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/cmd/roachprod/install/iterm2.go (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package install
    12  
    13  import (
    14  	"fmt"
    15  	"os"
    16  	"os/exec"
    17  	"strings"
    18  	"syscall"
    19  )
    20  
    21  const splitScript = `
    22   if (path to frontmost application as text) does not contain "iTerm" then
    23          return "Can't multi-ssh without iTerm 2 as the active terminal."
    24   end if
    25   tell application "iTerm2"
    26          activate
    27          tell (create window with default profile)
    28              set pane_count to %d
    29              set col_count to round ((pane_count) ^ 0.5) rounding up
    30              set row_count to round ((pane_count) / col_count) rounding up
    31              repeat row_count - 1 times
    32                  tell current session
    33                      split horizontally with default profile
    34                  end tell
    35              end repeat
    36              set cur to row_count
    37              repeat row_count times
    38                  repeat col_count - 1 times
    39                      if cur is equal to (pane_count) then
    40                          exit repeat
    41                      end if
    42                      tell current session
    43                          split vertically with default profile
    44                      end tell
    45                      set cur to cur + 1
    46                  end repeat
    47                  tell application "System Events" to tell process "iTerm2" to click menu item "Select Pane Below" of menu 1 of menu item "Select Split Pane" of menu 1 of menu bar item "Window" of menu bar 1
    48              end repeat
    49              tell current tab
    50                  set node_list to {%s}
    51                  repeat with i from 1 to pane_count
    52                      tell item i of sessions to write text "roachprod ssh %s:" & item i of node_list
    53                  end repeat
    54              end tell
    55          end tell
    56      end tell
    57  return`
    58  
    59  // maybeSplitScreenSSHITerm2 sshs to all of the specified nodes in the
    60  // SyncedCluster in an iTerm2 split-screen configuration if possible. It
    61  // returns true in the first position if it went through with the split-screen
    62  // ssh.
    63  func maybeSplitScreenSSHITerm2(c *SyncedCluster) (bool, error) {
    64  	osascriptPath, err := exec.LookPath("osascript")
    65  	if err != nil {
    66  		return false, err
    67  	}
    68  	nodeStrings := make([]string, len(c.Nodes))
    69  	for i, nodeID := range c.Nodes {
    70  		nodeStrings[i] = fmt.Sprint(nodeID)
    71  	}
    72  	script := fmt.Sprintf(splitScript, len(c.Nodes), strings.Join(nodeStrings, ","), c.Name)
    73  	allArgs := []string{
    74  		"osascript",
    75  		"-e",
    76  		script,
    77  	}
    78  	return true, syscall.Exec(osascriptPath, allArgs, os.Environ())
    79  }