j7

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2022 License: MIT Imports: 8 Imported by: 0

README

j7

Build Lint

Shell scripting in Go.

Installation

go get github.com/mgenware/j7

Examples

Checking if a command is installed and performing an install command if necessary. (assuming macOS with homebrew installed)

package main

import (
	"fmt"
	"os/exec"

	"github.com/mgenware/j7"
	"github.com/mgenware/j7/loggers"
)

func main() {
	t := j7.NewTunnel(j7.NewLocalNode(), loggers.NewConsoleLogger())

	_, err := exec.LookPath("tree")
	if err != nil {
		t.Logger().Log(j7.LogLevelError, "tree is not installed")
		t.Run("brew install tree")
	}
	fmt.Println("tree is installed")
	t.Run("tree .")
}

Example 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 Example
package main

import (
	"github.com/mgenware/j7"
	"github.com/mgenware/j7/loggers"
)

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

	t := j7.NewTunnel(j7.NewSSHNode(config), loggers.NewConsoleLogger())
	t.Run("pwd")
	t.Run("ls")
}

Sample output:

🚗 pwd
/root

🚗 ls
bin
build
data

Windows Support

Use WSL 2.

Documentation

Index

Constants

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

Variables

This section is empty.

Functions

func NewDefaultKeyBasedAuth

func NewDefaultKeyBasedAuth() []ssh.AuthMethod

func NewKeyBasedAuth

func NewKeyBasedAuth(keyFile string) []ssh.AuthMethod

func NewPwdBasedAuth

func NewPwdBasedAuth(pwd string) []ssh.AuthMethod

func SafeNewKeyBasedAuth

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

func SafeNewPwdBasedAuth

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

Types

type LocalNode

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

LocalNode is used for running commands locally.

func NewLocalNode

func NewLocalNode() *LocalNode

func (*LocalNode) RunOrError

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

type Logger

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

type Node

type Node interface {
	RunOrError(cmd string) ([]byte, error)
}

type SSHConfig

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

type SSHNode

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

func NewSSHNode

func NewSSHNode(config *SSHConfig) *SSHNode

func SafeNewSSHNode

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

func (*SSHNode) RunOrError

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

type Tunnel

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

func NewTunnel

func NewTunnel(node Node, logger Logger) *Tunnel

func (*Tunnel) Logger

func (w *Tunnel) Logger() Logger

func (*Tunnel) Node

func (w *Tunnel) Node() Node

func (*Tunnel) Run

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

func (*Tunnel) RunOrError

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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