@@ -4,6 +4,7 @@ import { info, printJson, handleApproval, renderNextActions } from '../util.js'
44
55export const SERVICE_TYPES = [ 'postgres' , 'storage' , 'compute' ] as const
66export type ServiceType = ( typeof SERVICE_TYPES ) [ number ]
7+ const SERVICE_NAME_RE = / ^ [ a - z 0 - 9 ] [ a - z 0 - 9 - ] { 0 , 38 } $ /
78
89export function q ( branch ?: string ) : string {
910 return branch ? `?branch=${ encodeURIComponent ( branch ) } ` : ''
@@ -16,6 +17,10 @@ export function assertType(type: string, allowed: readonly string[] = SERVICE_TY
1617 if ( ! allowed . includes ( type ) ) throw new Error ( `type must be ${ allowed . join ( '|' ) } ` )
1718}
1819
20+ export function assertServiceName ( name : string ) : void {
21+ if ( ! SERVICE_NAME_RE . test ( name ) ) throw new Error ( 'service name must be lower-kebab (a-z, 0-9, -)' )
22+ }
23+
1924// Parse a positive-integer machine count.
2025export function parseCount ( raw : string ) : number {
2126 const n = Number ( raw )
@@ -107,6 +112,20 @@ export async function servicesRemove(type: string, name: string, opts: { branch?
107112 info ( `removed ${ type } service ${ name } from ${ branch ?? 'default' } ` )
108113}
109114
115+ export async function servicesRename ( type : string , name : string , newName : string , opts : { branch ?: string ; json ?: boolean } = { } ) : Promise < void > {
116+ assertType ( type )
117+ assertServiceName ( newName )
118+ const api = await ApiClient . load ( )
119+ const p = await requireProject ( )
120+ const branch = opts . branch ?? p . branch
121+ const { services } = await api . request ( 'GET' , `/projects/${ p . projectId } /services${ q ( branch ) } ` )
122+ const id = resolveServiceId ( services , type , name )
123+ const res = await api . rawRequest ( 'POST' , `/projects/${ p . projectId } /services/${ id } /rename` , { name : newName } )
124+ if ( handleApproval ( res ) ) return
125+ if ( opts . json ) return printJson ( res . body . service )
126+ info ( `renamed ${ type } service ${ name } to ${ newName } ` )
127+ }
128+
110129// Validate a bucket access-mode argument.
111130export function parseAccess ( raw : string ) : boolean {
112131 if ( raw === 'public' ) return true
0 commit comments