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