1 #define INCLUDE_TESTSETUP_WITHOUT_BOOST
2 #include "../tests/lib/TestSetup.h"
3 #undef INCLUDE_TESTSETUP_WITHOUT_BOOST
7 #include <boost/program_options.hpp>
8 namespace opt = boost::program_options;
10 #include <zypp/target/rpm/RpmDb.h>
11 #include <zypp/base/String.h>
13 static std::string appname( "unknown" );
15 int errexit( const std::string & msg_r = std::string(), int exit_r = 100 )
17 if ( ! msg_r.empty() )
19 cerr << endl << msg_r << endl << endl;
24 bool byTTL( const PublicKey & lhs, const PublicKey & rhs )
26 int cmp = lhs.gpgPubkeyVersion().compare( rhs.gpgPubkeyVersion() );
27 if ( cmp ) return cmp < 0;
28 return lhs.gpgPubkeyRelease() > rhs.gpgPubkeyRelease(); // intentionally reverse cdate
31 std::ostream & dumpPubkeyOn( std::ostream & str, const PublicKey & key_r )
33 std::vector<std::string> art( key_r.asciiArt().asLines( " ", PublicKey::AsciiArt::USE_COLOR ) );
35 std::vector<std::string> info;
36 str::split( (str::Str() << dump(key_r)).str(), std::back_inserter( info ), "\n" );
38 for ( unsigned i = 1; i < info.size(); ++i )
41 str << info[0] << endl;
42 for ( const auto & line : art )
47 /******************************************************************
49 ** FUNCTION NAME : main
50 ** FUNCTION TYPE : int
52 int main( int argc, char * argv[] )
54 appname = Pathname::basename( argv[0] );
55 ///////////////////////////////////////////////////////////////////
57 opt::options_description options( "Options" );
59 ( "key-file", opt::value<std::vector<std::string> >(),
60 "ASCII ascii armored public key file")
61 ( "root", opt::value<std::string>()->default_value( "/" ),
62 "Use the rmp database from system rooted at ARG")
63 ( "help,?", "Produce this help message")
66 opt::positional_options_description positional;
67 positional.add( "key-file", -1 );
69 opt::variables_map vm;
70 opt::store( opt::command_line_parser( argc, argv ).options( options ).positional( positional ).run(), vm );
73 if ( vm.count( "help" ) )
75 cerr << "Usage: " << appname << " [OPTIONS] [key-files...]" << endl;
76 cerr << "If no key files are given, list info about all gpg-pubkeys in the rpm database." << endl;
77 cerr << "Otherwise print info about each key and wheter it is present in the rpm database. " << endl;
78 cerr << options << endl;
81 ///////////////////////////////////////////////////////////////////
83 if ( ! PathInfo( vm["root"].as<std::string>() ).isDir() )
84 return errexit("--root requires a directory");
86 target::rpm::RpmDb rpmdb;
87 rpmdb.initDatabase( vm["root"].as<std::string>() );
88 std::list<PublicKey> rpmpubkeys( rpmdb.pubkeys() );
89 rpmpubkeys.sort( byTTL );
93 if ( ! vm.count( "key-file" ) )
96 for_each_( it, rpmpubkeys )
98 if ( last == it->gpgPubkeyVersion() )
102 dumpPubkeyOn( cout, *it );
103 last = it->gpgPubkeyVersion();
109 ///////////////////////////////////////////////////////////////////
111 for_each_( it, vm["key-file"].as< std::vector<std::string> >() )
113 cout << "=== " << PathInfo(*it) << endl;
114 PublicKey pubkey( *it );
115 dumpPubkeyOn( cout, pubkey );
117 std::string pubkeyV( pubkey.gpgPubkeyVersion() );
118 std::string pubkeyR( pubkey.gpgPubkeyRelease() );
120 for_each_( rpmpub, rpmpubkeys )
122 if ( rpmpub->gpgPubkeyVersion() == pubkeyV )
124 int cmp = rpmpub->gpgPubkeyRelease().compare( pubkeyR );
134 cout << "gpg-pubkey-" << rpmpub->gpgPubkeyVersion() << "-" << rpmpub->gpgPubkeyRelease() << " " << rpmpub->daysToLive() << endl;
139 cout << "*** Not in rpm database." << endl;
144 ///////////////////////////////////////////////////////////////////