Module: Pagoda::Helpers

Included in:
Command::Base, Command::Base
Defined in:
lib/pagoda/cli/helpers.rb

Constant Summary collapse

INDENT =
"  "

Instance Method Summary collapse

Instance Method Details

#build_indent(level = 1) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/pagoda/cli/helpers.rb', line 103

def build_indent(level=1)
  indent = ""
  level.times do
    indent += INDENT
  end
  indent
end

#confirm(message = "Are you sure you wish to continue? ", level = 1) ⇒ Object

def ask(message=nil, level=1)

(running_on_windows?) ? print("#{build_indent(level)}#{message}") : print("#{build_indent(level)}#{message}".blue)
STDOUT.flush
STDIN.gets.strip

end



32
33
34
35
36
37
38
39
40
# File 'lib/pagoda/cli/helpers.rb', line 32

def confirm(message="Are you sure you wish to continue? ", level=1)
  return true if ARGV.include? "-f"
  case message
  when Array
    agree("<%= color('#{message.join("\n")}', :blue)%>")
  when String
    agree("<%= color('#{message}', :blue)%>")
  end
end

#create_git_remote(id, remote) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/pagoda/cli/helpers.rb', line 74

def create_git_remote(id, remote)
  error "you do not have git installed on your computer" unless has_git?
  if git('remote').split("\n").include?(remote)
    display "Given remote (#{remote}) is already in use on this repo" 
    remote = ask("<%= color('what would you like to call the new remote? ', :blue) %>")
  end
  unless File.directory?(".git")
    if confirm "git has not been initialized yet, would you like us to do this for you? "
      display "git repo is being created in '#{Dir.pwd}'"
      git "init"
    else
      error(["repo has not been initialized." , "try 'git init'"])
    end
  end
  git "remote add #{remote} [email protected]:#{id}.git"
  git "config --add pagoda.id #{id}"
  display "Git remote #{remote} added"
  remote
end

#display(msg = "", newline = true, level = 1) ⇒ Object



17
18
19
# File 'lib/pagoda/cli/helpers.rb', line 17

def display(msg="", newline=true, level=1)
  say("<%= color('#{msg}', :green)%>")
end

#display_name(app) ⇒ Object



64
65
66
# File 'lib/pagoda/cli/helpers.rb', line 64

def display_name(app)
  client.app_info(app)[:name]
end

#error(msg, exit = true, level = 1) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/pagoda/cli/helpers.rb', line 42

def error(msg, exit=true, level=1)
  STDERR.puts
  STDERR.puts msg
  # begin
  #   case msg
  #   when Array
  #     say("<%= color('#{msg.join("\n")}', :red)%>")
  #   when String
  #     say("<%= color(msg, :red)%>")
  #   end
  # rescue 
  #   puts msg
  # end
  STDERR.puts
  exit 1 if exit
end

#format_date(date) ⇒ Object



21
22
23
24
# File 'lib/pagoda/cli/helpers.rb', line 21

def format_date(date)
  date = Time.parse(date) if date.is_a?(String)
  date.strftime("%Y-%m-%d %H:%M %Z")
end

#git(args) ⇒ Object



68
69
70
71
72
# File 'lib/pagoda/cli/helpers.rb', line 68

def git(args)
  return "" unless has_git?
  flattened_args = [args].flatten.compact.join(" ")
  %x{ git #{flattened_args} 2>&1 }.strip
end

#has_git?Boolean

Returns:

  • (Boolean)


59
60
61
62
# File 'lib/pagoda/cli/helpers.rb', line 59

def has_git?
  %x{ git --version }
  $?.success?
end

#home_directoryObject



5
6
7
# File 'lib/pagoda/cli/helpers.rb', line 5

def home_directory
  running_on_windows? ? ENV['USERPROFILE'] : ENV['HOME']
end

#remove_app(app) ⇒ Object



94
95
96
# File 'lib/pagoda/cli/helpers.rb', line 94

def remove_app(app)
  remove_git_remote(app)
end

#remove_git_remote(app) ⇒ Object



98
99
100
101
# File 'lib/pagoda/cli/helpers.rb', line 98

def remove_git_remote(app)
  git "remote rm pagoda"
  git "config --unset pagoda.id"
end

#running_on_a_mac?Boolean

Returns:

  • (Boolean)


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

def running_on_a_mac?
  RUBY_PLATFORM =~ /-darwin\d/
end

#running_on_windows?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/pagoda/cli/helpers.rb', line 9

def running_on_windows?
  RUBY_PLATFORM =~ /mswin32|mingw32/
end