Skip to content

Instantly share code, notes, and snippets.

@mamemomonga
Created January 19, 2022 02:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mamemomonga/134498dc27e818ac7dda91c572378aca to your computer and use it in GitHub Desktop.
Save mamemomonga/134498dc27e818ac7dda91c572378aca to your computer and use it in GitHub Desktop.
AWSのProfileを検索するツール
#!/usr/bin/env perl
#vim: ft=perl
# -----------------------------
# 使い方
# $ aws-profile-find [プロファイル名]
# プロファイル未指定・複数マッチの場合はプロファイル一覧を返します。
# プロファイルが1つだけ見つかった場合は、AWS_DEFAULT_PROFILEのexport文を返します。
# -----------------------------
use strict;
use warnings;
use feature 'say';
sub readConfig {
my $path=shift;
my $cnf="";
local $/;
open(my $fh,'<',$path) || die "$path - not found";
$cnf=<$fh>;
return $cnf;
}
sub profiles {
$_=shift;
my @profiles=();
while(/\[profile (.+?)\]/mg) {
push @profiles,$1;
}
return @profiles;
}
sub showList {
my @list=@_;
foreach(@list) {
say " $_";
}
}
my $arg=$ARGV[0] || '';
my @profiles=profiles( readConfig("$ENV{HOME}/.aws/config"));
if($arg eq '') {
showList(@profiles);
say '引数がありません';
exit(0);
}
my @matched=grep {/\Q$ARGV[0]\E/} @profiles;
if($#matched == 0) {
say "export AWS_DEFAULT_PROFILE=$matched[0]";
} elsif($#matched == -1) {
say 'マッチしません';
showList(@profiles);
} else {
say '複数見つかりました';
showList(@matched);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment