From 9110c1c4396a413bcbdc6540616c7ed21766b542 Mon Sep 17 00:00:00 2001 From: "Johann Kellerman kellerza@gmail.com" Date: Wed, 9 Jun 2021 19:45:35 +0200 Subject: [PATCH] comments --- .pre-commit-config.yaml | 8 ++++++++ clab/config/helpers.go | 6 ++++-- clab/config/template.go | 4 ---- clab/config/transport/ssh.go | 14 +++++++------- clab/config/transport/sshkind.go | 11 ++++++----- clab/config/transport/transport.go | 1 + clab/config/utils.go | 8 ++++---- clab/config/utils_test.go | 1 - cmd/config.go | 4 ++-- 9 files changed, 32 insertions(+), 25 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ee38b8751..dbb6547be 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -3,3 +3,11 @@ repos: rev: v2.0.0 hooks: - id: codespell + - repo: https://github.com/syntaqx/git-hooks + rev: v0.0.17 + hooks: + - id: forbid-binary + - id: go-fmt + - id: go-test + - id: go-mod-tidy + - id: shfmt diff --git a/clab/config/helpers.go b/clab/config/helpers.go index c4c1d5bbc..318b346a8 100644 --- a/clab/config/helpers.go +++ b/clab/config/helpers.go @@ -4,7 +4,7 @@ import ( "strings" ) -// Split a string on commans and trim each +// Split a string on commas and trim each line func SplitTrim(s string) []string { res := strings.Split(s, ",") for i, v := range res { @@ -13,13 +13,15 @@ func SplitTrim(s string) []string { return res } -// the new agreed node config +// The new agreed node config type NodeSettings struct { Vars map[string]string Transport string Templates []string } +// Temporary function to extract NodeSettings from the Labels +// In the next phase node settings will be added to the clab file func GetNodeConfigFromLabels(labels map[string]string) NodeSettings { nc := NodeSettings{ Vars: labels, diff --git a/clab/config/template.go b/clab/config/template.go index 17f153d25..53fb95a3f 100644 --- a/clab/config/template.go +++ b/clab/config/template.go @@ -58,10 +58,6 @@ func RenderAll(nodes map[string]*types.Node, links map[int]*types.Link) (map[str func (c *NodeConfig) String() string { s := fmt.Sprintf("%s: %v", c.TargetNode.ShortName, c.Info) - // s := fmt.Sprintf("%s %s using %s/%s", c.TargetNode.ShortName, c.source, c.TargetNode.Kind, c.templateName) - // if c.Data != nil { - // s += fmt.Sprintf(" (%d lines)", bytes.Count(c.Data, []byte("\n"))+1) - // } return s } diff --git a/clab/config/transport/ssh.go b/clab/config/transport/ssh.go index a9d0f4595..4a46f683f 100644 --- a/clab/config/transport/ssh.go +++ b/clab/config/transport/ssh.go @@ -21,20 +21,20 @@ type SSHSession struct { type SSHTransportOption func(*SSHTransport) error -// The reply the execute command and the prompt. +// The SSH reply, executed command and the prompt type SSHReply struct{ result, prompt, command string } // SSHTransport setting needs to be set before calling Connect() // SSHTransport implements the Transport interface type SSHTransport struct { - // Channel used to read. Can use Expect to Write & read wit timeout + // Channel used to read. Can use Expect to Write & read with timeout in chan SSHReply // SSH Session ses *SSHSession // Contains the first read after connecting LoginMessage *SSHReply // SSH parameters used in connect - // defualt: 22 + // default: 22 Port int // Keep the target for logging @@ -64,8 +64,8 @@ func WithUserNamePassword(username, password string) SSHTransportOption { } } -// Add a basic username & password to a config. -// Will initilize the config if required +// Add a basic username & password to a config +// Will initialize the config if required func HostKeyCallback(callback ...ssh.HostKeyCallback) SSHTransportOption { return func(tx *SSHTransport) error { tx.SSHConfig.HostKeyCallback = func(hostname string, remote net.Addr, key ssh.PublicKey) error { @@ -106,7 +106,7 @@ func NewSSHTransport(node *types.Node, options ...SSHTransportOption) (*SSHTrans } return c, nil } - return nil, fmt.Errorf("no tranport implemented for kind: %s", node.Kind) + return nil, fmt.Errorf("no transport implemented for kind: %s", node.Kind) } // Creates the channel reading the SSH connection @@ -374,7 +374,7 @@ func (ses *SSHSession) Close() { // The LogString will include the entire SSHReply // Each field will be prefixed by a character. // # - command sent -// | - result recieved +// | - result received // ? - prompt part of the result func (r *SSHReply) LogString(node string, linefeed, debug bool) string { ind := 12 + len(node) diff --git a/clab/config/transport/sshkind.go b/clab/config/transport/sshkind.go index 8ed5e9544..2e65090b3 100644 --- a/clab/config/transport/sshkind.go +++ b/clab/config/transport/sshkind.go @@ -7,17 +7,18 @@ import ( log "github.com/sirupsen/logrus" ) -// an interface to implement kind specific methods for transactions and prompt checking +// An interface to implement kind specific methods for transactions and prompt checking type SSHKind interface { // Start a config transaction ConfigStart(s *SSHTransport, transaction bool) error // Commit a config transaction ConfigCommit(s *SSHTransport) (*SSHReply, error) - // Prompt parsing function. + // Prompt parsing function + // // This function receives string, split by the delimiter and should ensure this is a valid prompt - // Valid prompt, strip te prompt from the result and add it to the prompt in SSHReply + // Valid prompt, strip the prompt from the result and add it to the prompt in SSHReply // - // A defualt implementation is promptParseNoSpaces, which simply ensures there are + // A default implementation is promptParseNoSpaces, which simply ensures there are // no spaces between the start of the line and the # PromptParse(s *SSHTransport, in *string) *SSHReply } @@ -88,7 +89,7 @@ func (sk *SrlSSHKind) PromptParse(s *SSHTransport, in *string) *SSHReply { return promptParseNoSpaces(in, s.PromptChar, 2) } -// This is a helper funciton to parse the prompt, and can be used by SSHKind's ParsePrompt +// This is a helper function to parse the prompt, and can be used by SSHKind's ParsePrompt // Used in SRL today func promptParseNoSpaces(in *string, promptChar string, lines int) *SSHReply { n := strings.LastIndex(*in, "\n") diff --git a/clab/config/transport/transport.go b/clab/config/transport/transport.go index 5863803b5..ad3f83db9 100644 --- a/clab/config/transport/transport.go +++ b/clab/config/transport/transport.go @@ -17,6 +17,7 @@ type Transport interface { Close() } +// Write config to a node func Write(tx Transport, host string, data, info []string, options ...TransportOption) error { // the Kind should configure the transport parameters before diff --git a/clab/config/utils.go b/clab/config/utils.go index cb85f7c42..40cfa83d6 100644 --- a/clab/config/utils.go +++ b/clab/config/utils.go @@ -16,6 +16,7 @@ const ( type Dict map[string]interface{} +// Prepare variables for all nodes. This will also prepare all variables for the links func PrepareVars(nodes map[string]*types.Node, links map[int]*types.Link) map[string]Dict { res := make(map[string]Dict) @@ -51,6 +52,7 @@ func PrepareVars(nodes map[string]*types.Node, links map[int]*types.Link) map[st return res } +// Prepare variables for a specific link func prepareLinkVars(lIdx int, link *types.Link, varsA, varsB map[string]interface{}) error { ncA := GetNodeConfigFromLabels(link.A.Node.Labels) ncB := GetNodeConfigFromLabels(link.B.Node.Labels) @@ -110,9 +112,7 @@ func linkIPfromSystemIP(link *types.Link) (netaddr.IPPrefix, netaddr.IPPrefix, e return ipA, ipA, fmt.Errorf("invalid ip %s", link.A.EndpointName) } } else { - // caluculate link IP from the system IPs - tbd - //var sysA, sysB netaddr.IPPrefix - + // Calculate link IP from the system IPs sysA, err := netaddr.ParseIPPrefix(link.A.Node.Labels[systemIP]) if err != nil { return ipA, ipA, fmt.Errorf("no 'ip' on link & the '%s' of %s: %s", systemIP, link.A.Node.ShortName, err) @@ -141,7 +141,7 @@ func ipLastOctet(in netaddr.IP) int { } res, err := strconv.Atoi(s[i+1:]) if err != nil { - log.Errorf("last octect %s from IP %s not a string", s[i+1:], s) + log.Errorf("last octet %s from IP %s not a string", s[i+1:], s) } return res } diff --git a/clab/config/utils_test.go b/clab/config/utils_test.go index 177a45642..bfd275c31 100644 --- a/clab/config/utils_test.go +++ b/clab/config/utils_test.go @@ -43,7 +43,6 @@ func TestIPLastOctect(t *testing.T) { "10.0.0.1/32": 1, "::1/32": 1, } - for k, v := range lst { n := netaddr.MustParseIPPrefix(k) lo := ipLastOctet(n.IP()) diff --git a/cmd/config.go b/cmd/config.go index af6f01bdd..43c23ad3d 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -41,7 +41,7 @@ var configCmd = &cobra.Command{ return err } - // config map per node. each node gets a config.NodeConfig + // Config map per node. Each node gets a config.NodeConfig allConfig, err := config.RenderAll(c.Nodes, c.Links) if err != nil { return err @@ -79,7 +79,7 @@ var configCmd = &cobra.Command{ log.Errorf("%s: %s", kind, err) } } else if ct == "grpc" { - // newGRPCTransport + // NewGRPCTransport } else { log.Errorf("Unknown transport: %s", ct) return