REST Service for POPCORN - ILIAS
alex
2025-11-28 a380a465ce02059a630ef678fecd6666cbdf8f30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const yargs = require("yargs")
const csv = require("json-2-csv")
 
const db = require("../lib/db")
const {exitDelayed} = require("../lib/tools");
 
/////////////////////////////////////////////////////////////////////////
 
const argv = yargs
   .command({
      command: "list",
      describe: "list course admin roles",
      builder: {
         format: {
            alias: "f",
            demandOption: false,
            describe: "output format: table(default) | json | csv",
            type: "string",
            default: "table"
         }
      },
      handler: async function ({format}) {
         const data = await db.getCourseAdminRoles()
         await print(data, format)
         exitDelayed()
      }
   })
   .command({
      command: "listn",
      describe: "list courses without user assigned admin role",
      builder: {
         format: {
            alias: "f",
            demandOption: false,
            describe: "output format: table(default) | json | csv",
            type: "string",
            default: "table"
         }
      },
      handler: async function ({format}) {
         const data = await db.getCourseWithoutAdminRoles()
         await print(data, format)
         exitDelayed()
      }
   })
   // .command({
   //    command: "get <item> [_id]",
   //    describe: "get db items - if _id is given, get only one document identified by _id",
   //    builder: {
   //       skip: {
   //          demandOption: false,
   //          describe: "skip this number of items",
   //          type: "number",
   //          default: 0,
   //       },
   //       limit: {
   //          demandOption: false,
   //          describe: "limit to this number of items",
   //          type: "number",
   //          default: 5,
   //       },
   //    },
   //    handler: async function ({item, _id, skip, limit}) {
   //       const db = require("../data/db/db")
   //       if (!_id) {
   //          const res = await db[item].find().skip(skip).limit(limit).lean()
   //          console.log(JSON.stringify(res, null, "   "))
   //       } else {
   //          const res = await db[item].findOne({_id}).lean()
   //          console.log(JSON.stringify(res, null, "   "))
   //       }
   //       exitDelayed()
   //    }
   // })
   .alias("h", "help")
   .demandCommand()
   .version(false)
   // .wrap(yargs.terminalWidth())
   // .wrap(100)
   .strict()
   .argv
 
 
/////////////////////////////////////////////////////////////////////////
 
async function print(data, format) {
   switch (format) {
      case "table":
         console.table(data)
         break;
      case "json":
         console.log(JSON.stringify(data, null, "   "))
         break;
      case "csv":
         const res = await csv.json2csv(data)
         console.log(res)
         break;
      default:
         throw new Error(`unknown format "${format}"`)
   }
}