1414#include <unistd.h>
1515#include <jansson.h>
1616#include <stdio.h>
17+ #include <getopt.h>
1718#include <flux/core.h>
1819
1920#include "src/common/libutil/log.h"
21+ #include "ccan/str/str.h"
22+
23+ #define OPTIONS "j"
24+ static const struct option longopts [] = {
25+ {"json-decode" , no_argument , 0 , 'j' },
26+ { 0 , 0 , 0 , 0 },
27+ };
28+
29+ static void usage (void )
30+ {
31+ fprintf (stderr , "Usage: info_lookup [--json-decode] <jobid> <key> ...\n" );
32+ exit (1 );
33+ }
2034
2135int main (int argc , char * argv [])
2236{
2337 flux_t * h ;
2438 flux_future_t * f ;
2539 flux_jobid_t id ;
2640 json_t * keys ;
27- int i ;
41+ int i , ch ;
42+ int flags = 0 ;
2843
29- if (argc < 3 ) {
30- fprintf (stderr , "Usage: info_lookup <jobid> <key> ...\n" );
31- exit (1 );
44+ while ((ch = getopt_long (argc , argv , OPTIONS , longopts , NULL )) != -1 ) {
45+ switch (ch ) {
46+ case 'j' : /* --json-decode */
47+ flags |= FLUX_JOB_LOOKUP_JSON_DECODE ;
48+ break ;
49+ default :
50+ usage ();
51+ break ;
52+ }
3253 }
54+ if ((argc - optind ) < 2 )
55+ usage ();
3356
3457 if (!(h = flux_open (NULL , 0 )))
3558 log_err_exit ("flux_open" );
3659
37- if (flux_job_id_parse (argv [1 ], & id ) < 0 )
60+ if (flux_job_id_parse (argv [optind ++ ], & id ) < 0 )
3861 log_msg_exit ("error parsing jobid: %s" , argv [1 ]);
3962
4063 if (!(keys = json_array ()))
4164 log_msg_exit ("json_array" );
4265
43- for (i = 2 ; i < argc ; i ++ ) {
66+ for (i = optind ; i < argc ; i ++ ) {
4467 json_t * key = json_string (argv [i ]);
4568 if (!key )
4669 log_msg_exit ("json_string_value" );
@@ -55,20 +78,39 @@ int main (int argc, char *argv[])
5578 "{s:I s:O s:i}" ,
5679 "id" , id ,
5780 "keys" , keys ,
58- "flags" , 0 )))
81+ "flags" , flags )))
5982 log_err_exit ("flux_rpc_pack" );
6083
61- for (i = 2 ; i < argc ; i ++ ) {
62- json_t * value ;
63- char * s ;
64- if (flux_rpc_get_unpack (f , "{s:o}" , argv [i ], & value ) < 0 )
65- log_msg_exit ("job-info.lookup: %s" ,
66- future_strerror (f , errno ));
67- if (!(s = json_dumps (value , JSON_ENCODE_ANY )))
68- log_msg_exit ("invalid json result" );
69- printf ("%s\n" , s );
84+ for (i = optind ; i < argc ; i ++ ) {
85+ if (flags & FLUX_JOB_LOOKUP_JSON_DECODE ) {
86+ json_t * value ;
87+ char * s ;
88+ if (flux_rpc_get_unpack (f , "{s:o}" , argv [i ], & value ) < 0 )
89+ log_msg_exit ("job-info.lookup: %s" ,
90+ future_strerror (f , errno ));
91+ if (streq (argv [i ], "jobspec" ) || streq (argv [i ], "R" )) {
92+ if (!json_is_object (value ))
93+ log_msg_exit ("job-info.lookup: key %s not an object" ,
94+ argv [i ]);
95+ }
96+ else {
97+ if (!json_is_string (value ))
98+ log_msg_exit ("job-info.lookup: key %s not a string" ,
99+ argv [i ]);
100+ }
101+ if (!(s = json_dumps (value , JSON_ENCODE_ANY )))
102+ log_msg_exit ("invalid json result" );
103+ printf ("%s\n" , s );
104+ free (s );
105+ }
106+ else {
107+ char * s ;
108+ if (flux_rpc_get_unpack (f , "{s:s}" , argv [i ], & s ) < 0 )
109+ log_msg_exit ("job-info.lookup: %s" ,
110+ future_strerror (f , errno ));
111+ printf ("%s\n" , s );
112+ }
70113 fflush (stdout );
71- free (s );
72114 }
73115
74116 flux_future_destroy (f );
0 commit comments