From 2d8b6f92f9b98c8aa5d7dc2aacb8c963146df42d Mon Sep 17 00:00:00 2001 From: networkop Date: Tue, 6 Jul 2021 18:21:11 +0100 Subject: [PATCH] refactored get short name function --- clab/clab.go | 2 +- clab/config.go | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/clab/clab.go b/clab/clab.go index 31278339a..02047d236 100644 --- a/clab/clab.go +++ b/clab/clab.go @@ -308,7 +308,7 @@ func (c *CLab) ListContainers(ctx context.Context, labels []*types.GenericFilter } func (c *CLab) GetNodeRuntime(query string) (runtime.ContainerRuntime, error) { - _, _, shortName, err := parseLongName(query) + shortName, err := getShortName(c.Config.Name, query) if err != nil { return nil, err } diff --git a/clab/config.go b/clab/config.go index 7956291f2..ab169c97b 100644 --- a/clab/config.go +++ b/clab/config.go @@ -609,10 +609,11 @@ func sysMemory(v string) uint64 { return m } -func parseLongName(input string) (string, string, string, error) { - result := strings.Split(input, "-") - if len(result) != 3 { - return "", "", "", fmt.Errorf("failed to parse container name %q", input) +// returns nodeCfg.ShortName based on the provided containerName and labName +func getShortName(labName, containerName string) (string, error) { + result := strings.Split(containerName, "-"+labName+"-") + if len(result) != 2 { + return "", fmt.Errorf("failed to parse container name %q", containerName) } - return result[0], result[1], result[2], nil + return result[1], nil }