summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhitsumabushi <hitsumabushi.org@gmail.com>2018-07-05 20:50:19 +0900
committerChristian Poessinger <christian@poessinger.com>2021-12-04 16:44:55 +0100
commitdd3b64dbc42a34dab66798dc6e7baf0dca84a841 (patch)
treeb54328401e09da5bd41c97f3c676a4e096c8fa8b
parentade279c9d5300f920db2f7ecec3a7c220bcd59a4 (diff)
downloadvyatta-config-mgmt-dd3b64dbc42a34dab66798dc6e7baf0dca84a841.tar.gz
vyatta-config-mgmt-dd3b64dbc42a34dab66798dc6e7baf0dca84a841.zip
T4049: add "compare command" support to show config diff in set notation
vyos@vyos# compare [edit protocols bgp] >local-as 20 +neighbor 1.1.1.1 { + remote-as 2000 +} [edit] vyos@vyos# compare commands set protocols bgp local-as '20' set protocols bgp neighbor 1.1.1.1 remote-as '2000' (cherry picked from commit 7ebf5824def200dcbac8ebb3851fa59df405b6c0)
-rwxr-xr-xscripts/vyatta-config-mgmt.pl54
1 files changed, 41 insertions, 13 deletions
diff --git a/scripts/vyatta-config-mgmt.pl b/scripts/vyatta-config-mgmt.pl
index 0d23fcb..c1c92dc 100755
--- a/scripts/vyatta-config-mgmt.pl
+++ b/scripts/vyatta-config-mgmt.pl
@@ -125,6 +125,23 @@ sub filter_version_string {
return join("\n", @lines);
}
+sub format_commands {
+ my ($diff) = @_;
+
+ my @difflines = split('\n', $diff);
+ my @cmds = ();
+ foreach my $line (@difflines){
+ my @words = split(' ', $line);
+ my $elements = scalar(@words);
+ my @non_leaf = @words[0 .. ($elements - 2)] ;
+ my $path = join(' ', @non_leaf);
+ $path =~ s/'//g;
+ my $cmd = "$path " . @words[($elements - 1)];
+ push(@cmds, $cmd);
+ }
+ my $results = join("\n", @cmds);
+ return "$results\n";
+}
#
# main
@@ -264,17 +281,30 @@ if ($action eq 'show-commit-file') {
if ($action eq 'diff') {
print "diff\n" if $debug;
my $args = $#ARGV;
+ my $show_args = '--show-show-defaults --show-context-diff';
+
+ # flag for command style output
+ my $is_command_format = 0;
+ if ($args >= 0 and $ARGV[$args] eq 'commands') {
+ $is_command_format = 1;
+ $show_args = '$show_args --show-commands';
+ delete($ARGV[$args]);
+ $args--;
+ }
+
if ($args < 0) {
my $rc = system("cli-shell-api sessionChanged");
if (defined $rc and $rc > 0) {
print "No changes between working and active configurations\n";
exit 0;
}
- my $show_args = '--show-show-defaults --show-context-diff';
# default behavior for showConfig is @ACTIVE vs. @WORKING, so no
# need to write to a file first
my $diff = `cli-shell-api showConfig $show_args`;
if (defined $diff and length($diff) > 0) {
+ if ($is_command_format) {
+ $diff = format_commands($diff);
+ }
print "$diff";
} else {
print "No changes between working and active configurations\n";
@@ -287,8 +317,11 @@ if ($action eq 'diff') {
my $outfile = $filename1;
$outfile =~ s/(.*)\.gz/$1/g;
system("zcat $filename1 > $outfile");
- my $diff = `cli-shell-api showConfig --show-cfg1 $outfile --show-cfg2 \@WORKING --show-show-defaults --show-context-diff`;
+ my $diff = `cli-shell-api showConfig --show-cfg1 $outfile --show-cfg2 \@WORKING $show_args`;
if (defined $diff and length($diff) > 0) {
+ if ($is_command_format) {
+ $diff = format_commands($diff);
+ }
print "$diff";
} else {
print "No changes between working and "
@@ -308,8 +341,11 @@ if ($action eq 'diff') {
my $outfile2 = $filename2;
$outfile2 =~ s/(.*)\.gz/$1/g;
system("zcat $filename2 > $outfile2");
- my $diff = `cli-shell-api showConfig --show-cfg1 $outfile2 --show-cfg2 $outfile --show-show-defaults --show-context-diff`;
+ my $diff = `cli-shell-api showConfig --show-cfg1 $outfile2 --show-cfg2 $outfile $show_args`;
if (defined $diff and length($diff) > 0) {
+ if ($is_command_format) {
+ $diff = format_commands($diff);
+ }
print "$diff";
} else {
print "No changes between revision $rev1 and "
@@ -332,16 +368,8 @@ if ($action eq 'diff') {
system("zcat $filename2 > $outfile2");
my $diff = `cli-shell-api showConfig --show-cfg1 $outfile2 --show-cfg2 $outfile --show-commands --show-show-defaults --show-context-diff`;
if (defined $diff and length($diff) > 0) {
- my @difflines = split('\n', $diff);
- foreach my $line (@difflines){
- my @words = split(' ', $line);
- my $elements = scalar(@words);
- my @non_leaf = @words[0 .. ($elements - 2)] ;
- my $path = join(' ', @non_leaf);
- $path =~ s/'//g;
- my $cmd = "$path " . @words[($elements - 1)];
- print "$cmd\n";
- }
+ my $commands = format_commands($diff);
+ print "$commands"
} else {
print "No changes between revision $rev1 and "
. "revision $rev2 configurations\n";