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