j9

package module
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2023 License: BSD-3-Clause Imports: 9 Imported by: 2

README

j9

Build Lint

Shell scripting in Go.

Installation

go get github.com/mgenware/j9/v2

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/v2"
)

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.Run("brew", "install", "tree")
	}
	fmt.Println("tree is installed")
	t.Run("tree", ".")
}

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/v2"
)

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.RunSync("pwd")
	t.RunSync("ls")
}

Sample output:

pwd
/root

ls
bin
build
data

Windows Support

Use WSL 2.

Run vs RunSync

First, they have different function signatures:

// Run runs the given command, returns an error if the command fails.
Run(name string, arg ...string) error

// RunSync runs the given command, returns the output and an error if the command fails.
RunSync(cmd string) ([]byte, error)

Use them based on your use cases.

Run RunSync
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
	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) RunCmd added in v2.1.0

func (node *LocalNode) RunCmd(wd string, name string, arg ...string) error

func (*LocalNode) RunCmdSync added in v2.1.0

func (node *LocalNode) RunCmdSync(wd string, cmd string) ([]byte, error)

type Logger

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

type Node

type Node interface {
	// RunCmd runs the given command, returns an error if the command fails.
	RunCmd(wd string, name string, arg ...string) error

	// RunCmdSync runs the given command, returns the output and an error if the command fails.
	RunCmdSync(wd string, cmd string) ([]byte, 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) RunCmd added in v2.1.0

func (node *SSHNode) RunCmd(wd string, name string, arg ...string) error

func (*SSHNode) RunCmdSync added in v2.1.0

func (node *SSHNode) RunCmdSync(wd string, cmd string) ([]byte, error)

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) CDRaw added in v2.1.0

func (w *Tunnel) CDRaw(dir string) error

func (*Tunnel) LastDir added in v2.1.0

func (w *Tunnel) LastDir() 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) Run

func (w *Tunnel) Run(name string, arg ...string)

Calls RunRaw and panics if there is an error.

func (*Tunnel) RunRaw added in v2.1.0

func (w *Tunnel) RunRaw(name string, arg ...string) error

Calls `node.RunCmd`.

func (*Tunnel) RunSync

func (w *Tunnel) RunSync(cmd string) []byte

Calls RunSyncRaw and panics if there is an error.

func (*Tunnel) RunSyncRaw added in v2.1.0

func (w *Tunnel) RunSyncRaw(cmd string) ([]byte, error)

Calls `node.RunCmdSync`.

Directories

Path Synopsis
example
cd

Jump to

Keyboard shortcuts

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