Skip to content

Commit 60e7452

Browse files
committed
refactor(utils): enhance cost handling and error management
- Introduce new types for RepoName and Cost. - Add GetCosts function to read and parse cost data. - Implement CostFileNotFoundError for better error handling. - Refactor getRepoName to GetRepoName for consistency. [Generated by Kommit]
1 parent e6c76c0 commit 60e7452

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

internal/utils/cost.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,31 @@ func costFilepath() string {
2020
return filepath.Join(dir, "kommit", "cost.json")
2121
}
2222

23-
type Cost struct {
24-
RepoName string
25-
Cost float64
23+
type RepoName string
24+
type Cost float64
25+
type Costs map[RepoName]Cost
26+
27+
func GetCosts() (Costs, error) {
28+
costFilePath := costFilepath()
29+
if costFilePath == "" {
30+
return nil, fmt.Errorf("could not determine cost file path")
31+
}
32+
33+
if _, err := os.Stat(costFilePath); os.IsNotExist(err) {
34+
return nil, CostFileNotFoundError{}
35+
}
36+
37+
costData, err := os.ReadFile(costFilePath)
38+
if err != nil {
39+
return nil, CostFileNotFoundError{}
40+
}
41+
42+
var costs Costs
43+
if err := json.Unmarshal(costData, &costs); err != nil {
44+
return nil, fmt.Errorf("failed to unmarshal cost data: %w", err)
45+
}
46+
47+
return costs, nil
2648
}
2749

2850
func UpdateCost(cost float64) error {
@@ -36,7 +58,7 @@ func UpdateCost(cost float64) error {
3658
return fmt.Errorf("failed to create directory: %w", err)
3759
}
3860

39-
repoName, err := getRepoName()
61+
repoName, err := GetRepoName()
4062
if err != nil {
4163
return fmt.Errorf("failed to get repository name: %w", err)
4264
}
@@ -62,7 +84,7 @@ func UpdateCost(cost float64) error {
6284
return nil
6385
}
6486

65-
func getRepoName() (string, error) {
87+
func GetRepoName() (string, error) {
6688
fullPath, err := ExecGit("rev-parse", "--show-toplevel")
6789
if err != nil {
6890
return "", err

internal/utils/errors.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,9 @@ func (e UnsupportedModelError) Is(target error) bool {
1616
_, ok := target.(UnsupportedModelError)
1717
return ok
1818
}
19+
20+
type CostFileNotFoundError struct{}
21+
22+
func (e CostFileNotFoundError) Error() string {
23+
return "Cost file not found"
24+
}

0 commit comments

Comments
 (0)