From a7ef79a69c6af2593eea9f74b1ff64e39379f73f Mon Sep 17 00:00:00 2001 From: Zach Musgrave Date: Wed, 7 Jul 2021 15:33:08 -0700 Subject: [PATCH] Added a simple shell for exploring a noms dataset --- go/store/cmd/noms/splunk.pl | 103 ++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100755 go/store/cmd/noms/splunk.pl diff --git a/go/store/cmd/noms/splunk.pl b/go/store/cmd/noms/splunk.pl new file mode 100755 index 00000000000..1f2afacbe1b --- /dev/null +++ b/go/store/cmd/noms/splunk.pl @@ -0,0 +1,103 @@ +#!/usr/bin/perl + +my $noms_dir = '.dolt/noms'; + +if (! -d $noms_dir) { + die "Use splunk in a dolt directory"; +} + +if (! check_exists_command('noms')) { + die "noms binary not found on $PATH"; +} + +print "Welcome to the splunk shell for exploring dolt repository storage.\n"; + +my $manifest = `noms manifest $noms_dir`; +my $root = get_root($manifest); + +my $message = "Currently examining root.\nUse numeric labels to navigate the tree\n.. to back up a level, / to return to root.\nType quit or exit to exit.\n"; + +my $hash = $root; +my @stack = ($root); + +while (true) { + my $labels = print_show($hash); + + print $message if $message; + $message = ""; + + print "> "; + my $input = <>; + chomp $input; + if ($input eq "quit" or $input eq "exit") { + print "Bye\n"; + exit 0; + } + + if ($input eq "..") { + if (scalar @stack <= 1) { + $message = "At top level, cannot go back\n"; + next; + } + + shift @stack; + $hash = $stack[0]; + next; + } + + if ($input eq "/") { + $hash = $root; + @stack = ($root); + next; + } + + if (not defined $labels->{$input}) { + $message = "Invalid selection\nChoose a numeric label from the output above, or:\n.. to go back a level, / to go to the root\n"; + next; + } + + $hash = $labels->{$input}; + unshift @stack, $hash; +} + +sub show { + my $hash = shift; + my $cmd = "noms show $noms_dir\:\:#$hash"; + return `$cmd`; +} + +sub get_root { + my $manifest = shift; + for my $line (split /\n/, $manifest) { + next unless $line =~ /root/; + $line =~ /root:\s+([a-z0-9]{32})/; + return $1; + } + + die "couldn't determine root in $manifest"; +} + +sub print_show { + my $hash = shift; + + my %hashes; + my $label = 1; + + my $noms_show_output = show($hash); + for my $line (split /\n/, $noms_show_output) { + if ($line =~ /#([a-z0-9]{32})/) { + $hashes{$label} = $1; + print "$label) $line\n"; + $label++; + } else { + print " $line\n"; + } + } + + return \%hashes; +} + +sub check_exists_command { + my $check = `sh -c 'command -v $_[0]'`; + return $check; +}