1- const debug = require ( 'debug' ) ( 'codefresh:auth:login' ) ;
21const Command = require ( '../../Command' ) ;
3- const _ = require ( 'lodash' ) ;
4- const CFError = require ( 'cf-errors' ) ;
52const DEFAULTS = require ( '../../defaults' ) ;
6- const { auth } = require ( '../../../../logic' ) ;
7- const { JWTContext, APIKeyContext } = auth . contexts ;
8- const authManager = auth . manager ;
3+ const { Config } = require ( 'codefresh-sdk' ) ;
94const authRoot = require ( '../root/auth.cmd' ) ;
10- const { createContext } = require ( '../../../../logic/api/auth' ) ;
11-
12- const _loginWithToken = async ( url , token ) => {
13- let authContext ;
14- try {
15- authContext = JWTContext . createFromToken ( token , url ) ;
16- return authContext ;
17-
18- } catch ( err ) {
19- try {
20- authContext = APIKeyContext . createFromToken ( token , url ) ;
21- return authContext ;
22-
23- } catch ( err ) {
24- const error = new CFError ( {
25- cause : err ,
26- message : 'Failed to login with api key' ,
27- } ) ;
28- throw error ;
29- }
30- }
31- } ;
325
336const command = new Command ( {
347 command : 'create-context [name]' ,
@@ -40,7 +13,7 @@ const command = new Command({
4013 title : 'Create Context' ,
4114 weight : 20 ,
4215 } ,
43- builder : ( yargs ) => {
16+ builder : ( yargs ) => { // eslint-disable-line
4417 return yargs
4518 . option ( 'url' , {
4619 describe : 'Codefresh system custom url' ,
@@ -56,38 +29,25 @@ const command = new Command({
5629 } )
5730 . example ( 'codefresh auth create-context --api-key KEY' , 'Creating a default context using KEY as the api-key' )
5831 . example ( 'codefresh auth create-context my-context --api-key KEY' , 'Creating a named context' ) ;
59-
6032 } ,
6133 handler : async ( argv ) => {
62- const authContext = await _loginWithToken ( argv . url , argv [ 'api-key' ] ) ;
63- const userData = await authContext . validate ( ) ;
64- const roles = _ . get ( userData , 'roles' ) ;
34+ const configManager = Config . manager ( ) ;
35+ await configManager . loadConfig ( { configFilePath : argv . cfconfig } ) ;
6536
66- if ( roles . indexOf ( 'Admin' ) > - 1 ) {
67- authContext . onPrem = true ;
68- }
69-
70- if ( argv . name ) {
71- authContext . setName ( argv . name ) ;
72- }
73-
74- let updatedExistingContext = false ;
75- if ( authManager . getContextByName ( authContext . getName ( ) ) ) {
76- updatedExistingContext = true ;
77- }
37+ const updatedExistingContext = configManager . getContextByName ( argv . name ) ;
7838
79- await authManager . addContext ( authContext ) ;
80- await authManager . setCurrentContext ( authContext ) ;
81- await authManager . persistContexts ( authContext ) ;
82- await createContext ( ) ;
39+ const context = await configManager . createContext ( { apiKey : argv . apiKey , url : argv . url , name : argv . name } ) ;
40+ configManager . addContext ( context ) ;
41+ configManager . setCurrentContext ( context ) ;
42+ await configManager . persistConfig ( ) ;
8343
8444 if ( updatedExistingContext ) {
85- console . log ( `Updated context: ${ authContext . name } ` ) ;
45+ console . log ( `Updated context: ${ context . name } ` ) ;
8646 } else {
87- console . log ( `Created new context: ${ authContext . name } ` ) ;
47+ console . log ( `Created new context: ${ context . name } ` ) ;
8848 }
8949
90- console . log ( `Switched to context: ${ authContext . name } ` ) ;
50+ console . log ( `Switched to context: ${ context . name } ` ) ;
9151 } ,
9252} ) ;
9353
0 commit comments