Skip to content

Commit

Permalink
Dedup TopoFileLoading (#2226)
Browse files Browse the repository at this point in the history
* Dedup TopoFileLoading

* fix
  • Loading branch information
steiler authored Oct 10, 2024
1 parent 81a6474 commit 99a6b5d
Showing 1 changed file with 8 additions and 23 deletions.
31 changes: 8 additions & 23 deletions cmd/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/srl-labs/containerlab/links"
"github.com/srl-labs/containerlab/runtime"
"github.com/srl-labs/containerlab/types"
"gopkg.in/yaml.v2"
)

var (
Expand Down Expand Up @@ -185,11 +184,6 @@ func listContainers(ctx context.Context, topo string) ([]runtime.GenericContaine
clab.WithTimeout(timeout),
}

c, err := clab.NewContainerLab(opts...)
if err != nil {
return nil, err
}

// filter to list all containerlab containers
// it is overwritten if topo file is provided
filter := []*types.GenericFilter{{
Expand All @@ -200,29 +194,20 @@ func listContainers(ctx context.Context, topo string) ([]runtime.GenericContaine

// when topo file is provided, filter containers by lab name
if topo != "" {
topo, err = c.ProcessTopoPath(topo)
if err != nil {
return nil, err
}

// read topo yaml file to get the lab name
topo, err := os.ReadFile(topo)
if err != nil {
return nil, err
}

config := &clab.Config{}
opts = append(opts, clab.WithTopoPath(topo, varsFile))
}

err = yaml.Unmarshal(topo, config)
if err != nil {
return nil, fmt.Errorf("%w, failed to parse topology file", err)
}
c, err := clab.NewContainerLab(opts...)
if err != nil {
return nil, err
}

if topo != "" {
filter = []*types.GenericFilter{{
FilterType: "label",
Field: labels.Containerlab,
Operator: "=",
Match: config.Name,
Match: c.Config.Name,
}}
}

Expand Down

0 comments on commit 99a6b5d

Please sign in to comment.