Skip to content

Commit 9eeb6da

Browse files
authored
Fix JSON unmarshaling for empty file arrays and convert directory names to lowercase
1 parent 77dff75 commit 9eeb6da

File tree

3 files changed

+78
-15
lines changed

3 files changed

+78
-15
lines changed

cmd/init.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ func sanitizeDirectoryName(name string) string {
154154
// Ensure the name doesn't have any remaining problematic characters
155155
sanitized := replacer.Replace(name)
156156

157+
// Convert to lowercase
158+
sanitized = strings.ToLower(sanitized)
159+
157160
// If the name is empty after sanitization, use a default name
158161
if sanitized == "" {
159162
return "lab"

internal/mtcapi/lab.go

Lines changed: 75 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package mtcapi
22

33
import (
4+
"encoding/json"
45
"fmt"
56

67
"github.com/morethancertified/mtc-cli/internal/types"
@@ -24,43 +25,102 @@ func (c *MtcApiClient) GetLabInfo(userLessonID string) (types.LabInfo, error) {
2425

2526
// GetLabFiles fetches all files for a lab (public, bootstrap, and other)
2627
func (c *MtcApiClient) GetLabFiles(userLessonID string) ([]types.LabFile, error) {
27-
res, err := c.httpClient.R().
28-
SetResult(&types.LabFiles{}).
28+
// Make a single request and print the raw response for debugging
29+
rawRes, err := c.httpClient.R().
2930
Get("/labs/" + userLessonID + "/files")
3031
if err != nil {
3132
return nil, err
3233
}
3334

34-
if res.IsError() {
35-
return nil, fmt.Errorf("API error: %s", res.String())
35+
if rawRes.IsError() {
36+
return nil, fmt.Errorf("API error: %s", rawRes.String())
3637
}
3738

38-
result := res.Result().(*types.LabFiles)
39-
var files []types.LabFile
39+
// No need for debug printing in production code
40+
41+
// Check for empty files response: {"files":[]}
42+
if string(rawRes.Body()) == "{\"files\":[]}" {
43+
return []types.LabFile{}, nil
44+
}
45+
46+
// Try to unmarshal as a direct array of LabFile
47+
var directFiles []types.LabFile
48+
err = json.Unmarshal(rawRes.Body(), &directFiles)
49+
if err == nil {
50+
// If this succeeds, return the files directly
51+
return directFiles, nil
52+
}
53+
54+
// Try to unmarshal as a simple wrapper with files array
55+
var simpleFiles struct {
56+
Files []types.LabFile `json:"files"`
57+
}
58+
err = json.Unmarshal(rawRes.Body(), &simpleFiles)
59+
if err == nil && simpleFiles.Files != nil {
60+
return simpleFiles.Files, nil
61+
}
62+
63+
// If simple unmarshaling fails, try the full structured approach
64+
var labFiles types.LabFiles
65+
err = json.Unmarshal(rawRes.Body(), &labFiles)
66+
if err != nil {
67+
return nil, fmt.Errorf("failed to parse response: %v", err)
68+
}
4069

4170
// Combine all file types
42-
files = append(files, result.Files.Public...)
43-
files = append(files, result.Files.Bootstrap...)
44-
files = append(files, result.Files.Other...)
71+
var files []types.LabFile
72+
files = append(files, labFiles.Files.Public...)
73+
files = append(files, labFiles.Files.Bootstrap...)
74+
files = append(files, labFiles.Files.Other...)
4575

4676
return files, nil
4777
}
4878

4979
// GetLabPublicFiles fetches only public files for a lab
5080
func (c *MtcApiClient) GetLabPublicFiles(userLessonID string) ([]types.LabFile, error) {
51-
res, err := c.httpClient.R().
52-
SetResult(&types.LabPublicFiles{}).
81+
// Make a single request and print the raw response for debugging
82+
rawRes, err := c.httpClient.R().
5383
Get("/labs/" + userLessonID + "/files/public")
5484
if err != nil {
5585
return nil, err
5686
}
5787

58-
if res.IsError() {
59-
return nil, fmt.Errorf("API error: %s", res.String())
88+
if rawRes.IsError() {
89+
return nil, fmt.Errorf("API error: %s", rawRes.String())
90+
}
91+
92+
// No need for debug printing in production code
93+
94+
// Check for empty files response: {"files":[]}
95+
if string(rawRes.Body()) == "{\"files\":[]}" {
96+
return []types.LabFile{}, nil
97+
}
98+
99+
// Try to unmarshal as a direct array of LabFile
100+
var directFiles []types.LabFile
101+
err = json.Unmarshal(rawRes.Body(), &directFiles)
102+
if err == nil {
103+
// If this succeeds, return the files directly
104+
return directFiles, nil
105+
}
106+
107+
// Try to unmarshal as a simple wrapper with files array
108+
var simpleFiles struct {
109+
Files []types.LabFile `json:"files"`
110+
}
111+
err = json.Unmarshal(rawRes.Body(), &simpleFiles)
112+
if err == nil && simpleFiles.Files != nil {
113+
return simpleFiles.Files, nil
114+
}
115+
116+
// If simple unmarshaling fails, try the structured approach
117+
var labFiles types.LabPublicFiles
118+
err = json.Unmarshal(rawRes.Body(), &labFiles)
119+
if err != nil {
120+
return nil, fmt.Errorf("failed to parse public files response: %v", err)
60121
}
61122

62-
result := res.Result().(*types.LabPublicFiles)
63-
return result.Files, nil
123+
return labFiles.Files, nil
64124
}
65125

66126
// GetLabFileURL fetches a pre-signed URL for a specific file

mtc-cli

4.73 KB
Binary file not shown.

0 commit comments

Comments
 (0)