Skip to content

Commit

Permalink
fixup! BugZilla 518578 : Let users to use .Chefile in addition to Che…
Browse files Browse the repository at this point in the history
…file. It allows to use a hidden file format

Change-Id: Ia598346149941fe9d8338498e6a07dd3283d8430
Signed-off-by: Florent BENOIT <fbenoit@redhat.com>
  • Loading branch information
benoitf committed Jul 18, 2017
1 parent 4368c3c commit ee0f0ea
Showing 1 changed file with 67 additions and 43 deletions.
110 changes: 67 additions & 43 deletions dockerfiles/lib/src/internal/dir/che-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ export class CheDir {
*/
dotCheFile: string;

/**
* Alternate path to the Chefile file with .chefile
*/
dotCheFileLowercase: string;

dotCheFolder : any;
workspacesFolder : any;
cliFolder : any;
Expand Down Expand Up @@ -107,6 +112,7 @@ export class CheDir {
this.folderName = this.path.basename(this.currentFolder);
this.cheFile = this.path.resolve(this.currentFolder, 'Chefile');
this.dotCheFile = this.path.resolve(this.currentFolder, '.Chefile');
this.dotCheFileLowercase = this.path.resolve(this.currentFolder, '.chefile');
this.dotCheFolder = this.path.resolve(this.currentFolder, '.che');
this.dotCheIdFile = this.path.resolve(this.dotCheFolder, 'id');
this.dotCheSshPrivateKeyFile = this.path.resolve(this.dotCheFolder, 'ssh-key.private');
Expand Down Expand Up @@ -204,38 +210,18 @@ export class CheDir {

parse() {

let bothFileExists : boolean = false;
try {
this.fs.statSync(this.cheFile);
// we have a file
Log.getLogger().debug('Found Chefile at ', this.cheFile);
let cheFiles : Array<any> = this.findCheFiles();

try {
this.fs.statSync(this.dotCheFile);
// There is an error as we shouldn't have both .Chefile and Chefile
bothFileExists = true;
} catch (e) {
// this file is optional
Log.getLogger().debug('There is a Chefile but not .Chefile at ' + this.dotCheFile);
}
} catch (e) {

// if there is a .Chefile, use it, else return
try {
this.fs.statSync(this.dotCheFile);
Log.getLogger().debug('The alternate file .Chefile is present at ', this.dotCheFile);
this.cheFile = this.dotCheFile;
} catch (e) {
Log.getLogger().debug('No chefile defined, use default settings');
return;
}
}

if (bothFileExists) {
throw new Error('Error while parsing the Chefile as there is ' + this.cheFile + ' and ' + this.dotCheFile + '. Only one file at a time is allowed.');
if (cheFiles.length === 0) {
Log.getLogger().debug('No chefile defined, use default settings');
return;
} else if (cheFiles.length >= 2) {
throw new Error('Error while parsing the Chefile as it was found at ' + JSON.stringify(cheFiles) + '. Only one file at a time is allowed.');
} else {
this.cheFile = cheFiles[0];
Log.getLogger().debug('Parsing chefile with name ', this.cheFile);
}


// load the chefile script if defined
var script_code : string = this.fs.readFileSync(this.cheFile).toString();

Expand Down Expand Up @@ -397,6 +383,52 @@ export class CheDir {

}

/**
* Checks if the given file is existing or not
* @param filename the path to check
* @returns {boolean} true if it exists
*/
fileExistsCaseSensitive(filename) : boolean {
var dir = this.path.dirname(filename);
if (dir === '/' || dir === '.') {
return true;
}
var filenames = this.fs.readdirSync(dir);
if (filenames.indexOf(this.path.basename(filename)) === -1) {
return false;
}
return this.fileExistsCaseSensitive(dir);
}

/**
* Search all the chefiles with the corresponding pattern : Chefile, .Chefile, .chefile
* @returns {Array<any>} containing all references found
*/
findCheFiles() : Array<any> {

let foundCheFiles : Array<any> = new Array<any>();

// Try with Chefile
if (this.fileExistsCaseSensitive(this.cheFile)) {
Log.getLogger().debug('Chefile is present at ', this.cheFile);
foundCheFiles.push(this.cheFile);
}

// Try with .Chefile
if (this.fileExistsCaseSensitive(this.dotCheFile)) {
Log.getLogger().debug('The alternate file .Chefile is present at ', this.dotCheFile);
foundCheFiles.push(this.dotCheFile);
}

// Try with .chefile
if (this.fileExistsCaseSensitive(this.dotCheFileLowercase)) {
this.fs.statSync(this.dotCheFileLowercase);
Log.getLogger().debug('The alternate file .chefile is present at ', this.dotCheFileLowercase);
foundCheFiles.push(this.dotCheFileLowercase);
}
return foundCheFiles;
}


init() : Promise<any> {
return this.isInitialized().then((isInitialized) => {
Expand All @@ -407,21 +439,13 @@ export class CheDir {
Log.getLogger().info('Adding', this.dotCheFolder, 'directory');
this.initCheFolders();

// write a default chefile if there is none
try {
this.fs.statSync(this.cheFile);
Log.getLogger().debug('Chefile is present at ', this.cheFile);
} catch (e) {

try {
this.fs.statSync(this.dotCheFile);
Log.getLogger().debug('The alternate file .Chefile is present at ', this.dotCheFile);
} catch (e) {
// write default
Log.getLogger().debug('Write a default Chefile at ', this.cheFile);
this.writeDefaultChefile();
}
let cheFiles: Array<any> = this.findCheFiles();

// write a default chefile if there is none
if (cheFiles.length === 0) {
// write default
Log.getLogger().debug('Write a default Chefile at ', this.cheFile);
this.writeDefaultChefile();
}
return true;
}
Expand Down

0 comments on commit ee0f0ea

Please sign in to comment.