Module: Zillabyte::Helpers

Extended by:
Helpers
Included in:
Auth, CLI, Command, Command::Base, Helpers, Runner::AppRunner, Runner::ComponentRunner
Defined in:
lib/zillabyte/helpers.rb

Defined Under Namespace

Classes: DataSchemaBuilder, TableOutputBuilder

Instance Method Summary collapse

Instance Method Details

#add_git_remote(name, remote = "zillabyte") ⇒ Object



117
118
119
120
121
122
123
124
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/zillabyte/helpers.rb', line 117

def add_git_remote(name, remote = "zillabyte")
  
  remotes = {}
  remotes[remote] = Zillabyte::Auth.git_host
  
  if ENV["ZILLABYTE_EXTRA_GIT_REMOTES"]
    # This is for dev purposes -- auto init various git env endpoints
    JSON.parse(ENV["ZILLABYTE_EXTRA_GIT_REMOTES"]).each_pair do |remote, host|
      remotes[remote] = host
    end
  end
  
  # Maybe initialize a git directory at the app root... 
  if File.exists?(File.join(Dir.pwd, ".git")) == false && File.exists?(File.join(Dir.pwd, "zillabyte.conf.yaml"))
    git("init .")
  end
  
  # Install each git remote
  remotes.each_pair do |remote, host|
  
    if git('remote').split("\n").include?(remote)
      # already exists.. remove it. 
      git("remote remove #{remote}")
    end

    git_url = "git@#{host}:#{Zillabyte::Auth.user}/#{name}"
    create_git_remote(remote, git_url)
    
  end
  
end

#app ⇒ Object



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

def app
  @app ||= if options[:confirm].is_a?(String)
    if options[:app] && (options[:app] != options[:confirm])
      error("Mismatch between --app and --confirm")
    end
    options[:confirm]
  elsif options[:app].is_a?(String)
    options[:app]
  elsif ENV.has_key?('ZILLABYTE_APP')
    ENV['ZILLABYTE_APP']
  elsif app_from_dir = extract_app_in_dir(Dir.pwd)
    app_from_dir
  else
    # raise instead of using error command to enable rescuing when app is optional
    raise Zillabyte::Command::CommandFailed.new("No app specified.\nRun this command from an app folder or specify which app to use with --app APP.") unless options[:ignore_no_app]
  end
end

#ask ⇒ Object



41
42
43
# File 'lib/zillabyte/helpers.rb', line 41

def ask
  $stdin.gets.to_s.strip
end

#create_git_remote(remote, url) ⇒ Object



65
66
67
68
69
70
# File 'lib/zillabyte/helpers.rb', line 65

def create_git_remote(remote, url)
  return if git('remote').split("\n").include?(remote)
  return unless File.exists?(".git")
  git "remote add #{remote} #{url}"
  display "git remote #{remote} added"
end

#display(msg = "", new_line = true) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/zillabyte/helpers.rb', line 8

def display(msg="", new_line=true)
  if new_line
   puts(msg)
  else
    print(msg)
    $stdout.flush
  end
end

#error(message, format = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/zillabyte/helpers.rb', line 18

def error(message, format=nil)
  if format == "json"
    $stdout.puts(({"error"=>message}).to_json)
    exit
  else
    $stderr.puts(format_with_bang(message))
    exit(1)
  end
end

#extract_app_from_git_config ⇒ Object



111
112
113
114
# File 'lib/zillabyte/helpers.rb', line 111

def extract_app_from_git_config
  remote = git("config zillabyte.remote")
  remote == "" ? nil : remote
end

#extract_app_in_dir(dir) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/zillabyte/helpers.rb', line 94

def extract_app_in_dir(dir)
  return unless remotes = git_remotes(dir)

  if remote = options[:remote]
    remotes[remote]
  elsif remote = extract_app_from_git_config
    remotes[remote]
  else
    apps = remotes.values.uniq
    if apps.size == 1
      apps.first
    else
      raise(Heroku::Command::CommandFailed, "Multiple apps in folder and no app specified.\nSpecify app with --app APP.") unless options[:ignore_no_app]
    end
  end
end

#format_with_bang(message) ⇒ Object



28
29
30
31
# File 'lib/zillabyte/helpers.rb', line 28

def format_with_bang(message)
  return '' if message.to_s.strip == ""
  " !    " + message.split("\n").join("\n !    ")
end

#get_flow_name(dir = Dir.pwd) ⇒ Object



150
151
152
153
154
# File 'lib/zillabyte/helpers.rb', line 150

def get_flow_name(dir = Dir.pwd)
  meta = Zillabyte::API::Flows.get_rich_meta_info_from_script(dir)
  return meta["name"] if meta
  return nil
end

#git(args) ⇒ Object



59
60
61
62
63
# File 'lib/zillabyte/helpers.rb', line 59

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

#handle_downloading_manifest(file, res, type = nil) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/zillabyte/helpers.rb', line 157

def handle_downloading_manifest(file, res, type = nil)
  if res["manifest"]
    res["manifest"].each_with_index do |uri, index|
    
      # Do we have multiple files? then suffix with numbers... 
      if res["manifest"].size <= 1
        shard_file = file
      else
        shard_file = "#{File.basename(file, File.extname(file))}__#{'%04i' % index}#{File.extname(file)}" 
      end
    
      display "downloading #{shard_file}" unless type == "json"
      
      File.open(shard_file, "w") do |f|
        f.write open(uri).read
      end
    end
  else
    error "expected remote server to return file manifest"
  end
end

#has_git? ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
# File 'lib/zillabyte/helpers.rb', line 54

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

#longest(items) ⇒ Object



33
34
35
# File 'lib/zillabyte/helpers.rb', line 33

def longest(items)
  items.map { |i| i.to_s.length }.sort.last
end

#read_multiline ⇒ Object



37
38
39
# File 'lib/zillabyte/helpers.rb', line 37

def read_multiline
  $stdin.gets
end

#with_tty(&block) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/zillabyte/helpers.rb', line 45

def with_tty(&block)
  return unless $stdin.isatty
  begin
    yield
  rescue
    # fails on windows
  end
end