-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e44a85
commit 834236e
Showing
10 changed files
with
385 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
'use strict' | ||
|
||
const EventEmitter = require('events') | ||
const baseWidget = require('../src/baseWidget') | ||
const clipboardy = require('clipboardy') | ||
|
||
class hook extends baseWidget(EventEmitter) { | ||
init () { | ||
if (!this.widgetsRepo.has('toolbar')) { | ||
return null | ||
} | ||
|
||
this.notifyOnImageUpdate() | ||
|
||
const toolbar = this.widgetsRepo.get('toolbar') | ||
toolbar.on('key', (keyString) => { | ||
// on refresh keypress, update all containers and images information | ||
|
||
if (keyString === 'r') { | ||
this.removeImage() | ||
} | ||
|
||
if (keyString === 'c') { | ||
this.copyImageIdToClipboard() | ||
} | ||
}) | ||
} | ||
|
||
copyImageIdToClipboard () { | ||
if (this.widgetsRepo && this.widgetsRepo.has('imageList')) { | ||
const imageId = this.widgetsRepo.get('imageList').getSelectedImage() | ||
if (imageId) { | ||
clipboardy.writeSync(imageId) | ||
|
||
const actionStatus = this.widgetsRepo.get('actionStatus') | ||
const message = `Image Id ${imageId} was copied to the clipboard` | ||
|
||
actionStatus.emit('message', { | ||
message: message | ||
}) | ||
} | ||
} | ||
} | ||
|
||
notifyOnImageUpdate () { | ||
setInterval(() => { | ||
if (this.widgetsRepo && this.widgetsRepo.has('imageList')) { | ||
// Update on Docker Info | ||
this.utilsRepo.get('docker').systemDf((data) => { | ||
if (!data.Images) { | ||
return | ||
} | ||
|
||
const UseImages = [] | ||
const UnuseImages = [] | ||
|
||
data.Images.forEach(image => { | ||
if (image.Containers > 0) { | ||
UseImages.push(image) | ||
} else { | ||
UnuseImages.push(image) | ||
} | ||
}) | ||
|
||
data.UseImages = UseImages | ||
data.UnuseImages = UnuseImages | ||
|
||
this.emit('imagesUtilization', data) | ||
this.emit('imageSize', data) | ||
}) | ||
} | ||
}, 1000) | ||
} | ||
|
||
removeImage () { | ||
if (this.widgetsRepo && this.widgetsRepo.has('imageList')) { | ||
const imageId = this.widgetsRepo.get('imageList').getSelectedImage() | ||
if (imageId && imageId !== 0 && imageId !== false) { | ||
const actionStatus = this.widgetsRepo.get('actionStatus') | ||
|
||
const title = 'Removing image' | ||
let message = 'Removing image...' | ||
|
||
actionStatus.emit('message', { | ||
title: title, | ||
message: message | ||
}) | ||
|
||
this.utilsRepo.get('docker').removeImage(imageId, (err, data) => { | ||
if (err) { | ||
message = err.json.message | ||
} else { | ||
message = 'Removed image successfully' | ||
this.widgetsRepo.get('imageList').refreshList() | ||
} | ||
|
||
actionStatus.emit('message', { | ||
title: title, | ||
message: message | ||
}) | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = hook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,6 @@ | |
|
||
module.exports = { | ||
container: 'containers', | ||
service: 'services' | ||
service: 'services', | ||
image: 'images' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict' | ||
|
||
const InfoWidget = require('../../src/widgetsTemplates/info.widget.template') | ||
|
||
class myWidget extends InfoWidget { | ||
getLabel () { | ||
return 'Image Info' | ||
} | ||
|
||
getSelectedItemId () { | ||
return this.widgetsRepo.get('imageList').getSelectedImage() | ||
} | ||
|
||
getItemById (itemId, cb) { | ||
return this.utilsRepo.get('docker').getImage(itemId, cb) | ||
} | ||
} | ||
|
||
module.exports = myWidget |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
'use strict' | ||
|
||
const ListWidget = require('../../src/widgetsTemplates/list.widget.template') | ||
|
||
class myWidget extends ListWidget { | ||
constructor ({ blessed = {}, contrib = {}, screen = {}, grid = {} }) { | ||
super({ blessed, contrib, screen, grid }) | ||
this.imagesListData = [] | ||
} | ||
|
||
getLabel () { | ||
return 'Images' | ||
} | ||
|
||
getListItems (cb) { | ||
this.utilsRepo.get('docker').listImages(cb) | ||
} | ||
|
||
filterList (data) { | ||
let imageTitleList = this.imagesListData[0] | ||
let imageList = this.imagesListData.slice(1) | ||
let filteredimages = [] | ||
|
||
if (data) { | ||
filteredimages = imageList.filter((container, index, containerItems) => { | ||
const imageName = container[1] | ||
const imageTag = container[2] | ||
|
||
if ((imageName.indexOf(data) !== -1) || (imageTag.indexOf(data) !== -1)) { | ||
return true | ||
} | ||
}) | ||
} | ||
|
||
if (filteredimages.length > 0) { | ||
filteredimages.unshift(imageTitleList) | ||
this.update(filteredimages) | ||
} else { | ||
this.update(this.imagesListData) | ||
} | ||
} | ||
|
||
formatList (images) { | ||
const imageList = [] | ||
|
||
if (images) { | ||
images.forEach((image) => { | ||
const getTag = (tag, part) => tag ? tag[0].split(':')[part] : 'none' | ||
|
||
imageList.push([ | ||
image.Id.substring(7, 12), | ||
image.RepoDigests ? image.RepoDigests[0].split('@')[0] : getTag(image[2], 0), | ||
getTag(image.RepoTags, 1), | ||
this.timeDifference(image.Created), | ||
this.formatBytes(image.Size) | ||
]) | ||
}) | ||
} | ||
|
||
imageList.unshift(['Id', 'Name', 'Tag', 'Created', 'Size']) | ||
|
||
this.imagesListData = imageList | ||
|
||
return imageList | ||
} | ||
|
||
/** | ||
* Format raw bytes into human readable size. | ||
* | ||
* @param {number} bytes - number of bytes. | ||
* @returns {string} human readable size. | ||
*/ | ||
formatBytes (bytes, decimals) { | ||
if (bytes === 0) return '0 Bytes' | ||
let k = 1000 | ||
|
||
let sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] | ||
|
||
let i = Math.floor(Math.log(bytes) / Math.log(k)) | ||
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i] | ||
} | ||
|
||
/** | ||
* Convert number of seceond to | ||
* @param {number} creationDate Images creation date in unix time. | ||
* @returns | ||
*/ | ||
timeDifference (creationDate) { | ||
const msPerMinute = 60 * 1000 | ||
const msPerHour = msPerMinute * 60 | ||
const msPerDay = msPerHour * 24 | ||
const msPerMonth = msPerDay * 30 | ||
const msPerYear = msPerDay * 365 | ||
|
||
const elapsed = Date.now() - (creationDate * 1000) | ||
|
||
const cleanReturn = (number, format) => `${Math.round(number)} ${format}${Math.round(number) === 1 ? '' : 's'} ago` | ||
|
||
if (elapsed < msPerMinute) { return cleanReturn(elapsed / 1000, 'second') } | ||
if (elapsed < msPerHour) { return cleanReturn(elapsed / msPerMinute, 'minute') } | ||
if (elapsed < msPerDay) { return cleanReturn(elapsed / msPerHour, 'hour') } | ||
if (elapsed < msPerMonth) { return cleanReturn(elapsed / msPerDay, 'day') } | ||
if (elapsed < msPerYear) { return cleanReturn(elapsed / msPerMonth, 'month') } | ||
return cleanReturn(elapsed / msPerYear, 'year') | ||
} | ||
|
||
/** | ||
* returns a selected container from the containers listbox | ||
* @return {string} container id | ||
*/ | ||
getSelectedImage () { | ||
return this.widget.getItem(this.widget.selected).getContent().toString().trim().split(' ').shift() | ||
} | ||
} | ||
|
||
module.exports = myWidget |
Oops, something went wrong.