;; -*- lexical-binding: t; -*- ;; Manage AWS profiles (defvar aws-profiles '("default") "AWS profile names") (defvar aws-current-profile nil "Currently active AWS profile") (defun aws-local-profile () (make-local-variable 'aws-current-profile)) (add-hook 'eshell-mode-hook #'aws-local-profile) (add-hook 'vterm-mode-hook #'aws-local-profile) (add-hook 'term-mode-hook #'aws-local-profile) (defun aws-list-profiles () (when (not (executable-find "aws")) (user-error "Unable to find executable \"aws\"!")) (let ((profile-string (shell-command-to-string "aws configure list-profiles"))) (s-split "\n" profile-string))) (defun aws-switch-profile (profile) (interactive (list (completing-read "Profile: " (aws-list-profiles)))) (setenv "AWS_PROFILE" profile) (when (eq major-mode 'vterm-mode) (vterm-send-C-e) (vterm-send-C-u) (vterm-send-string (format "export AWS_PROFILE=%s\n" profile))) (setq aws-current-profile profile)) (defun vterm-aws-switch-profile (profile) "Switch the AWS profile for the current vterm command." (interactive (list (completing-read "Profile: " (aws-list-profiles)))) (vterm-send-C-a) (vterm-send-string (format "AWS_PROFILE=%s " profile))) (with-eval-after-load 'vterm (define-key vterm-mode-map (kbd "C-c a") #'vterm-aws-switch-profile)) (defun aws-sso-login () (interactive) (let ((buffer "*aws-sso-login*")) (async-shell-command "AWS_PROFILE=default aws sso login" buffer) (with-current-buffer buffer (let ((proc (get-buffer-process (current-buffer)))) (set-process-sentinel proc (lambda (proc sentinel) (when (string-match-p "finished" sentinel) (with-selected-window (get-buffer-window (process-buffer proc) t) (quit-window))))))))) (add-hook 'emacs-startup-hook (lambda () (aws-switch-profile "default"))) ;; A command to MFA to AWS (defun aws-mfa (mfa-token) (interactive (list (let ((prompt (if aws-current-profile (format "MFA code for %s: " aws-current-profile) "MFA code: "))) (read-from-minibuffer prompt)))) (let ((proc (start-process "aws-mfa" "*aws-mfa*" "aws-mfa" "--force"))) (set-process-sentinel proc (make-success-err-msg-sentinel "*aws-mfa*" "AWS MFA succeeded" "AWS MFA failed, check *aws-mfa* buffer for details")) (process-send-string proc (concat mfa-token "\n")))) (define-minor-mode aws-modeline-mode "Displays the current AWS profile in the modeline." :global t (if aws-modeline-mode (add-to-list 'mode-line-misc-info '(:eval (format " AWS:%s " (or aws-current-profile "default")))) (setq mode-line-misc-info (delete '(:eval (format " AWS:%s " (or aws-current-profile "default"))) mode-line-misc-info)))) (add-hook 'after-init-hook #'aws-modeline-mode) ;; A dired interface to S3 (use-package s3ed :commands (s3ed-find-file s3ed-save-file)) (provide 'init-aws)