j9

package module
v3.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 26, 2026 License: BSD-3-Clause Imports: 11 Imported by: 3

README

j9

Build Lint

Shell scripting, SSH in Go.

Installation

go get github.com/mgenware/j9/v3

Examples

Check if tree command is available and install it if necessary. (Assuming on macOS with homebrew installed):

package main

import (
	"fmt"
	"os/exec"

	"github.com/mgenware/j9/v3"
)

func main() {
	// Local node runs on your local system.
	// `ConsoleLogger` prints logs to the current console.
	t := j9.NewTunnel(j9.NewLocalNode(), j9.NewConsoleLogger())

	// Check if the command `tree` is installed.
	_, err := exec.LookPath("tree")
	if err != nil {
		t.Logger().Log(j9.LogLevelError, "tree is not installed")
		t.Spawn(&j9.SpawnOpt{Name: "brew", Args: []string{"install", "tree"}})
	}
	fmt.Println("tree is installed")
	t.Spawn(&j9.SpawnOpt{Name: "tree", Args: []string{"."}})
}

Sample output when tree is not installed:

❯ go run main.go
tree is not installed
brew install tree
==> Downloading https://homebrew.bintray.com/bottles/tree-1.7.0.high_sierra.bottle.1.tar.gz
==> Pouring tree-1.7.0.high_sierra.bottle.1.tar.gz
🍺  /usr/local/Cellar/tree/1.7.0: 8 files, 114.3KB
tree is installed
tree .
.
└── main.go

0 directories, 1 file
SSH with a private key
package main

import (
	"github.com/mgenware/j9/v3"
)

func main() {
	config := &j9.SSHConfig{
		Host: "1.2.3.4",
		User: "root",
		Auth: j9.MustCreateKeyBasedAuth("~/key.pem"),
	}

	t := j9.NewTunnel(j9.MustCreateSSHNode(config), j9.NewConsoleLogger())
	t.Shell("pwd")
	t.Shell("ls")
}

Sample output:

pwd
/root

ls
bin
build
data

Windows Support

Use WSL 2.

Spawn vs Shell

Use them based on your use cases.

Spawn Shell
Return stdout and stderr as a string
Live process output (good for long-running processes)
Supported in LocalNode
Supported in SSHNode

Documentation

Index

Constants

View Source
const (
	LogLevelError   = iota
	LogLevelWarning = iota
	LogLevelInfo    = iota
	LogLevelSuccess = iota
	LogLevelVerbose = iota
)

Variables

This section is empty.

Functions

func MustCreateDefaultKeyBasedAuth

func MustCreateDefaultKeyBasedAuth() []ssh.AuthMethod

Creates a new ssh.AuthMethod with the default key file("~/.ssh/id_rsa").

func MustCreateKeyBasedAuth

func MustCreateKeyBasedAuth(keyFile string) []ssh.AuthMethod

Creates a new ssh.AuthMethod with the given key file, panics if there is an error.

func MustCreateNewPwdBasedAuth

func MustCreateNewPwdBasedAuth(pwd string) []ssh.AuthMethod

Creates a new ssh.AuthMethod with the given password, panics if there is an error.

func NewKeyBasedAuth

func NewKeyBasedAuth(keyFile string) ([]ssh.AuthMethod, error)

Creates a new ssh.AuthMethod with the given key file.

func NewPwdBasedAuth

func NewPwdBasedAuth(pwd string) ([]ssh.AuthMethod, error)

Creates a new ssh.AuthMethod with the given password.

Types

type ConsoleLogger

type ConsoleLogger struct {
}

func NewConsoleLogger

func NewConsoleLogger() *ConsoleLogger

func (*ConsoleLogger) Log

func (c *ConsoleLogger) Log(level int, message string)

type LocalNode

type LocalNode struct {
}

LocalNode is used for running commands locally.

func NewLocalNode

func NewLocalNode() *LocalNode

func (*LocalNode) Shell

func (node *LocalNode) Shell(params *ShellOpt) (string, error)

func (*LocalNode) Spawn

func (node *LocalNode) Spawn(params *SpawnOpt) error

type Logger

type Logger interface {
	Log(level int, message string)
}

type Node

type Node interface {
	Spawn(params *SpawnOpt) error
	Shell(params *ShellOpt) (string, error)
}

Node is an interface for running commands in a specific environment.

type SSHConfig

type SSHConfig struct {
	Host string
	User string
	Port int
	Auth []ssh.AuthMethod
}

SSHConfig is the configuration for SSHNode.

type SSHNode

type SSHNode struct {
	Logger Logger
	// contains filtered or unexported fields
}

SSHNode is a node that runs commands on a remote SSH server.

func MustCreateSSHNode

func MustCreateSSHNode(config *SSHConfig) *SSHNode

Creates a new SSH node with the given configuration, panics if there is an error.

func NewSSHNode

func NewSSHNode(config *SSHConfig) (*SSHNode, error)

Creates a new SSH node with the given configuration.

func (*SSHNode) Shell

func (node *SSHNode) Shell(params *ShellOpt) (string, error)

func (*SSHNode) Spawn

func (node *SSHNode) Spawn(params *SpawnOpt) error

type ShellOpt added in v3.2.0

type ShellOpt struct {
	WorkingDir string
	Cmd        string
	Env        []string
}

type SpawnOpt added in v3.2.0

type SpawnOpt struct {
	WorkingDir string
	Name       string
	Args       []string
	Env        []string
}

func (*SpawnOpt) String added in v3.4.0

func (s *SpawnOpt) String() string

type Tunnel

type Tunnel struct {
	// contains filtered or unexported fields
}

Tunnel is a wrapper for a node that provides a logger.

func NewTunnel

func NewTunnel(node Node, logger Logger) *Tunnel

NewTunnel creates a new tunnel with the given node and logger.

func (*Tunnel) CD

func (w *Tunnel) CD(dir string)

func (*Tunnel) Dir added in v3.1.0

func (w *Tunnel) Dir() string

func (*Tunnel) Logger

func (w *Tunnel) Logger() Logger

Logger returns the logger.

func (*Tunnel) Node

func (w *Tunnel) Node() Node

Node returns the node.

func (*Tunnel) Shell

func (w *Tunnel) Shell(params *ShellOpt) string

func (*Tunnel) ShellRaw

func (w *Tunnel) ShellRaw(params *ShellOpt) (string, error)

func (*Tunnel) Spawn

func (w *Tunnel) Spawn(params *SpawnOpt)

func (*Tunnel) SpawnRaw

func (w *Tunnel) SpawnRaw(params *SpawnOpt) error

Directories

Path Synopsis
example
brewInstallTree command
cd command
echoAndPing command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL