|
1 | 1 | package nautobot |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | | - "io" |
6 | | - "io/fs" |
7 | | - "net/http" |
8 | | - "os" |
9 | | - "path/filepath" |
10 | | - |
11 | | - nb "github.com/nautobot/go-nautobot/v2" |
12 | | - "go.yaml.in/yaml/v3" |
| 4 | + "fmt" |
| 5 | + "github.com/charmbracelet/log" |
| 6 | + "io" |
| 7 | + "io/fs" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + |
| 12 | + nb "github.com/nautobot/go-nautobot/v2" |
| 13 | + "go.yaml.in/yaml/v3" |
13 | 14 | ) |
14 | 15 |
|
15 | 16 | func ListYAMLFiles(dir string) ([]string, error) { |
16 | | - var files []string |
| 17 | + var files []string |
17 | 18 |
|
18 | | - err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { |
19 | | - if err != nil { |
20 | | - return fmt.Errorf("error accessing %s: %w", path, err) |
21 | | - } |
| 19 | + err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { |
| 20 | + if err != nil { |
| 21 | + return fmt.Errorf("error accessing %s: %w", path, err) |
| 22 | + } |
22 | 23 |
|
23 | | - if d.IsDir() { |
24 | | - return nil |
25 | | - } |
| 24 | + if d.IsDir() { |
| 25 | + return nil |
| 26 | + } |
26 | 27 |
|
27 | | - ext := filepath.Ext(path) |
28 | | - if ext == ".yaml" || ext == ".yml" { |
29 | | - files = append(files, path) |
30 | | - } |
31 | | - return nil |
32 | | - }) |
| 28 | + ext := filepath.Ext(path) |
| 29 | + if ext == ".yaml" || ext == ".yml" { |
| 30 | + files = append(files, path) |
| 31 | + } |
| 32 | + return nil |
| 33 | + }) |
33 | 34 |
|
34 | | - if err != nil { |
35 | | - return nil, err |
36 | | - } |
| 35 | + if err != nil { |
| 36 | + return nil, err |
| 37 | + } |
37 | 38 |
|
38 | | - return files, nil |
| 39 | + return files, nil |
39 | 40 | } |
40 | 41 |
|
41 | 42 | func ParseYAMLToStruct[T any](path string) (T, error) { |
42 | | - var result T |
| 43 | + var result T |
43 | 44 |
|
44 | | - data, err := os.ReadFile(path) |
45 | | - if err != nil { |
46 | | - return result, fmt.Errorf("failed to read file %s: %w", path, err) |
47 | | - } |
| 45 | + data, err := os.ReadFile(path) |
| 46 | + if err != nil { |
| 47 | + return result, fmt.Errorf("failed to read file %s: %w", path, err) |
| 48 | + } |
48 | 49 |
|
49 | | - if err := yaml.Unmarshal(data, &result); err != nil { |
50 | | - return result, fmt.Errorf("failed to parse YAML %s: %w", path, err) |
51 | | - } |
| 50 | + if err := yaml.Unmarshal(data, &result); err != nil { |
| 51 | + return result, fmt.Errorf("failed to parse YAML %s: %w", path, err) |
| 52 | + } |
52 | 53 |
|
53 | | - return result, nil |
| 54 | + return result, nil |
54 | 55 | } |
55 | 56 |
|
56 | 57 | func buildBulkWritableCableRequestStatus(uuid string) *nb.BulkWritableCableRequestStatus { |
57 | | - return &nb.BulkWritableCableRequestStatus{ |
58 | | - Id: &nb.BulkWritableCableRequestStatusId{ |
59 | | - String: nb.PtrString(uuid), |
60 | | - }, |
61 | | - } |
| 58 | + return &nb.BulkWritableCableRequestStatus{ |
| 59 | + Id: &nb.BulkWritableCableRequestStatusId{ |
| 60 | + String: nb.PtrString(uuid), |
| 61 | + }, |
| 62 | + } |
62 | 63 | } |
63 | 64 |
|
64 | 65 | func buildNullableBulkWritableCircuitRequestTenant(uuid string) nb.NullableBulkWritableCircuitRequestTenant { |
65 | | - return *nb.NewNullableBulkWritableCircuitRequestTenant(&nb.BulkWritableCircuitRequestTenant{ |
66 | | - Id: &nb.BulkWritableCableRequestStatusId{ |
67 | | - String: nb.PtrString(uuid), |
68 | | - }, |
69 | | - }) |
| 66 | + return *nb.NewNullableBulkWritableCircuitRequestTenant(&nb.BulkWritableCircuitRequestTenant{ |
| 67 | + Id: &nb.BulkWritableCableRequestStatusId{ |
| 68 | + String: nb.PtrString(uuid), |
| 69 | + }, |
| 70 | + }) |
70 | 71 | } |
71 | 72 |
|
72 | 73 | func buildNullableBulkWritableRackRequestRackGroup(uuid string) *nb.NullableBulkWritableRackRequestRackGroup { |
73 | | - return nb.NewNullableBulkWritableRackRequestRackGroup(&nb.BulkWritableRackRequestRackGroup{ |
74 | | - Id: &nb.BulkWritableCableRequestStatusId{ |
75 | | - String: nb.PtrString(uuid), |
76 | | - }, |
77 | | - }) |
| 74 | + return nb.NewNullableBulkWritableRackRequestRackGroup(&nb.BulkWritableRackRequestRackGroup{ |
| 75 | + Id: &nb.BulkWritableCableRequestStatusId{ |
| 76 | + String: nb.PtrString(uuid), |
| 77 | + }, |
| 78 | + }) |
78 | 79 | } |
79 | 80 |
|
80 | 81 | // readResponseBody safely reads and closes the response body. |
81 | 82 | // Returns the body content as a string. If resp is nil, returns empty string. |
82 | 83 | func readResponseBody(resp *http.Response) string { |
83 | | - if resp == nil || resp.Body == nil { |
84 | | - return "" |
85 | | - } |
86 | | - defer resp.Body.Close() |
87 | | - |
88 | | - bodyBytes, err := io.ReadAll(resp.Body) |
89 | | - if err != nil { |
90 | | - return fmt.Sprintf("failed to read response body: %v", err) |
91 | | - } |
92 | | - return string(bodyBytes) |
| 84 | + if resp == nil || resp.Body == nil { |
| 85 | + return "" |
| 86 | + } |
| 87 | + defer func(Body io.ReadCloser) { |
| 88 | + err := Body.Close() |
| 89 | + if err != nil { |
| 90 | + log.Info("failed to close response body", "error", err) |
| 91 | + } |
| 92 | + }(resp.Body) |
| 93 | + |
| 94 | + bodyBytes, err := io.ReadAll(resp.Body) |
| 95 | + if err != nil { |
| 96 | + return fmt.Sprintf("failed to read response body: %v", err) |
| 97 | + } |
| 98 | + return string(bodyBytes) |
93 | 99 | } |
0 commit comments