Class: Kalipso::CLI

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

Instance Method Summary collapse

Instance Method Details

#base_urlObject



91
92
93
# File 'lib/kalipso/cli.rb', line 91

def base_url
  puts Jaysus::Remote.base_url
end

#create(name = nil, path = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/kalipso/cli.rb', line 19

def create(name = nil, path = nil)
  if name.present?
    puts "Creating #{name}"
  else
    puts "Creating a new site"
  end
  begin
    remote_site = Site::Remote.create(:name => name)
    path = path || File.expand_path("~/Sites/oncalypso/#{remote_site.name}")
    Site::Local.create({
      :name => remote_site.name,
      :id => remote_site.id,
      :path => path
    })
    Pathname.new(path).mkpath unless Pathname.new(path).exist?
    puts "Site #{remote_site.name} site created and linked to #{path}"
    
  rescue RestClient::UnprocessableEntity
    puts "There was an error uploading your site"
  end
end

#fetch(name) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/kalipso/cli.rb', line 106

def fetch(name)
  site = Site::Remote.find_by_name(name)
  if site.present?
    puts "fetching #{site.name}"
    local_site = Site::Local.find_or_create_by_id(site.id)
    path = File.expand_path("~/Sites/oncalypso/#{name}")
    pathname = Pathname.new(path)
    pathname.mkpath unless pathname.exist?
    local_site.name = site.name
    local_site.path = path
    local_site.save
    `rsync -arvH -e "ssh -i #{Jaysus::Local.store_dir.join('keys', 'id_rsa')}" [email protected]:/home/sites/#{site.name}/ #{path.gsub(/\/+$/, '')}/`
    puts "Site #{name} downloaded to #{path.gsub(/\/+$/, '')}"
  else
    "Site #{name} not found. Maybe try 'kalipso sync'"
  end
end


71
72
73
74
75
76
# File 'lib/kalipso/cli.rb', line 71

def link(site, path = nil)
  site = Site::Local.find_by_name(site)
  path = path.present? ? File.expand_path(path) : Dir.pwd
  site.update_attributes(:path => path)
  puts "#{site.name} linked to #{path}"
end

#open(site = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/kalipso/cli.rb', line 57

def open(site = nil)
  if site.present?
    site = Site::Local.find_by_name(site)
  else
    site = Site::Local.find_by_path(Dir.pwd)
  end
  if site.present?
    `open http://#{site.name}.oncalypso.com`
  else
    puts "Could not find site"
  end
end

#path(name) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/kalipso/cli.rb', line 5

def path(name)
  site = Site::Local.find_by_name(name)
  if site.present?
    if site.path.present?
      puts site.path
    else
      puts "Path not present. You need to link this site to a path."
    end
  else
    puts "Could not find site #{name}"
  end
end

#sitesObject



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/kalipso/cli.rb', line 42

def sites
  if Site::Local.all.empty?
    sync
  else
    Site::Local.all.each do |site|
      out = "".tap do |out|
        out << site.name
        out << " -> #{site.path}" if site.path.present?
      end
      puts out
    end
  end
end

#store_dirObject



86
87
88
# File 'lib/kalipso/cli.rb', line 86

def store_dir
  puts Jaysus::Local.store_dir
end

#syncObject



96
97
98
99
100
101
102
103
# File 'lib/kalipso/cli.rb', line 96

def sync
  puts "fetching remote sites"
  Site::Remote.all.each do |site|
    puts "syncing #{site.name}"
    local_site = Site::Local.find_or_create_by_id(site.id)
    local_site.update_attributes(site.attributes)
  end
end


79
80
81
82
83
# File 'lib/kalipso/cli.rb', line 79

def unlink(site)
  site = Site::Local.find_by_name(site)
  site.update_attributes(:path => "")
  puts "Site #{site.name} unlinked"
end

#upload(name = nil) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/kalipso/cli.rb', line 125

def upload(name = nil)
  if name.present?
    puts "uploading #{name}"
    site = Site::Local.find_by_name(name)
    path = site.path
  else
    path = File.expand_path(Dir.pwd)
    site = Site::Local.find_by_path(path)
  end
  if site.present?
    puts "uploading #{name} from #{path}"
    if site.path.present?
      puts "uploading #{site.path} to #{site.name}.oncalypso.com"
      command = %Q[rsync -arvH -e "ssh -i #{Jaysus::Local.store_dir.join('keys', 'id_rsa')}" #{site.path.gsub(/\/+$/, '')}/ [email protected]:/home/sites/#{site.name}]
      `#{command}`
      puts "#{site.path} uploaded to http://#{site.name}.oncalypso.com"
    else
      puts "You need to link this site first"
    end
  else
    puts "site not found with path #{path}"
  end
end

#versionObject



150
151
152
# File 'lib/kalipso/cli.rb', line 150

def version
  puts File.read(File.expand_path('../../../VERSION', __FILE__))
end