1
0
Fork 0

m6220-proxy: make cli iface into library

Change-Id: Ieededb08a930d7b862575cc569d467cdd93e3e0d
Reviewed-on: https://gerrit.hackerspace.pl/c/hscloud/+/1156
Reviewed-by: q3k <q3k@hackerspace.pl>
master
q3k 2021-10-07 18:46:14 +00:00 committed by q3k
parent 3943744814
commit 848db46bc0
2 changed files with 25 additions and 13 deletions

View File

@ -0,0 +1,13 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "cli",
srcs = ["cli.go"],
importpath = "code.hackerspace.pl/hscloud/dc/m6220-proxy/cli",
visibility = ["//visibility:public"],
deps = [
"@com_github_golang_glog//:go_default_library",
"@com_github_ziutek_telnet//:go_default_library",
"@org_golang_x_net//trace:go_default_library",
],
)

View File

@ -1,16 +1,15 @@
package main
package cli
import (
"context"
"fmt"
"strings"
"github.com/golang/glog"
"github.com/ziutek/telnet"
"golang.org/x/net/trace"
)
type cliClient struct {
type Client struct {
conn *telnet.Conn
username string
@ -20,15 +19,15 @@ type cliClient struct {
promptHostname string
}
func newCliClient(c *telnet.Conn, username, password string) *cliClient {
return &cliClient{
func NewClient(c *telnet.Conn, username, password string) *Client {
return &Client{
conn: c,
username: username,
password: password,
}
}
func (c *cliClient) readUntil(ctx context.Context, delims ...string) (string, error) {
func (c *Client) readUntil(ctx context.Context, delims ...string) (string, error) {
chStr := make(chan string, 1)
chErr := make(chan error, 1)
go func() {
@ -53,7 +52,7 @@ func (c *cliClient) readUntil(ctx context.Context, delims ...string) (string, er
}
}
func (c *cliClient) readString(ctx context.Context, delim byte) (string, error) {
func (c *Client) readString(ctx context.Context, delim byte) (string, error) {
chStr := make(chan string, 1)
chErr := make(chan error, 1)
go func() {
@ -78,7 +77,7 @@ func (c *cliClient) readString(ctx context.Context, delim byte) (string, error)
}
}
func (c *cliClient) writeLine(ctx context.Context, s string) error {
func (c *Client) writeLine(ctx context.Context, s string) error {
n, err := c.conn.Write([]byte(s + "\n"))
if got, want := n, len(s)+1; got != want {
err = fmt.Errorf("wrote %d bytes out of %d", got, want)
@ -91,17 +90,17 @@ func (c *cliClient) writeLine(ctx context.Context, s string) error {
return nil
}
func (c *cliClient) trace(ctx context.Context, f string, parts ...interface{}) {
func (c *Client) trace(ctx context.Context, f string, parts ...interface{}) {
tr, ok := trace.FromContext(ctx)
if !ok {
fmted := fmt.Sprintf(f, parts...)
glog.Infof("[no trace] %s", fmted)
//fmted := fmt.Sprintf(f, parts...)
//glog.Infof("[no trace] %s", fmted)
return
}
tr.LazyPrintf(f, parts...)
}
func (c *cliClient) logIn(ctx context.Context) error {
func (c *Client) logIn(ctx context.Context) error {
if c.loggedIn {
return nil
}
@ -185,7 +184,7 @@ func (c *cliClient) logIn(ctx context.Context) error {
return nil
}
func (c *cliClient) runCommand(ctx context.Context, command string) ([]string, string, error) {
func (c *Client) RunCommand(ctx context.Context, command string) ([]string, string, error) {
if err := c.logIn(ctx); err != nil {
return nil, "", fmt.Errorf("could not log in: %v", err)
}