aislegopher/cmd/main.go
Steve Dudenhoeffer 9da603f937 Add context support to GetPriceHistory function
Refactored GetPriceHistory to accept a context parameter, enabling better control over request lifecycle and cancellation. Updated the main function and deferred body close logic to align with the new context usage. This improves code robustness and readability.
2025-01-19 23:54:17 -05:00

48 lines
874 B
Go

package main
import (
"context"
"fmt"
"os"
"strconv"
"gitea.stevedudenhoeffer.com/steve/aislegopher"
)
func main() {
// usage: aisegopher <product_id>
ctx := context.Background()
if len(os.Args) != 2 {
fmt.Println("usage: aisegopher <product_id>")
os.Exit(1)
}
productId, err := strconv.Atoi(os.Args[1])
if err != nil {
fmt.Println("Invalid product ID: ", os.Args[1], " error:", err)
os.Exit(1)
}
priceHistory, err := aislegopher.GetPriceHistory(ctx, productId)
if err != nil {
fmt.Println("Error getting price history: ", err)
os.Exit(1)
}
fmt.Println("Regular Prices:")
for _, dp := range priceHistory.RegularPrices {
fmt.Println(dp.Date, dp.Price)
}
if len(priceHistory.ThirdPartyPrices) > 0 {
fmt.Println("Third Party Prices:")
for _, dp := range priceHistory.ThirdPartyPrices {
fmt.Println(dp.Date, dp.Price)
}
}
}