Class: Taskcmd::CLI::Project

Inherits:
Thor
  • Object
show all
Defined in:
lib/taskcmd/cli/project.rb

Instance Method Summary collapse

Instance Method Details

#add(name) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/taskcmd/cli/project.rb', line 23

def add(name)
  if Taskcmd.storage.project_exists?(name)
    raise Taskcmd::Error, "project with name '#{name}' already exists"
  end

  project = Taskcmd::Project.new(name)
  path = Taskcmd.storage.save_project(project)
  say("Created project '#{name}', data will be stored at '#{path}'.")

  # If this is the only project
  if Taskcmd.storage.project_name_list.length == 1
    Taskcmd.config_set(:active_project, name)
    say("'#{name}' is now the active project.")
  end
end

#listObject



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/taskcmd/cli/project.rb', line 8

def list
  projects = Taskcmd.storage.project_name_list
  if projects.empty?
    say("No projects.")
    return
  end

  active_project = Taskcmd.config_get(:active_project)
  projects.each do |project|
    out = project + (project == active_project ? " (active)" : "")
    say(out)
  end
end

#rm(name) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/taskcmd/cli/project.rb', line 57

def rm(name)
  unless Taskcmd.storage.project_exists?(name)
    raise Taskcmd::Error, "project with name '#{name}' does not exist"
  end

  path = Taskcmd.storage.project_file(name)
  if options[:force]
    sure = true
  else
    say("This will delete project '#{name}' and all its tasks, along with its file '#{path}'.", [:white, :on_red])
    sure = yes?("This is non-recoverable. Are you sure? Enter 'y' or 'yes' to confirm.", [:white, :on_red])
    say()
  end

  if sure
    active_project = Taskcmd.config_get(:active_project)
    if active_project == name
      Taskcmd.config_set(:active_project, nil)
    end

    Taskcmd.storage.delete_project(name)
    say("Project '#{name}' was deleted.")
  else
    say("Project was not deleted.")
  end
end

#switch(name) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/taskcmd/cli/project.rb', line 40

def switch(name)
  active_project = Taskcmd.config_get(:active_project)
  if active_project == name
    say("'#{name}' is already the active project.")
    return
  end

  unless Taskcmd.storage.project_exists?(name)
    raise Taskcmd::Error, "project with name '#{name}' does not exist"
  end

  Taskcmd.config_set(:active_project, name)
  say("'#{name}' is now the active project.")
end