|
| 1 | +#!/usr/bin/env node |
| 2 | +let json = require('format-json'); |
| 3 | +let program = require('commander'); |
| 4 | +let LaunchDarklyUtils = require('./dist/src/LaunchDarklyUtils.js').LaunchDarklyUtils; |
| 5 | +require('dotenv').config(); |
| 6 | + |
| 7 | +if (!process.env.LAUNCHDARKLY_API_TOKEN) { |
| 8 | + console.log('missing environment var LAUNCHDARKLY_API_TOKEN - exiting..'); |
| 9 | + process.exit(1); |
| 10 | +} |
| 11 | + |
| 12 | +let log = console; |
| 13 | +if (process.env.LAUNCHDARKLY_API_LOGLEVEL === 'debug') log = undefined; |
| 14 | +new LaunchDarklyUtils().create(process.env.LAUNCHDARKLY_API_TOKEN, log).then(function(ldUtils) { |
| 15 | + |
| 16 | + program |
| 17 | + .version('0.1.0'); |
| 18 | + |
| 19 | + program |
| 20 | + .command('getFeatureFlags <projectKey>') |
| 21 | + .description('get all flags for a given project') |
| 22 | + .action(function(projectKey) { |
| 23 | + ldUtils.flags.getFeatureFlags(projectKey).then(function(response) { |
| 24 | + console.log(json.plain(response)); |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + program |
| 29 | + .command(`getFeatureFlag <projectKey> <featureFlagKey> [environmentKeyQuery]`) |
| 30 | + .description('get a single flag - with optional env') |
| 31 | + .action(function(projectKey, featureFlagKey) { |
| 32 | + ldUtils.flags.getFeatureFlag(projectKey, featureFlagKey).then(function(response) { |
| 33 | + console.log(json.plain(response)); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + program |
| 38 | + .command(`getFeatureFlagState <projectKey> <featureFlagKey> <environmentKeyQuery>`) |
| 39 | + .description('get boolean flag state') |
| 40 | + .action(function(projectKey, featureFlagKey, environmentKeyQuery) { |
| 41 | + ldUtils.flags.getFeatureFlagState(projectKey, featureFlagKey, environmentKeyQuery).then(function(response) { |
| 42 | + console.log(json.plain(response)); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + program |
| 47 | + .command(`toggleFeatureFlag <projectKey> <featureFlagKey> <environmentKeyQuery> <value>`) |
| 48 | + .description('set boolean flag state') |
| 49 | + .action(function(projectKey, featureFlagKey, environmentKeyQuery, value) { |
| 50 | + let enabled = value === 'true'; |
| 51 | + ldUtils.flags.toggleFeatureFlag(projectKey, featureFlagKey, environmentKeyQuery, enabled).then(function(response) { |
| 52 | + console.log(json.plain(response)); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + program |
| 57 | + .command('getCustomRoles') |
| 58 | + .description('get all custom roles in account') |
| 59 | + .action(function() { |
| 60 | + ldUtils.roles.getCustomRoles().then(function(response) { |
| 61 | + console.log(json.plain(response)); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + program |
| 66 | + .command('getCustomRole <customRoleKey>') |
| 67 | + .description('get custom role by key') |
| 68 | + .action(function(customRoleKey) { |
| 69 | + ldUtils.roles.getCustomRole(customRoleKey).then(function(response) { |
| 70 | + console.log(json.plain(response)); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + program |
| 75 | + .command(`createCustomRole <customRoleKey> <customRoleName> <customRolePolicyArray> [customRoleDescription]`) |
| 76 | + .description('create custom role') |
| 77 | + .action(function(customRoleKey, customRoleName, customRolePolicyArray, customRoleDescription) { |
| 78 | + customRolePolicyArray = JSON.parse(customRolePolicyArray); |
| 79 | + ldUtils.roles.createCustomRole(customRoleKey, customRoleName, customRolePolicyArray, customRoleDescription).then(function(response) { |
| 80 | + console.log(json.plain(response)); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + program |
| 85 | + .command(`updateCustomRole <customRoleKey> <customRoleName> <customRolePolicyArray> [customRoleDescription]`) |
| 86 | + .description('update custom role') |
| 87 | + .action(function(customRoleKey, customRoleName, customRolePolicyArray, customRoleDescription) { |
| 88 | + customRolePolicyArray = JSON.parse(customRolePolicyArray); |
| 89 | + ldUtils.roles.updateCustomRole(customRoleKey, customRoleName, customRolePolicyArray, customRoleDescription).then(function(response) { |
| 90 | + console.log(json.plain(response)); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + program |
| 95 | + .command(`upsertCustomRole <customRoleKey> <customRoleName> <customRolePolicyArray> [customRoleDescription]`) |
| 96 | + .description('create or update custom role') |
| 97 | + .action(function(customRoleKey, customRoleName, customRolePolicyArray, customRoleDescription) { |
| 98 | + customRolePolicyArray = JSON.parse(customRolePolicyArray); |
| 99 | + ldUtils.roles.upsertCustomRole(customRoleKey, customRoleName, customRolePolicyArray, customRoleDescription).then(function(response) { |
| 100 | + console.log(json.plain(response)); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + program |
| 105 | + .command('bulkUpsertCustomRoles <roleBulkLoadFile>') |
| 106 | + .description('path to a file containing array of role json') |
| 107 | + .action(function(roleBulkLoadFile) { |
| 108 | + ldUtils.roles.bulkUpsertCustomRoles(roleBulkLoadFile).then(function(response) { |
| 109 | + console.log(json.plain(response)); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + program |
| 114 | + .command('bulkUpsertCustomRoleFolder <roleFolder>') |
| 115 | + .description('path to a directory containing multiple files of role json') |
| 116 | + .action(function(roleFolder) { |
| 117 | + ldUtils.roles.bulkUpsertCustomRoleFolder(roleFolder).then(function(response) { |
| 118 | + console.log(json.plain(response)); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + console.log(process.argv); |
| 123 | + if (process.argv.length < 3) { |
| 124 | + program.help(); |
| 125 | + process.exit(1); |
| 126 | + } |
| 127 | + |
| 128 | + program |
| 129 | + .parse(process.argv); |
| 130 | + |
| 131 | +}) |
| 132 | +.catch(function(e) { |
| 133 | + console.log(json.plain(e)); |
| 134 | +}); |
| 135 | + |
0 commit comments