Skip to content

Commit

Permalink
Merge pull request #170 from WcaleNieWolny/metadata_channel_set_and_c…
Browse files Browse the repository at this point in the history
…urrent_channel
  • Loading branch information
riderx authored Oct 12, 2023
2 parents 79b563e + ae212e2 commit 812b962
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 7 deletions.
74 changes: 74 additions & 0 deletions src/channel/currentBundle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { program } from "commander";
import * as p from '@clack/prompts';
import { checkAppExistsAndHasPermissionErr } from "../api/app";
import { OptionsBase } from "../api/utils";
import { createSupabaseClient, findSavedKey, getConfig, verifyUser } from "../utils";


interface Options extends OptionsBase {
channel?: string,
quiet?: boolean
}

interface Channel {
version: {
name: string;
}
}

export const currentBundle = async (channel: string, appId: string, options: Options) => {
const { quiet } = options

if (!quiet)
p.intro(`List current bundle`);


options.apikey = options.apikey || findSavedKey(quiet)
const config = await getConfig();
appId = appId || config?.app?.appId

if (!options.apikey) {
p.log.error("Missing API key, you need to provide a API key to upload your bundle");
program.error('');
}
if (!appId) {
p.log.error("Missing argument, you need to provide a appId, or be in a capacitor project");
program.error('');
}
const supabase = createSupabaseClient(options.apikey)

const userId = await verifyUser(supabase, options.apikey, ['write', 'all', 'read']);
// Check we have app access to this appId
await checkAppExistsAndHasPermissionErr(supabase, appId);

if (!channel) {
p.log.error(`Please provide a channel to get the bundle from.`);
program.error('');
}

const { data: supabaseChannel, error } = await supabase
.from('channels')
.select('version ( name )')
.eq('name', channel)
.eq('app_id', appId)
.eq('created_by', userId)
.limit(1)

if (error || supabaseChannel.length === 0) {
p.log.error(`Error retrieving channel ${channel} for app ${appId}. Perhaps the channel does not exists?`);
program.error('');
}

const { version } = supabaseChannel[0] as any as Channel
if (!version) {
p.log.error(`Error retrieving channel ${channel} for app ${appId}. Perhaps the channel does not exists?`);
program.error('');
}

if (!quiet)
p.log.info(`Current bundle for channel ${channel} is ${version.name}`)
else
console.log(version.name)

process.exit()
}
29 changes: 26 additions & 3 deletions src/channel/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ interface Options extends OptionsBase {
upgrade?: boolean;
ios?: boolean;
android?: boolean;
selfAssign?: boolean;
selfAssign?: boolean,
disableAutoUpdate: string,
channel?: string;
}

const disableAutoUpdatesPossibleOptions = ['major', 'minor', 'metadata', 'none']

export const setChannel = async (channel: string, appId: string, options: Options) => {
p.intro(`Set channel`);
options.apikey = options.apikey || findSavedKey()
Expand All @@ -41,7 +44,7 @@ export const setChannel = async (channel: string, appId: string, options: Option
// Check we have app access to this appId
await checkAppExistsAndHasPermissionErr(supabase, appId);

const { bundle, latest, downgrade, upgrade, ios, android, selfAssign, state } = options;
const { bundle, latest, downgrade, upgrade, ios, android, selfAssign, state, disableAutoUpdate } = options;
if (!channel) {
p.log.error("Missing argument, you need to provide a channel");
program.error('');
Expand All @@ -57,7 +60,8 @@ export const setChannel = async (channel: string, appId: string, options: Option
upgrade == null &&
ios == null &&
android == null &&
selfAssign == null) {
selfAssign == null &&
disableAutoUpdate == null) {
p.log.error("Missing argument, you need to provide a option to set");
program.error('');
}
Expand Down Expand Up @@ -111,6 +115,25 @@ export const setChannel = async (channel: string, appId: string, options: Option
p.log.info(`Set ${appId} channel: ${channel} to ${selfAssign ? 'allow' : 'disallow'} self assign to this channel`);
channelPayload.allow_device_self_set = !!selfAssign
}
if (disableAutoUpdate != null) {
let finalDisableAutoUpdate = disableAutoUpdate.toLocaleLowerCase()

// The user passed an unimplemented strategy
if (!disableAutoUpdatesPossibleOptions.includes(finalDisableAutoUpdate)) {
// eslint-disable-next-line max-len
p.log.error(`Channel strategy ${finalDisableAutoUpdate} is not known. The possible values are: ${disableAutoUpdatesPossibleOptions.join(', ')}.`);
program.error('');
}

// This metadata is called differently in the database
if (finalDisableAutoUpdate === 'metadata') {
finalDisableAutoUpdate = 'version_number'
}

// This cast is safe, look above
channelPayload.disableAutoUpdate = finalDisableAutoUpdate as any
p.log.info(`Set ${appId} channel: ${channel} to ${finalDisableAutoUpdate} disable update strategy to this channel`);
}
try {
const { error: dbError } = await updateOrCreateChannel(supabase, channelPayload)
if (dbError) {
Expand Down
14 changes: 13 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getInfo } from './app/info';
import { saveKeyCommand, createKeyCommand } from './key';
import { deleteBundle } from './bundle/delete';
import { setChannel } from './channel/set';
import { currentBundle } from './channel/currentBundle';
import { uploadCommand, uploadDeprecatedCommand } from './bundle/upload';
import pack from '../package.json'
import { loginCommand } from './login';
Expand Down Expand Up @@ -203,6 +204,14 @@ channel
.description('List channel')
.action(listChannels)

channel
.command('currentBundle [channel] [appId]')
.description('Get current bundle for specific channel in Capgo Cloud')
.action(currentBundle)
.option('-c, --channel <channel>', 'channel to get the current bundle from')
.option('-a, --apikey <apikey>', 'apikey to link to your account')
.option('--quiet', 'only print the bundle version')

channel
.command('set [channelId] [appId]')
.alias('s')
Expand All @@ -221,7 +230,10 @@ channel
.option('--android', 'Allow sending update to android devices')
.option('--no-android', 'Disable sending update to android devices')
.option('--self-assign', 'Allow to device to self assign to this channel')
.option('--no-self-assign', 'Disable devices to self assign to this channel');
.option('--no-self-assign', 'Disable devices to self assign to this channel')
.option('--disable-auto-update <disableAutoUpdate>',
'Disable auto update strategy for this channel.The possible options are: major, minor, metadata, none'
)

const key = program
.command('key')
Expand Down
8 changes: 5 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,20 @@ export const checkPlanValid = async (supabase: SupabaseClient<Database>, userId:
}
}

export const findSavedKey = () => {
export const findSavedKey = (quiet = false) => {
// search for key in home dir
const userHomeDir = homedir();
let key
let keyPath = `${userHomeDir}/.capgo`;
if (existsSync(keyPath)) {
p.log.info(`Use global apy key ${keyPath}`)
if (!quiet)
p.log.info(`Use global apy key ${keyPath}`)
key = readFileSync(keyPath, 'utf8').trim();
}
keyPath = `.capgo`;
if (!key && existsSync(keyPath)) {
p.log.info(`Use local apy key ${keyPath}`)
if (!quiet)
p.log.info(`Use local apy key ${keyPath}`)
key = readFileSync(keyPath, 'utf8').trim();
}
if (!key)
Expand Down

0 comments on commit 812b962

Please sign in to comment.