mirror of
https://codeberg.org/scip/note.git
synced 2025-12-17 12:41:10 +01:00
added JSON export format:
Starting with version 1.4 JSON export format will be supported but not be enabled by default. However a deprecation notice will appear if a user still uses YAML format. YAML support will be removed in version 1.5. Fixes https://github.com/TLINDEN/note/issues/10.
This commit is contained in:
41
bin/note
41
bin/note
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/perl
|
||||
#
|
||||
# note - console notes management with database and encryption support.
|
||||
# Copyright (C) 1999-2017 T.v.Dein (see README for details!)
|
||||
# Copyright (C) 1999-2023 T.v.Dein (see README for details!)
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU General Public License
|
||||
@@ -38,6 +38,7 @@ use Getopt::Long;
|
||||
use FileHandle;
|
||||
use File::Spec;
|
||||
use YAML;
|
||||
use JSON::PP;
|
||||
|
||||
|
||||
#
|
||||
@@ -71,6 +72,7 @@ my (
|
||||
$opt_, $opt_i, $opt_r, $opt_e, $opt_d, $opt_enc,
|
||||
$opt_s, $opt_t, $opt_T, $opt_l, $opt_L, $opt_c,
|
||||
$opt_D, $opt_I, $opt_o, $opt_h, $opt_n, $opt_v,
|
||||
$opt_j,
|
||||
|
||||
#
|
||||
# set from commandline (or interactive)
|
||||
@@ -133,7 +135,8 @@ my (
|
||||
'printlines' => 0,
|
||||
'cache' => 0,
|
||||
'preferrededitor' => '',
|
||||
'motd' => ''
|
||||
'motd' => '',
|
||||
'usejson' => 0, # will be the default in the future
|
||||
);
|
||||
|
||||
# these are not customizable at runtime!
|
||||
@@ -141,7 +144,7 @@ $hardparams = "(readonly|maxlen|dbdriver|useencryption|cryptmethod)";
|
||||
$CONF = File::Spec->catfile($ENV{HOME}, ".noterc");
|
||||
$USER = getlogin || getpwuid($<); chomp $USER;
|
||||
$TOPIC = 1;
|
||||
$VERSION = "1.3.26";
|
||||
$VERSION = "1.4.0";
|
||||
$CurDepth = 1; # the current depth inside the topic "directory" structure...
|
||||
$maxlen = "auto";
|
||||
$timelen = 22;
|
||||
@@ -214,6 +217,7 @@ else {
|
||||
"list|l:s" => \$opt_l, # string, optional
|
||||
"longlist|L:s" => \$opt_L, # string, optional
|
||||
"dump|Dump|D:s" => \$opt_D, # string, optional
|
||||
"json|j" => \$opt_j, # bool, optional
|
||||
"import|Import|I:s" => \$opt_I, # string, optional
|
||||
"overwrite|o!" => \$opt_o, # no arg
|
||||
"help|h|?!" => \$opt_h, # no arg
|
||||
@@ -276,6 +280,10 @@ else {
|
||||
else {
|
||||
$dump_file = "-"; # use STDIN
|
||||
}
|
||||
|
||||
if (defined $opt_j) {
|
||||
$conf{usejson} = 1; # force JSON
|
||||
}
|
||||
}
|
||||
elsif (defined $opt_I) {
|
||||
$mode = "import";
|
||||
@@ -923,6 +931,7 @@ sub edit {
|
||||
|
||||
sub dump {
|
||||
my(%res, $num, $DUMP);
|
||||
|
||||
# $dump_file
|
||||
if ($dump_file eq "-") {
|
||||
$DUMP = *STDOUT;
|
||||
@@ -931,8 +940,11 @@ sub dump {
|
||||
open (DUMPFILE, ">$dump_file") or die "could not open $dump_file\n";
|
||||
$DUMP = *DUMPFILE;
|
||||
}
|
||||
|
||||
select $DUMP;
|
||||
|
||||
%res = $db->get_all();
|
||||
|
||||
# FIXME: prepare hashing in NOTEDB class
|
||||
foreach $num (sort { $a <=> $b } keys %res) {
|
||||
print STDOUT "dumping note number $num to $dump_file\n" if($dump_file ne "-");
|
||||
@@ -947,7 +959,17 @@ sub dump {
|
||||
my $date = $res{$num}->{date};
|
||||
$res{$num} = { body => $body, title => $title, path => $path, date => $date};
|
||||
}
|
||||
print Dump(\%res);
|
||||
|
||||
if($conf{usejson}) {
|
||||
my $json = JSON::PP->new->utf8->pretty;
|
||||
print $json->encode(\%res);
|
||||
}
|
||||
else {
|
||||
warn "Deprecation notice: YAML export format will not be supported in the future!
|
||||
Enable JSON using the UseJSON config parameter or the -j commandline parameter!";
|
||||
print Dump(\%res);
|
||||
}
|
||||
|
||||
close(DUMPFILE);
|
||||
select STDOUT;
|
||||
}
|
||||
@@ -964,9 +986,16 @@ sub import {
|
||||
$DUMP = *DUMPFILE;
|
||||
}
|
||||
|
||||
my $yaml = join '', <$DUMP>;
|
||||
my $serialized = join '', <$DUMP>;
|
||||
|
||||
my $res = Load($yaml);
|
||||
my $res;
|
||||
|
||||
if($serialized =~ /^\{/) {
|
||||
$res = decode_json($serialized);
|
||||
}
|
||||
else {
|
||||
$res = Load($serialized);
|
||||
}
|
||||
|
||||
foreach my $number (keys %{$res}) {
|
||||
my $note;
|
||||
|
||||
Reference in New Issue
Block a user