Class: Ding::Cli

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

Instance Method Summary collapse

Instance Method Details

#key_genObject



113
114
115
116
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
# File 'lib/ding/cli.rb', line 113

def key_gen
  key_name = options[:name] || "#{options[:host]}_#{options[:type]}"
  say "\nDing ding ding: let's create and configure a new ssh key #{key_name}:\n\n", :green

  Ding::Ssh.new(options).tap do |s|
    if s.ssh_key_exists?(key_name)
      if yes?("Do you want me to replace the existing key?", :yellow)
        say "> Removing existing key #{key_name}", :cyan
        s.delete_ssh_key key_name

        say "> Creating the replacement ssh key pair", :cyan
        s.create_ssh_key key_name, ENV['USER']
      else
        say "> Using existing key #{key_name}", :cyan
      end
    else
      say "> Creating the new ssh key pair", :green
      s.create_ssh_key key_name, ENV['USER']
    end
    say "> Adding the private key to the ssh config", :green
    s.update_config options[:host], key_name

    say "> Copying the public key to the clipboard", :green
    copy_file_to_clipboard s.ssh_public_key_file(key_name)
  end

rescue => e
  show_error e
else
  say "\n  --> I'm finished: ding ding ding!\n\n", :green
  exit 0
end

#key_showObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/ding/cli.rb', line 147

def key_show
  say "\nDing ding ding: let's copy a public key to the clipboard:\n\n", :green

  Ding::Ssh.new(options).tap do |s|
    key_name = ask_which_item(s.list_ssh_keys, "Which key do you want to copy?")
    say "\n> Copying the public key to the clipboard", :green
    copy_file_to_clipboard s.ssh_public_key_file(key_name)
  end

rescue => e
  show_error e
else
  say "\n  --> You can now Command-v to paste that key: ding ding ding!\n\n", :green
  exit 0
end

#pushObject



23
24
25
26
27
28
29
30
31
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ding/cli.rb', line 23

def push
  testing_branch = options[:branch] || Ding::TESTING_BRANCH.dup
  develop_branch = Ding::DEVELOP_BRANCH.dup

  if options[:skip]
    say "\nDing ding ding: let's push the development branch to #{testing_branch}:\n\n", :green
  else
    say "\nDing ding ding: let's merge one or more feature branches to #{testing_branch}:\n\n", :green
  end

  repo = Ding::Git.new(options).tap do |r|
    if r.is_dirty?
      say "> Local repo is dirty, resetting state", :yellow
      r.reset_local_state
    end
    r.checkout r.branch_in_context(develop_branch)

    say "> Deleting #{testing_branch}", :green
    r.delete_branch(testing_branch)

    say "> Synchronising with the remote", :green
    r.update
  end

  unless options[:skip]
    branches = repo.remote_branches(options[:pattern])
    if branches.empty?
      say "\n --> No feature branches available to test, I'm out of here!\n\n", :red
      exit 1
    end
    feature_branches = ask_which_item(branches, "\nWhich feature branch should I use?", :multiple)
    say "\n"
  end

  original_commit_parents = repo.commit_parents(testing_branch)

  repo.tap do |r|
    say "> Deleting any synched #{testing_branch}", :green
    r.delete_branch(testing_branch)

    say "> Creating #{testing_branch} from #{develop_branch}", :green
    r.create_branch(testing_branch)

    unless options[:skip]
      say "> Merging in selected feature #{feature_branches.count == 1 ? 'branch' : 'branches'}...", :green
      merge_errors = false
      feature_branches.each do |branch|
        if r.merge_branch(branch)
          say "#{branch}", :green
        else
          say "#{branch}", :red
          merge_errors = true
        end
      end
    end

    unless merge_errors
      say "> Pushing #{testing_branch} to the remote", :green
      r.push(testing_branch)
    else
      say "\n  --> There were merge errors, ding dang it!\n\n", :red
      exit 1
    end

    if original_commit_parents &&
        (remote_commit_parents = repo.commit_parents(testing_branch)) &&
        remote_commit_parents.sort == original_commit_parents.sort
      result = options[:verbose] ? ": #{original_commit_parents}" : ''
      say "\n  --> That did not alter the testing branch commit parents#{result}!\n", :yellow
    end
  end

rescue => e
  show_error e
else
  say "\n  --> I'm finished: ding ding ding!\n\n", :green
  exit 0
end

#versionObject



103
104
105
# File 'lib/ding/cli.rb', line 103

def version
  say "ding version #{Ding::VERSION}\n"
end