Class: GitFit::CLI

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


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

def self.exit_on_failure?
  true
end

Instance Method Details

#initObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/git_fit/cli.rb', line 85

def init
  path = options[:output] || 'config/config.yml'
  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



32
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
# File 'lib/git_fit/cli.rb', line 32

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



70
71
72
# File 'lib/git_fit/cli.rb', line 70

def sync
  super
end

#versionObject



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

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