Class: GitFit::CLI

Inherits:
Thor
  • Object
show all
Includes:
GitFit::Cli::Checkpointable, GitFit::Cli::Sync
Defined in:
lib/git_fit/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GitFit::Cli::Checkpointable

#checkpoint_db

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/git_fit/cli.rb', line 13

def self.exit_on_failure?
  true
end

Instance Method Details

#auth(source) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/git_fit/cli.rb', line 82

def auth(source)
  if source == 'garmin'
    GitFit::Auth::Garmin.new(options, git_fit_config).call
    return
  end
  if source == 'strava'
    GitFit::Auth::Strava.new(options, git_fit_config).call
  else
    super
  end
rescue GitFit::Sync::AuthError => e
  say_status :error, "Auth failed: #{e.message}", :red
end

#dedupObject



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/git_fit/cli.rb', line 106

def dedup
  db = connect_db
  log_path = options[:log] == 'auto' ? nil : options[:log]
  count = GitFit::Dedup::Service.new(
    db,
    incremental: options[:incremental],
    log_path: log_path,
    verbose: options[:verbose],
  ).run
  say_status :done, "Dedup: #{count} matches updated", :green
rescue StandardError => e
  say_status :error, "Dedup failed: #{e.message}", :red
end

#initObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/git_fit/cli.rb', line 128

def init
  path = options[:output] || GitFit::Config.default_path
  FileUtils.mkdir_p(File.dirname(path))

  if File.exist?(path)
    existing = YAML.safe_load(File.read(path)) || {}
    merged = deep_merge(ConfigTemplate.hash, existing)
    File.write(path, YAML.dump(merged))
    say_status :merged, path, :green
  else
    File.write(path, ConfigTemplate.generate)
    say_status :created, path, :green
  end
end

#parse(file) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/git_fit/cli.rb', line 33

def parse(file)
  format = file.match?(/\.gpx$/i) ? :gpx : :tcx
  content = File.read(file)
  parser = format == :gpx ? GitFit::Parser::GPX.new(source: 'file') : GitFit::Parser::TCX.new(source: 'file')
  activities = parser.call(content)

  if activities.empty?
    say_status :warn, "No activities found in #{file}", :yellow
    return
  end

  activities.each do |act|
    say_status :activity, act[:name] || 'unnamed', :green
    say "  #{act[:sport_category]}#{act[:sport_type] ? " (#{act[:sport_type]})" : ""}"
    say "  #{act[:start_date]} | #{act[:distance]&.round(2)}m" if act[:distance]
    say "  #{act[:moving_time]}s" if act[:moving_time]
  end

  if options[:save]
    db = connect_db
    now = Time.now.utc
    activities.each do |act|
      existing = db[:activities].where(run_id: act[:run_id]).first
      if existing
        db[:activities].where(run_id: act[:run_id]).update(act.merge(updated_at: now))
      else
        db[:activities].insert(act.merge(created_at: now, updated_at: now))
      end
    end
    say_status :done, "Saved #{activities.size} activities", :green
  end
rescue StandardError => e
  say_status :error, "Parse failed: #{e.message}", :red
end

#syncObject



72
73
74
# File 'lib/git_fit/cli.rb', line 72

def sync
  super
end

#versionObject



27
28
29
# File 'lib/git_fit/cli.rb', line 27

def version
  puts "git-fit v#{GitFit::VERSION}"
end