mirror of
https://github.com/sstent/GarminSync.git
synced 2026-01-25 16:42:20 +00:00
fixing flags
This commit is contained in:
@@ -12,28 +12,17 @@ import (
|
||||
"github.com/sstent/garminsync/internal/garmin"
|
||||
)
|
||||
|
||||
// Global flag variables for download command
|
||||
var downloadAll bool
|
||||
var downloadMissing bool
|
||||
var maxRetries int
|
||||
|
||||
// downloadCmd represents the download command
|
||||
var downloadCmd = &cobra.Command{
|
||||
Use: "download",
|
||||
Short: "Download missing FIT files",
|
||||
Long: `Downloads missing activity files from Garmin Connect`,
|
||||
}
|
||||
|
||||
var downloadAll bool
|
||||
var downloadMissing bool
|
||||
var maxRetries int
|
||||
|
||||
func init() {
|
||||
downloadCmd.Flags().BoolVar(&downloadAll, "all", false, "Download all activities")
|
||||
downloadCmd.Flags().BoolVar(&downloadMissing, "missing", false, "Download only missing activities")
|
||||
downloadCmd.Flags().IntVar(&maxRetries, "max-retries", 3, "Maximum download retry attempts (default: 3)")
|
||||
|
||||
downloadCmd.MarkFlagsMutuallyExclusive("all", "missing")
|
||||
downloadCmd.MarkFlagsRequiredAtLeastOne("all", "missing")
|
||||
|
||||
rootCmd.AddCommand(downloadCmd)
|
||||
|
||||
downloadCmd.RunE = func(cmd *cobra.Command, args []string) error {
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
// Load configuration
|
||||
cfg, err := config.LoadConfig()
|
||||
if err != nil {
|
||||
@@ -52,18 +41,18 @@ func init() {
|
||||
}
|
||||
|
||||
// Initialize database
|
||||
db, err := db.NewDatabase(cfg.DatabasePath)
|
||||
database, err := db.NewDatabase(cfg.DatabasePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to connect to database: %w", err)
|
||||
}
|
||||
defer db.Close()
|
||||
defer database.Close()
|
||||
|
||||
// Get activities to download
|
||||
var activities []garmin.Activity
|
||||
if downloadAll {
|
||||
activities, err = db.GetAll()
|
||||
activities, err = database.GetAll()
|
||||
} else if downloadMissing {
|
||||
activities, err = db.GetMissing()
|
||||
activities, err = database.GetMissing()
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get activities: %w", err)
|
||||
@@ -97,7 +86,7 @@ func init() {
|
||||
err := client.DownloadActivityFIT(activity.ActivityId, filename)
|
||||
if err == nil {
|
||||
// Mark as downloaded in database
|
||||
if err := db.MarkDownloaded(activity.ActivityId, filename); err != nil {
|
||||
if err := database.MarkDownloaded(activity.ActivityId, filename); err != nil {
|
||||
fmt.Printf("⚠️ Failed to mark activity %d as downloaded: %v\n", activity.ActivityId, err)
|
||||
} else {
|
||||
successCount++
|
||||
@@ -119,5 +108,17 @@ func init() {
|
||||
|
||||
fmt.Printf("\n📊 Download summary: %d/%d activities successfully downloaded\n", successCount, total)
|
||||
return nil
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
// Bind flags to global variables
|
||||
downloadCmd.Flags().BoolVar(&downloadAll, "all", false, "Download all activities")
|
||||
downloadCmd.Flags().BoolVar(&downloadMissing, "missing", false, "Download only missing activities")
|
||||
downloadCmd.Flags().IntVar(&maxRetries, "max-retries", 3, "Maximum download retry attempts (default: 3)")
|
||||
|
||||
downloadCmd.MarkFlagsMutuallyExclusive("all", "missing")
|
||||
downloadCmd.MarkFlagsRequiredAtLeastOne("all", "missing")
|
||||
|
||||
rootCmd.AddCommand(downloadCmd)
|
||||
}
|
||||
64
cmd/list.go
64
cmd/list.go
@@ -10,6 +10,11 @@ import (
|
||||
"github.com/sstent/garminsync/internal/garmin"
|
||||
)
|
||||
|
||||
// Global flag variables for list command
|
||||
var listAll bool
|
||||
var listMissing bool
|
||||
var listDownloaded bool
|
||||
|
||||
// listCmd represents the list command
|
||||
var listCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
@@ -19,11 +24,6 @@ var listCmd = &cobra.Command{
|
||||
- Missing activities (not yet downloaded)
|
||||
- Downloaded activities`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
// Get flag values
|
||||
listAll, _ := cmd.Flags().GetBool("all")
|
||||
listMissing, _ := cmd.Flags().GetBool("missing")
|
||||
listDownloaded, _ := cmd.Flags().GetBool("downloaded")
|
||||
|
||||
// Initialize config
|
||||
cfg, err := config.LoadConfig()
|
||||
if err != nil {
|
||||
@@ -36,11 +36,11 @@ var listCmd = &cobra.Command{
|
||||
}
|
||||
|
||||
// Initialize database
|
||||
db, err := db.NewDatabase(cfg.DatabasePath)
|
||||
database, err := db.NewDatabase(cfg.DatabasePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to connect to database: %w", err)
|
||||
}
|
||||
defer db.Close()
|
||||
defer database.Close()
|
||||
|
||||
// Get activities from database with pagination
|
||||
page := 1
|
||||
@@ -50,11 +50,11 @@ var listCmd = &cobra.Command{
|
||||
var err error
|
||||
|
||||
if listAll {
|
||||
filteredActivities, err = db.GetAllPaginated(page, pageSize)
|
||||
filteredActivities, err = database.GetAllPaginated(page, pageSize)
|
||||
} else if listMissing {
|
||||
filteredActivities, err = db.GetMissingPaginated(page, pageSize)
|
||||
filteredActivities, err = database.GetMissingPaginated(page, pageSize)
|
||||
} else if listDownloaded {
|
||||
filteredActivities, err = db.GetDownloadedPaginated(page, pageSize)
|
||||
filteredActivities, err = database.GetDownloadedPaginated(page, pageSize)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
@@ -62,23 +62,37 @@ var listCmd = &cobra.Command{
|
||||
}
|
||||
|
||||
if len(filteredActivities) == 0 {
|
||||
if page == 1 {
|
||||
fmt.Println("No activities found matching the criteria")
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
// Print activities for current page
|
||||
for _, activity := range filteredActivities {
|
||||
fmt.Printf("Activity ID: %d, Start Time: %s, Filename: %s\n",
|
||||
activity.ActivityId, activity.StartTime.Format("2006-01-02 15:04:05"), activity.Filename)
|
||||
status := "❌ Not Downloaded"
|
||||
if activity.Downloaded {
|
||||
status = "✅ Downloaded"
|
||||
}
|
||||
fmt.Printf("ID: %d | %s | %s | %s\n",
|
||||
activity.ActivityId,
|
||||
activity.StartTime.Format("2006-01-02 15:04:05"),
|
||||
activity.Filename,
|
||||
status)
|
||||
}
|
||||
|
||||
// Prompt to continue or quit
|
||||
fmt.Printf("\nPage %d - Show more? (y/n): ", page)
|
||||
var response string
|
||||
fmt.Scanln(&response)
|
||||
if strings.ToLower(response) != "y" {
|
||||
// Only prompt if there might be more results
|
||||
if len(filteredActivities) == pageSize {
|
||||
fmt.Printf("\nPage %d - Show more? (y/n): ", page)
|
||||
var response string
|
||||
fmt.Scanln(&response)
|
||||
if strings.ToLower(response) != "y" {
|
||||
break
|
||||
}
|
||||
page++
|
||||
} else {
|
||||
break
|
||||
}
|
||||
page++
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -86,17 +100,13 @@ var listCmd = &cobra.Command{
|
||||
}
|
||||
|
||||
func init() {
|
||||
// Ensure rootCmd is properly initialized before adding subcommands
|
||||
if rootCmd == nil {
|
||||
panic("rootCmd must be initialized before adding subcommands")
|
||||
}
|
||||
|
||||
listCmd.Flags().Bool("all", false, "List all activities")
|
||||
listCmd.Flags().Bool("missing", false, "List activities that have not been downloaded")
|
||||
listCmd.Flags().Bool("downloaded", false, "List activities that have been downloaded")
|
||||
// Bind flags to global variables
|
||||
listCmd.Flags().BoolVar(&listAll, "all", false, "List all activities")
|
||||
listCmd.Flags().BoolVar(&listMissing, "missing", false, "List activities that have not been downloaded")
|
||||
listCmd.Flags().BoolVar(&listDownloaded, "downloaded", false, "List activities that have been downloaded")
|
||||
|
||||
listCmd.MarkFlagsMutuallyExclusive("all", "missing", "downloaded")
|
||||
listCmd.MarkFlagsRequiredAtLeastOne("all", "missing", "downloaded")
|
||||
|
||||
rootCmd.AddCommand(listCmd)
|
||||
}
|
||||
}
|
||||
@@ -9,11 +9,12 @@ import (
|
||||
|
||||
// Config holds application configuration
|
||||
type Config struct {
|
||||
GarminEmail string
|
||||
GarminPassword string
|
||||
DatabasePath string
|
||||
RateLimit time.Duration
|
||||
SessionPath string
|
||||
GarminEmail string
|
||||
GarminPassword string
|
||||
DatabasePath string
|
||||
RateLimit time.Duration
|
||||
SessionPath string
|
||||
SessionTimeout time.Duration
|
||||
}
|
||||
|
||||
// LoadConfig loads configuration from environment variables
|
||||
@@ -30,6 +31,8 @@ func LoadConfig() (*Config, error) {
|
||||
}
|
||||
|
||||
rateLimit := parseDuration(os.Getenv("RATE_LIMIT"), 2*time.Second)
|
||||
sessionTimeout := parseDuration(os.Getenv("SESSION_TIMEOUT"), 30*time.Minute)
|
||||
|
||||
sessionPath := os.Getenv("SESSION_PATH")
|
||||
if sessionPath == "" {
|
||||
sessionPath = "/data/session.json"
|
||||
@@ -41,11 +44,12 @@ func LoadConfig() (*Config, error) {
|
||||
}
|
||||
|
||||
return &Config{
|
||||
GarminEmail: email,
|
||||
GarminPassword: password,
|
||||
DatabasePath: databasePath,
|
||||
RateLimit: rateLimit,
|
||||
SessionPath: sessionPath,
|
||||
GarminEmail: email,
|
||||
GarminPassword: password,
|
||||
DatabasePath: databasePath,
|
||||
RateLimit: rateLimit,
|
||||
SessionPath: sessionPath,
|
||||
SessionTimeout: sessionTimeout,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -61,4 +65,4 @@ func parseDuration(value string, defaultValue time.Duration) time.Duration {
|
||||
}
|
||||
|
||||
return d
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user