gscp

package module
v0.0.0-...-a48ccf5 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2023 License: MIT Imports: 6 Imported by: 0

README

gscp

gscp is the ssh-config parser available in go.

gscp is named as an abbreviation of go ssh config parser.

Importing

import (
    "github.com/harakeishi/gscp"
)

Documentation

Visit the docs on GoDoc

usage

If no arguments are passed to LoadConfig(), ~/.ssh/config will be read.

package main

import (
	"fmt"
	"github.com/harakeishi/gscp"
)

func main() {
	// Reads a config file and gets it as a string
	s, _ := gscp.LoadConfig()
	// parse
	r, _ := gscp.Parse(s)
	fmt.Printf("%+v", r)
}
$ go run ./cmd/main.go
[{Name:testhost Options:[{Name:HostName Value:192.0.2.1} {Name:User Value:myuser} {Name:IdentityFile Value:~/.ssh/id_rsa} {Name:ServerAliveInterval Value:60}]}]

If you want to parse a config in a specific directory, pass the path as follows.

package main

import (
	"fmt"
	"github.com/harakeishi/gscp"
)

func main() {
	// Reads a config file and gets it as a string
	path := gscp.Path("./testData/test1_config")
	s, _ := gscp.LoadConfig(path)
	// parse
	fmt.Println(gscp.Parse(s))
}

Obtains information on the specified host and the specified options.

package main

import (
	"fmt"
	"github.com/harakeishi/gscp"
)

func main() {
	// Specify path
	path := gscp.Path("./testData/test1_config")
	// Load config.
	s, _ := gscp.LoadConfig(path)
	// Parse config.
	hosts, _ := gscp.Parse(s)
	// Obtain information on the specified host
	host := hosts.FindHost("testhost")
	fmt.Printf("%+v\n", host)
	// Obtain information on specified options
	hostname := host.FindOption("HostName").Value
	fmt.Printf("%+v\n", hostname)
}
PS C:\src\smp> go run .\cmd\main.go
{Name:testhost Options:[{Name:HostName Value:192.0.2.1} {Name:User Value:myuser} {Name:IdentityFile Value:~/.ssh/id_rsa} {Name:ServerAliveInterval Value:60}]}
192.0.2.1

License

Copyright (c) 2023 harakeishi

Licensed under MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadConfig

func LoadConfig(path ...option) (string, error)

Load ssh config. path is optional. If nothing is done, `~/.ssh.config` is read. To specify a path, pass the return value of Path() as an argument.

func Path

func Path(p string) option

Used to specify the path of the config file to be loaded in LoadConfig. Returns the setter function of the path of LoadOpts.

Types

type Host

type Host struct {
	Name    string
	Options []Option
}

func (Host) FindOption

func (h Host) FindOption(s string) Option

type Hosts

type Hosts []Host

func Parse

func Parse(s string) (Hosts, error)

Parses a config given as a string.

func (Hosts) FindHost

func (h Hosts) FindHost(s string) Host

type LoadOpts

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

type Option

type Option struct {
	Name  string
	Value string
}

Represents an ssh config option.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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