Class: Ding::Cli

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

Instance Method Summary collapse

Instance Method Details

#key_genObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ding/cli.rb', line 96

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



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ding/cli.rb', line 130

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



16
17
18
19
20
21
22
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
# File 'lib/ding/cli.rb', line 16

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

  say "\nDing ding ding: let's merge one or more feature branches to #{testing_branch}:\n\n", :green

  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

    develop_branch = r.branch_in_context(Ding::DEVELOP_BRANCH.dup)
    r.checkout develop_branch

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

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

  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)

  commit_parents = repo.commit_parents(testing_branch)

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

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

    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

    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 repo.commit_parents(testing_branch).sort == commit_parents.sort
      result = options[:verbose] ? ": #{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



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

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