From 190638265ec7b600b2723097f1d5c7c5d39c8bec Mon Sep 17 00:00:00 2001 From: Peter Wang Date: Mon, 19 Mar 2018 14:48:43 +0800 Subject: [PATCH] Fix some minor styling issue. --- client/base.go | 31 +++++++++++++++++-------------- client/base_test.go | 10 +++++----- client/fc.go | 13 ++++++++----- client/iscsi.go | 23 +++++++++++++++-------- client/iscsi_test.go | 2 +- cmd/app.go | 20 +++++++++++++------- 6 files changed, 59 insertions(+), 40 deletions(-) diff --git a/client/base.go b/client/base.go index 03dabfe..749f0c5 100644 --- a/client/base.go +++ b/client/base.go @@ -30,8 +30,9 @@ import ( "github.com/sirupsen/logrus" ) -var log *logrus.Logger = logrus.New() +var log = logrus.New() +// VolumeFormat defines the `volume` output format var VolumeFormat = `Volume Information: Multipath: %s Single Paths: @@ -40,6 +41,7 @@ Multipath ID: %s WWN: %s ` +// HostInfoFormat defines the `info` command output var HostInfoFormat = ` Host Name(FQDN): %s iSCSI Qualified Name(IQN): %s @@ -51,7 +53,7 @@ Connected iSCSI Sessions: %s ` -// Enable the console log for client +// InitLog enables the console log for client func InitLog(debug bool) error { if debug { log = logrus.New() @@ -84,17 +86,16 @@ func HandleConnect(args ...string) error { var err error if len(args) <= 0 { log.Error("Target IP or wwn is required.") - err = fmt.Errorf("Target IP or wwn is required.") + err = fmt.Errorf("target IP or wwn is required") } else if len(args) == 1 { // User only supply the LUN ID, so did a wildcard scan for all connected targets - err = fmt.Errorf("Currently [lun id] is not supported.") - log.WithError(err).Error("Unsupported parameters.") - log.Error("%s", args) + err = fmt.Errorf("currently [lun id] is not supported") + log.WithError(err).Error("Unsupported parameters. {%s}", args) } else { target := args[0] // Make sure the last param is LUN ID. - if _, err = ValidateLunId(args[len(args)-1:]); err == nil { - if IsIpLike(target) { + if _, err = ValidateLunID(args[len(args)-1:]); err == nil { + if IsIPLike(target) { return HandleISCSIConnect(args...) } if IsFcLike(target) { @@ -114,10 +115,10 @@ func HandleDisconnect(args ...string) error { func HandleExtend(args ...string) error { var err error if len(args) <= 0 { - err = fmt.Errorf("Need device name or Target IP with LUN ID.") + err = fmt.Errorf("need device name or Target IP with LUN ID") } else if len(args) == 1 { // User only supplies the local device name - err = fmt.Errorf("Currently device name is not supported.") + err = fmt.Errorf("currently device name is not supported") } else { // User specify TargetIP with LUN ID err = HandleISCSIExtend(args...) @@ -126,6 +127,7 @@ func HandleExtend(args ...string) error { } +// HandleInfo displays the host information func HandleInfo(args ...string) error { hostInfo, err := connector.GetHostInfo() if err != nil { @@ -154,13 +156,14 @@ func BeautifyHostInfo(info connector.HostInfo) { fmt.Printf(HostInfoFormat, info.Hostname, info.Initiator, sWwns, sTargetWwns, sIscsiTargets) } -func ValidateLunId(lunIDs []string) ([]int, error) { +// ValidateLunID validates the LunIDs as integer +func ValidateLunID(lunIDs []string) ([]int, error) { var err error re, _ := regexp.Compile("\\d+") var ret []int for _, lun := range lunIDs { if re.MatchString(lun) == false { - err = fmt.Errorf("%s does not look like a LUN ID.", lun) + err = fmt.Errorf("%s does not look like a LUN ID", lun) break } i, _ := strconv.Atoi(lun) @@ -181,8 +184,8 @@ func IsLunLike(data string) bool { return true } -// IsIpLike tests if *data* is a ipv4 address. -func IsIpLike(data string) bool { +// IsIPLike tests if *data* is a ipv4 address. +func IsIPLike(data string) bool { // IPv4 match if m, _ := regexp.MatchString("^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$", data); !m { return false diff --git a/client/base_test.go b/client/base_test.go index 188b034..497a1e9 100644 --- a/client/base_test.go +++ b/client/base_test.go @@ -45,13 +45,13 @@ func TestHandleInfoFailed(t *testing.T) { assert.Error(t, err) } func TestValidateLunId_True(t *testing.T) { - lunids, err := ValidateLunId([]string{"12", "113"}) + lunids, err := ValidateLunID([]string{"12", "113"}) assert.Nil(t, err) assert.Len(t, lunids, 2) } func TestValidateLunId_False(t *testing.T) { - lunids, err := ValidateLunId([]string{"a", "b"}) + lunids, err := ValidateLunID([]string{"a", "b"}) assert.Error(t, err) assert.Len(t, lunids, 0) } @@ -67,15 +67,15 @@ func TestIsLunLike_True(t *testing.T) { } func TestIsIpLike_True(t *testing.T) { - r := IsIpLike("192.168.1.29") + r := IsIPLike("192.168.1.29") assert.True(t, r) } func TestIsIpLike_False(t *testing.T) { - r := IsIpLike("192.168.1.") + r := IsIPLike("192.168.1.") assert.False(t, r) - r = IsIpLike("129") + r = IsIPLike("129") assert.False(t, r) } diff --git a/client/fc.go b/client/fc.go index ba1ceb6..78eee1c 100644 --- a/client/fc.go +++ b/client/fc.go @@ -17,32 +17,34 @@ package client import ( "fmt" - "github.com/peter-wangxu/goock/connector" "strconv" + "github.com/peter-wangxu/goock/connector" ) var fcConnector = connector.NewFibreChannelConnector() +// SetFcConnector sets the connector for FC connection func SetFcConnector(fc connector.FibreChannelInterface) { fcConnector = fc } -func Convert2ConnectionProperty(wwns []string, lunId string) connector.ConnectionProperty { +// Convert2ConnectionProperty converts wwn and lunid pair into ConnnectionProperty +func Convert2ConnectionProperty(wwns []string, lunID string) connector.ConnectionProperty { var property connector.ConnectionProperty property.TargetWwns = wwns - property.TargetLun, _ = strconv.Atoi(lunId) + property.TargetLun, _ = strconv.Atoi(lunID) property.StorageProtocol = connector.FcProtocol return property } +// HandleFCConnect handles the connection of FC target func HandleFCConnect(args ...string) error { var err error if len(args) == 1 { // User only supply the LUN ID, so did a wildcard scan for all connected targets - err = fmt.Errorf("Currently [lun id] is not supported.") + err = fmt.Errorf("currently [LUN ID] is not supported") log.WithError(err).Error("Unsupported parameters.") - log.Error("%s", args) } else { targets := args[:len(args)-1] @@ -58,6 +60,7 @@ func HandleFCConnect(args ...string) error { return err } +// HandleFCExtend handle the request to extend the FC devices. func HandleFCExtend(args ...string) error { return nil } diff --git a/client/iscsi.go b/client/iscsi.go index ca10d02..4672057 100644 --- a/client/iscsi.go +++ b/client/iscsi.go @@ -25,10 +25,13 @@ import ( var iscsiConnector = connector.NewISCSIConnector() +// SetISCSIConnector sets the iSCSI connector +// This will help when doing mock testing func SetISCSIConnector(iscsi connector.ISCSIInterface) { iscsiConnector = iscsi } +// Session2ConnectionProperty converts a session to an ConnectionProperty func Session2ConnectionProperty(sessions []model.ISCSISession, lun int) connector.ConnectionProperty { conn := connector.ConnectionProperty{} var portals []string @@ -45,10 +48,11 @@ func Session2ConnectionProperty(sessions []model.ISCSISession, lun int) connecto return conn } +// HandleISCSIConnect connects the iSCSI target via iscsiadm func HandleISCSIConnect(args ...string) error { var err error if len(args) == 1 { - err = fmt.Errorf("Currently Target IP is not supported.") + err = fmt.Errorf("currently Target IP is not supported") log.Error("Target IP and LUN ID(s) is required.") //log.Info("LUN ID is not specified, will query all LUNs on target IP: %s", args[0]) //targetIP := args[0] @@ -62,7 +66,7 @@ func HandleISCSIConnect(args ...string) error { } else { log.Debugf("Trying to validate the target IP : %s, LUN ID: %s", args[0], args[1:]) var lunIDs []int - lunIDs, err = ValidateLunId(args[1:]) + lunIDs, err = ValidateLunID(args[1:]) if err == nil { targetIP := args[0] sessions := iscsiConnector.DiscoverPortal(targetIP) @@ -76,20 +80,20 @@ func HandleISCSIConnect(args ...string) error { return err } +// HandleISCSIDisconnect disconnects the volume devices from local host // Accessible format likes follows: // /dev/sdb // sdb // - func HandleISCSIDisconnect(args ...string) error { var err error if len(args) == 1 { // TODO Support the device name removal - err = fmt.Errorf("Currently device name is not supported.") + err = fmt.Errorf("currently device name is not supported") } else if len(args) >= 2 { targetIP := args[0] - lunIDs, err := ValidateLunId(args[1:]) + lunIDs, err := ValidateLunID(args[1:]) if err == nil { sessions := iscsiConnector.DiscoverPortal(targetIP) for _, lun := range lunIDs { @@ -105,11 +109,12 @@ func HandleISCSIDisconnect(args ...string) error { return err } +// HandleISCSIExtend extends the iscsi block device func HandleISCSIExtend(args ...string) error { - targetIp := args[0] - lunIDs, err := ValidateLunId(args[1:]) + targetIP := args[0] + lunIDs, err := ValidateLunID(args[1:]) - sessions := iscsiConnector.DiscoverPortal(targetIp) + sessions := iscsiConnector.DiscoverPortal(targetIP) if err == nil { for _, lun := range lunIDs { property := Session2ConnectionProperty(sessions, lun) @@ -119,12 +124,14 @@ func HandleISCSIExtend(args ...string) error { return err } +// FetchVolumeInfo fetches the volume information via iscsiConnector. func FetchVolumeInfo(sessions []model.ISCSISession, lun int) (connector.VolumeInfo, error) { connectionProperty := Session2ConnectionProperty(sessions, lun) return iscsiConnector.ConnectVolume(connectionProperty) } +// BeautifyVolumeInfo output the volume information to stdout. func BeautifyVolumeInfo(info connector.VolumeInfo) { beautifiedPaths := "" for _, path := range info.Paths { diff --git a/client/iscsi_test.go b/client/iscsi_test.go index e388088..ba8838e 100644 --- a/client/iscsi_test.go +++ b/client/iscsi_test.go @@ -17,7 +17,7 @@ func (fake *FakeISCSIConnector) GetHostInfo() (connector.HostInfo, error) { func (fake *FakeISCSIConnector) ConnectVolume(connectionProperty connector.ConnectionProperty) (connector.VolumeInfo, error) { if len(connectionProperty.TargetPortals) > 0 && connectionProperty.TargetPortals[0] == "10.244.244.244" { - return connector.VolumeInfo{}, fmt.Errorf("Failed to connect volume.") + return connector.VolumeInfo{}, fmt.Errorf("failed to connect volume") } return connector.VolumeInfo{}, nil } diff --git a/cmd/app.go b/cmd/app.go index 9205f6a..d887b71 100644 --- a/cmd/app.go +++ b/cmd/app.go @@ -22,25 +22,31 @@ import ( ) const ( + // CommandName sets the cli entry CommandName = "goock" + // Version is the string of cli version // Generated by http://patorjk.com/software/taag/#p=display&f=Slant&t=Goock // need to be updated along with version update. Version = ` - ______ __ ____ ___ ____ - / ____/___ ____ _____/ /__ / __ \ < // __ \ - / / __/ __ \/ __ \/ ___/ //_/ / / / / / // / / / -/ /_/ / /_/ / /_/ / /__/ ,< / /_/ / / // /_/ / -\____/\____/\____/\___/_/|_| \____(_)_(_)____/ - - v0.1.0 + ______ __ ____ ___ ___ + / ____/___ ____ _____/ /__ / __ \ < /|__ \ + / / __/ __ \/ __ \/ ___/ //_/ / / / / / / __/ / + / /_/ / /_/ / /_/ / /__/ ,< / /_/ / / / / __/ + \____/\____/\____/\___/_/|_| \____(_)_(_)____/ + + + v0.1.2 ` + // Usage specifies the simple usage Usage = `A easy-to-use block device management tool.` ) +// App is the super struct of urfave app type App struct { *cli.App } +// NewApp initiates the CLI entry. func NewApp() *App { app := cli.NewApp() app.Version = Version