Skip to content

Commit 06b525f

Browse files
committed
fix #5 info command
1 parent ad1dd5b commit 06b525f

File tree

3 files changed

+73
-8
lines changed

3 files changed

+73
-8
lines changed

src/clientcommands.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include "clientcommands.h"
1111
#include <QEventLoop>
1212
#include <QTimer>
13+
#include <QTextStream>
14+
1315
#include "merginuserauth.h"
1416

1517
ClientCommands::ClientCommands( const QString &dir, int timeout ):
@@ -106,6 +108,61 @@ void ClientCommands::sync()
106108
download( lp.projectNamespace, lp.projectName );
107109
}
108110

111+
static void printVal( bool isJsonFormat,
112+
QTextStream &out,
113+
const QString &key,
114+
const QString &val,
115+
bool isLast )
116+
{
117+
118+
QChar separator;
119+
QChar quote;
120+
QString space;
121+
if ( isJsonFormat )
122+
{
123+
separator = ',';
124+
quote = '"';
125+
}
126+
else
127+
{
128+
separator = '\n';
129+
space = " ";
130+
}
131+
132+
out << quote << key << quote << ":" << space << quote << val << quote
133+
;
134+
135+
if ( !isLast )
136+
out << separator;
137+
}
138+
139+
void ClientCommands::info( bool isJsonFormat )
140+
{
141+
Q_ASSERT( isAuthorized() );
142+
LocalProject lp = mLocalProjectsManager.projectFromDirectory( QDir::currentPath() );
143+
if ( !lp.isValid() )
144+
throw QString( "no mergin project in the current directory" );
145+
146+
QTextStream out( stdout );
147+
148+
QChar prefix;
149+
QChar suffix;
150+
if ( isJsonFormat )
151+
{
152+
suffix = '}';
153+
prefix = '{';
154+
} else {
155+
suffix = '\n';
156+
}
157+
158+
out << prefix;
159+
printVal( isJsonFormat, out, "name", lp.projectName, false );
160+
printVal( isJsonFormat, out, "namespace", lp.projectNamespace, false );
161+
printVal( isJsonFormat, out, "id", lp.id(), false );
162+
printVal( isJsonFormat, out, "localVersion", QString::number( lp.localVersion ), true );
163+
out << suffix;
164+
}
165+
109166

110167
bool ClientCommands::isAuthorized()
111168
{

src/clientcommands.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class ClientCommands: public QObject
3131
void remove( const QString &projectNamespace, const QString &projectName );
3232
void download( const QString &projectNamespace, const QString &projectName );
3333
void sync( );
34+
void info( bool isJsonFormat );
3435

3536
private:
3637
bool isAuthorized();

src/clientmain.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ struct Args
2626
QString projectName;
2727
QString dataDir;
2828
QString logFile;
29+
bool isJsonFormat;
2930
int timeout = 10000;
3031
};
3132

@@ -56,10 +57,12 @@ Args parseArgs()
5657
parser.setApplicationDescription( "Mergin Command Line Client" );
5758
QCommandLineOption helpOption = parser.addHelpOption();
5859
QCommandLineOption versionOption = parser.addVersionOption();
59-
QCommandLineOption logOption("log", "log file (debug output)", "log");
60-
parser.addOption(logOption);
60+
QCommandLineOption logOption( "log", "log file (debug output)", "log" );
61+
parser.addOption( logOption );
62+
QCommandLineOption jsonOption( "json", "output as JSON format (e.g. in info format)", "json" );
63+
parser.addOption( jsonOption );
6164

62-
parser.addPositionalArgument( "command", "create/remove/download/sync" );
65+
parser.addPositionalArgument( "command", "create/remove/download/sync/info" );
6366
parser.addPositionalArgument( "project", "namespace/projectname [only for create/remove/download]" );
6467

6568
QCommandLineOption urlOption( "url", "or use env. var MERGIN_URL. defaults to https://public.cloudmergin.com/", "url" );
@@ -82,11 +85,10 @@ Args parseArgs()
8285
args.user = parseEnvArg( parser.value( userOption ), "MERGIN_USER" );
8386
args.pass = parseEnvArg( parser.value( passwordOption ), "MERGIN_PASSWORD" );
8487
args.logFile = parser.value( logOption );
88+
args.isJsonFormat = parser.isSet( jsonOption );
8589

86-
87-
88-
if (!args.logFile.isEmpty())
89-
CoreUtils::setLogFilename(args.logFile);
90+
if ( !args.logFile.isEmpty() )
91+
CoreUtils::setLogFilename( args.logFile );
9092

9193
args.command = posArgs.at( 0 );
9294
if ( args.command == "create" ||
@@ -105,7 +107,8 @@ Args parseArgs()
105107
args.dataDir = QDir::currentPath();
106108

107109
}
108-
else if ( args.command == "sync" )
110+
else if ( args.command == "sync" ||
111+
args.command == "info" )
109112
{
110113
QDir current = QDir::current();
111114
current.cdUp();
@@ -157,6 +160,10 @@ int main( int argc, char *argv[] )
157160
{
158161
cmd.sync();
159162
}
163+
else if ( args.command == "info" )
164+
{
165+
cmd.info( args.isJsonFormat );
166+
}
160167
else
161168
{
162169
throw QString( "invalid command" ) + args.command;

0 commit comments

Comments
 (0)