Skip to content

Commit

Permalink
use utils func to create topo file on generate
Browse files Browse the repository at this point in the history
  • Loading branch information
steiler committed May 2, 2023
1 parent 88ed50d commit dc33e4a
Showing 1 changed file with 3 additions and 17 deletions.
20 changes: 3 additions & 17 deletions cmd/generate.go
Expand Up @@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"net"
"os"
"strconv"
"strings"

Expand All @@ -17,6 +16,7 @@ import (
"github.com/srl-labs/containerlab/clab"
"github.com/srl-labs/containerlab/nodes"
"github.com/srl-labs/containerlab/types"
"github.com/srl-labs/containerlab/utils"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -82,7 +82,7 @@ var generateCmd = &cobra.Command{
}
log.Debugf("generated topo: %s", string(b))
if file != "" {
err = saveTopoFile(file, b)
err = utils.CreateFile(file, string(b))

Check warning on line 85 in cmd/generate.go

View check run for this annotation

Codecov / codecov/patch

cmd/generate.go#L85

Added line #L85 was not covered by tests
if err != nil {
return err
}
Expand All @@ -91,7 +91,7 @@ var generateCmd = &cobra.Command{
reconfigure = true
if file == "" {
file = fmt.Sprintf("%s.clab.yml", name)
err = saveTopoFile(file, b)
err = utils.CreateFile(file, string(b))

Check warning on line 94 in cmd/generate.go

View check run for this annotation

Codecov / codecov/patch

cmd/generate.go#L94

Added line #L94 was not covered by tests
if err != nil {
return err
}
Expand Down Expand Up @@ -306,17 +306,3 @@ func parseNodesFlag(kind string, nodes ...string) ([]nodesDef, error) {
}
return result, nil
}

func saveTopoFile(path string, data []byte) error {
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666) // skipcq: GSC-G302
if err != nil {
return err
}

_, err = f.Write(data)
if err != nil {
return err
}

return f.Close()
}

0 comments on commit dc33e4a

Please sign in to comment.