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>
diff --git a/dc/m6220-proxy/cli/BUILD.bazel b/dc/m6220-proxy/cli/BUILD.bazel
new file mode 100644
index 0000000..7ba74a4
--- /dev/null
+++ b/dc/m6220-proxy/cli/BUILD.bazel
@@ -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",
+ ],
+)
diff --git a/dc/m6220-proxy/cli.go b/dc/m6220-proxy/cli/cli.go
similarity index 87%
rename from dc/m6220-proxy/cli.go
rename to dc/m6220-proxy/cli/cli.go
index b9642cf..2e89ca8 100644
--- a/dc/m6220-proxy/cli.go
+++ b/dc/m6220-proxy/cli/cli.go
@@ -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 @@
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) 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) 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 @@
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 @@
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)
}