Class: Trk::CLI
- Inherits:
-
Thor
- Object
- Thor
- Trk::CLI
- Defined in:
- lib/trk/cli.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add_github_keys(username) ⇒ Object
- #completion ⇒ Object
- #link_show(filename) ⇒ Object
- #link_show_end(filename) ⇒ Object
- #link_show_raw(filename) ⇒ Object
- #open_shell ⇒ Object
- #pull ⇒ Object
- #sos(*arguments) ⇒ Object
Class Method Details
.help(shell, subcommand = false) ⇒ Object
18 19 20 21 22 |
# File 'lib/trk/cli.rb', line 18 def self.help(shell, subcommand = false) super puts "\nTo enable autocomplete, put in .bashrc following two lines:" puts "# https://trk.tools/app/trk-readme-source-links\ntrk completion > /dev/null 2>&1 && complete -W \"$(trk completion)\" trk" end |
.start(given_args = ARGV, config = {}) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/trk/cli.rb', line 10 def self.start(given_args = ARGV, config = {}) if given_args.include?("--version") || given_args.include?("-v") puts "trk version #{VERSION}" exit end super end |
Instance Method Details
#add_github_keys(username) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/trk/cli.rb', line 30 def add_github_keys(username) uri = URI "https://trk.tools/sh/ssh-tips/-/raw/main/add_github_keys_to_authorized_keys.sh" script = Net::HTTP.get uri stdout, stderr, status = Open3.capture3 "bash -s -- #{username}", stdin_data: script puts stdout if status.success? puts "#{CHECK_ICON} add_github_keys #{username} done" else puts "#{UNCHECK_ICON} add_github_keys #{username} failed:\n#{stderr}" end end |
#completion ⇒ Object
25 26 27 |
# File 'lib/trk/cli.rb', line 25 def completion puts self.class.all_commands.keys.join(" ") end |
#link_show(filename) ⇒ Object
126 127 128 129 130 131 132 133 134 |
# File 'lib/trk/cli.rb', line 126 def link_show(filename) result = Link.new(filename).show if result.success? # puts "#{CHECK_ICON} link done #{result.message}\n#{result.data[:link]}" puts result.data[:link] else puts "#{UNCHECK_ICON} link failed\n#{result.}" end end |
#link_show_end(filename) ⇒ Object
137 138 139 140 141 142 143 144 145 |
# File 'lib/trk/cli.rb', line 137 def link_show_end(filename) result = Link.new(filename).show_end if result.success? # puts "#{CHECK_ICON} link done #{result.message}\n#{result.data[:link]}" puts result.data[:link] else puts "#{UNCHECK_ICON} link failed\n#{result.}" end end |
#link_show_raw(filename) ⇒ Object
148 149 150 151 152 153 154 155 156 |
# File 'lib/trk/cli.rb', line 148 def link_show_raw(filename) result = Link.new(filename).raw if result.success? # puts "#{CHECK_ICON} link done #{result.message}\n#{result.data[:link]}" puts result.data[:link] else puts "#{UNCHECK_ICON} link failed\n#{result.}" end end |
#open_shell ⇒ Object
120 121 122 123 |
# File 'lib/trk/cli.rb', line 120 def open_shell puts "Opening shell for manual fix. Type 'exit' when done." system('PS1="trk pull paused on [$(basename $(pwd))]$ " bash --norc') end |
#pull ⇒ Object
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/trk/cli.rb', line 43 def pull base_dir = File.("~/trk.tools") sub_sub_dirs = Dir.glob("#{base_dir}/*/*").select { |d| File.directory?(d) } sub_sub_dirs.each do |dir| next unless File.directory?(File.join(dir, ".git")) Dir.chdir(dir) do puts "\nš Check if the last commit is the same in: #{dir}" # Get remote latest commit SHA via glab API project_path = dir.sub("#{base_dir}/", "") encoded_project = URI.encode_www_form_component(project_path) stdout_remote, stderr, status = Open3.capture3("glab api projects/#{encoded_project}/repository/commits?per_page=1") unless status.success? puts "#{UNCHECK_ICON} Glab api failed: #{dir}\n#{stderr}" open_shell next end remote_commit = JSON.parse(stdout_remote).first["id"] stdout_local, _stderr_local, status_local = Open3.capture3("git rev-parse HEAD") local_commit = stdout_local.strip if status_local.success? if remote_commit == local_commit stdout, stderr, _status = Open3.capture3("git status --porcelain") if stdout.empty? puts "#{CHECK_ICON} Up-to-date: #{project_path}, skipping pull" else puts "#{UNCHECK_ICON} Up-to-date but Git status --porcelain failed in: #{dir}\n#{stdout}\n#{stderr}" system "git status" system "git diff" puts "Do you want to run 'git add . && git commit -amUpdate' and continue (Y/n) ?" answer = $stdin.gets.strip.downcase if answer == "" || answer == "y" success = system "git add . && git commit -amUpdate && git push" open_shell unless success else open_shell end end else puts "\nš Pulling updates in: #{dir}" _stdout, stderr, status = Open3.capture3("git pull") unless status.success? puts "#{UNCHECK_ICON} Git pull failed in: #{dir}\n#{stderr}" open_shell next end _stdout, stderr, status = Open3.capture3("git push") unless status.success? puts "#{UNCHECK_ICON} Git push failed in: #{dir}\n#{stderr}" open_shell next end stdout, stderr, _status = Open3.capture3("git status --porcelain") if stdout.empty? puts "#{CHECK_ICON} Git pull push successful in: #{dir}" else puts "#{UNCHECK_ICON} Git status --porcelain failed in: #{dir}\n#{stdout}\n#{stderr}" system "git status" system "git diff" puts "Do you want to run 'git add . && git commit -amUpdate' and continue (Y/n) ?" answer = $stdin.gets.strip.downcase if answer == "" || answer == "y" success = system "git add . && git commit -amUpdate && git push" open_shell unless success else open_shell end end end end end puts "\n#{CHECK_ICON} All done!" end |
#sos(*arguments) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/trk/cli.rb', line 159 def sos(*arguments) command = <<~HERE_DOC # Start of code: https://trk.tools/sh/screen-tips#remote-multiuser-access-help-using-ssh-and-gnu-screen # remote version sos() { sos_user=$1 sos_port=$2 sos_host=h2.trk.tools bash <(curl -sL sos.trk.tools) ;} # local version # sos() { sos_user=$1 sos_port=$2 sos_host=h2.trk.tools ~/trk.tools/sh/screen-tips/remote_multiuser_access_help_using_ssh_and_gnu_screen.sh ;} # End of code: https://trk.tools/sh/screen-tips#remote-multiuser-access-help-using-ssh-and-gnu-screen HERE_DOC bashrc_path = File.("~/.bashrc") bashrc_content = File.read(bashrc_path) if File.exist?(bashrc_path) if bashrc_content&.include?("sos()") puts "#{UNCHECK_ICON} sos already exists in .bashrc" else File.open(bashrc_path, "a") do |file| file.puts file.puts command end puts "#{CHECK_ICON} sos function added to .bashrc, please open new terminal, or run: source ~/.bashrc" end end |