executable_ssh-aliases 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env -S guile --no-auto-compile -e (ssh-aliases) -s
  2. !#
  3. ;;;; ssh-aliases --- SYNOPSIS
  4. ;;;; Copyright © 2020 Oleg Pykhalov <go.wigust@gmail.com>
  5. ;;;; Released under the GNU GPLv3 or any later version.
  6. (define-module (ssh-aliases)
  7. #:use-module (ice-9 match)
  8. #:use-module (ice-9 rdelim)
  9. #:use-module (srfi srfi-26)
  10. #:use-module (srfi srfi-37)
  11. #:export (main))
  12. ;;; Commentary:
  13. ;;;
  14. ;;; DESCRIPTION
  15. ;;;
  16. ;;; Code:
  17. (define %options
  18. (let ((display-and-exit-proc (lambda (msg)
  19. (lambda (opt name arg loads)
  20. (display msg) (quit)))))
  21. (list (option '(#\v "version") #f #f
  22. (display-and-exit-proc "ssh-aliases version 0.0.1\n"))
  23. (option '(#\h "help") #f #f
  24. (display-and-exit-proc
  25. "Usage: ssh-aliases ...")))))
  26. (define %default-options
  27. '())
  28. (define %know-hosts
  29. (and=> (getenv "HOME")
  30. (lambda (home)
  31. (string-append home "/.ssh/known_hosts"))))
  32. (define %command
  33. (or (and=> (getenv "SSH_COMMAND") (lambda (command) command))
  34. "ssh"))
  35. (define (main args)
  36. (for-each (lambda (host)
  37. (format #t "alias ~a=~s~%"
  38. (if (string-suffix? ".intr" host)
  39. (string-drop-right host (string-length ".intr"))
  40. host)
  41. (format #f "~a ~a" %command host))
  42. (format #t "complete -W ~s ~a~%"
  43. "apache block clean containers images mysql nginx ping sg status te traceroute"
  44. (string-drop-right host (string-length ".intr"))))
  45. (sort (filter (cut string-suffix? ".intr" <>)
  46. (map (match-lambda ((host _ ...) host))
  47. (map (cut string-split <> #\space)
  48. (string-split (with-input-from-file %know-hosts read-string)
  49. #\newline))))
  50. string<)))
  51. ;;; ssh-aliases ends here