-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-sops.pl
More file actions
executable file
·49 lines (44 loc) · 1.05 KB
/
git-sops.pl
File metadata and controls
executable file
·49 lines (44 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
#!/usr/bin/env perl
use strict;
use IPC::Open2;
use IPC::Open3;
binmode STDIN;
binmode STDOUT;
undef $/;
my $mode = shift @ARGV or die "Mode (clean/smudge) required\n";
my $file = shift @ARGV or die "Filename required\n";
my $input = <STDIN>;
sub run_sops {
my ($data, $flag) = @_;
my $pid = open2(my $out, my $in, "sops", $flag, "--filename-override", $file, "/dev/stdin");
binmode $in;
binmode $out;
print $in $data;
close $in;
my $result = <$out>;
close $out;
waitpid($pid, 0);
my $status = $? >> 8;
if ($status != 0) {
die "Error: SOPS exited with $status\n";
}
return $result;
}
if ($mode eq "smudge") {
print run_sops($input, "-d");
} elsif ($mode eq "clean") {
my $head_enc = "";
open(my $null, ">", "/dev/null");
my $pid = open3(undef, my $git_fh, $null, "git", "cat-file", "-p", "HEAD:$file");
binmode $git_fh;
$head_enc = <$git_fh>;
close $git_fh;
close $null;
if (length($head_enc) > 0 && run_sops($head_enc, "-d") eq $input) {
print $head_enc;
} else {
print run_sops($input, "-e");
}
} else {
die "Unknown mode: $mode\n";
}