Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration to Typescript #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
220 changes: 110 additions & 110 deletions README.md

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions examples/containers.js → examples/containers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';
import { DockerClient } from "../lib/docker";

const docker = require('../index');

const client = docker.Client({
const client = new DockerClient({
socket: '/var/run/docker.sock'
});

Expand Down
6 changes: 2 additions & 4 deletions examples/events.js → examples/events.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';
import { DockerClient } from "../lib/docker";

const docker = require('../index');

const client = docker.Client({
const client = new DockerClient({
socket: '/var/run/docker.sock'
});

Expand Down
6 changes: 2 additions & 4 deletions examples/info.js → examples/info.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';
import { DockerClient } from "../lib/docker";

const docker = require('../index');

const client = docker.Client({
const client = new DockerClient({
socket: '/var/run/docker.sock'
});

Expand Down
6 changes: 2 additions & 4 deletions examples/logs.js → examples/logs.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';
import { DockerClient } from "../lib/docker";

const docker = require('../index');

const client = docker.Client({
const client = new DockerClient({
socket: '/var/run/docker.sock'
});

Expand Down
16 changes: 7 additions & 9 deletions examples/stats.js → examples/stats.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';
import { DockerClient } from "../lib/docker";

const docker = require('../index');

const client = docker.Client({
const client = new DockerClient({
socket: '/var/run/docker.sock'
});

Expand All @@ -13,16 +11,16 @@ client.containers().stats('ping').then((emitter) => {
//console.log(data);

/* jshint ignore:start */
if(data && data.memory_stats) {
const mem = (data.memory_stats.usage / Math.pow(1024, 2)) * (Math.pow(2, 20) / Math.pow(10, 6)).toFixed(2);
if (data && data.memory_stats) {
const mem = (data.memory_stats.usage / Math.pow(1024, 2)) * Number((Math.pow(2, 20) / Math.pow(10, 6)).toFixed(2));
const memUsage = ((data.memory_stats.usage / data.memory_stats.limit) * 100).toFixed(2);

let cpuPercent = (data.cpu_stats.cpu_usage.total_usage / data.cpu_stats.system_cpu_usage);
let cpuPercent: any = (data.cpu_stats.cpu_usage.total_usage / data.cpu_stats.system_cpu_usage);
cpuPercent = cpuPercent * data.cpu_stats.cpu_usage.percpu_usage.length * 100;
cpuPercent = cpuPercent.toFixed(2);

const networkIn = (data.networks.eth0.rx_bytes / 1024 / 1024).toFixed(3)
const networkOut = (data.networks.eth0.tx_bytes / 1024 / 1024).toFixed(3)
const networkIn = (data.networks.eth0.rx_bytes / 1024 / 1024).toFixed(3);
const networkOut = (data.networks.eth0.tx_bytes / 1024 / 1024).toFixed(3);

console.log('Memory Usage:', mem + 'MB', memUsage + '%');
console.log('CPU Usage:', cpuPercent + '%');
Expand Down
8 changes: 3 additions & 5 deletions examples/swarm-nodes.js → examples/swarm-nodes.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict';
import { DockerClient } from "../lib/docker";
import * as fs from "fs";

const docker = require('../index');
const fs = require('fs');

const client = docker.Client({
const client = new DockerClient({
host: '192.168.99.100',
port: '2376',
tls: {
Expand Down
3 changes: 0 additions & 3 deletions index.js

This file was deleted.

1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { DockerClient } from './lib/docker';
95 changes: 45 additions & 50 deletions lib/docker.js → lib/docker.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
'use strict';

const Joi = require('joi');
const Promise = require('bluebird');
const modem = require('./modem');
const schemas = require('./schemas/system');
const configs = require('./endpoints/configs');
const containers = require('./endpoints/containers');
const images = require('./endpoints/images');
const networks = require('./endpoints/networks');
const nodes = require('./endpoints/nodes');
const plugins = require('./endpoints/plugins');
const services = require('./endpoints/services');
const swarm = require('./endpoints/swarm');
const secrets = require('./endpoints/secrets');
const tasks = require('./endpoints/tasks');
const volumes = require('./endpoints/volumes');

class DockerClient {
import * as Joi from 'joi';
import * as Promise from 'bluebird';
import * as schemas from './schemas/system';
import Modem from "./modem";
import ConfigClient from "./endpoints/configs";
import ContainersClient from "./endpoints/containers";
import ImagesClient from "./endpoints/images";
import NetworksClient from "./endpoints/networks";
import NodesClient from "./endpoints/nodes";
import PluginsClient from "./endpoints/plugins";
import ServicesClient from "./endpoints/services";
import SwarmClient from "./endpoints/swarm";
import SecretsClient from "./endpoints/secrets";
import TasksClient from "./endpoints/tasks";
import VolumesClient from "./endpoints/volumes";

export class DockerClient {

private modem: Modem;
private _services: any;

constructor(options) {
this.modem = modem.Client(options);
this.modem = new Modem(options);
this._services = {};
}

Expand Down Expand Up @@ -79,11 +80,9 @@ class DockerClient {
});
}

events(options) {
events(options = {}) {
const self = this;

options = options || {};

return self._validate(options, schemas.events.options).then((params) => {
return self.modem.stream({
method: 'GET',
Expand All @@ -102,88 +101,88 @@ class DockerClient {
}

configs() {
if(!this._services.configs) {
this._services.configs = configs.Client(this);
if (!this._services.configs) {
this._services.configs = new ConfigClient(this);
}

return this._services.configs;
}

containers() {
if(!this._services.containers) {
this._services.containers = containers.Client(this);
if (!this._services.containers) {
this._services.containers = new ContainersClient(this);
}

return this._services.containers;
}

images() {
if(!this._services.images) {
this._services.images = images.Client(this);
if (!this._services.images) {
this._services.images = new ImagesClient(this);
}

return this._services.images;
}

swarm() {
if(!this._services.swarm) {
this._services.swarm = swarm.Client(this);
if (!this._services.swarm) {
this._services.swarm = new SwarmClient(this);
}

return this._services.swarm;
}

networks() {
if(!this._services.networks) {
this._services.networks = networks.Client(this);
if (!this._services.networks) {
this._services.networks = new NetworksClient(this);
}

return this._services.networks;
}

nodes() {
if(!this._services.nodes) {
this._services.nodes = nodes.Client(this);
if (!this._services.nodes) {
this._services.nodes = new NodesClient(this);
}

return this._services.nodes;
}

plugins() {
if(!this._services.plugins) {
this._services.plugins = plugins.Client(this);
if (!this._services.plugins) {
this._services.plugins = new PluginsClient(this);
}

return this._services.plugins;
}

secrets() {
if(!this._services.secrets) {
this._services.secrets = secrets.Client(this);
if (!this._services.secrets) {
this._services.secrets = new SecretsClient(this);
}

return this._services.secrets;
}

services() {
if(!this._services.services) {
this._services.services = services.Client(this);
if (!this._services.services) {
this._services.services = new ServicesClient(this);
}

return this._services.services;
}

tasks() {
if(!this._services.tasks) {
this._services.tasks = tasks.Client(this);
if (!this._services.tasks) {
this._services.tasks = new TasksClient(this);
}

return this._services.tasks;
}

volumes() {
if(!this._services.volumes) {
this._services.volumes = volumes.Client(this);
if (!this._services.volumes) {
this._services.volumes = new VolumesClient(this);
}

return this._services.volumes;
Expand All @@ -194,7 +193,7 @@ class DockerClient {
return new Promise((resolve, reject) => {

Joi.validate(value, schema, {}, (err, value) => {
if(!err) {
if (!err) {
resolve(value);
} else {
reject(err);
Expand All @@ -206,7 +205,3 @@ class DockerClient {
}

}

module.exports.Client = function(options) {
return new DockerClient(options);
};
13 changes: 3 additions & 10 deletions lib/endpoints/configs.js → lib/endpoints/configs.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
'use strict';
import * as schemas from '../schemas/configs';

const schemas = require('../schemas/configs');
export default class ConfigClient {

class ConfigClient {

constructor(docker) {
this.docker = docker;
constructor(private docker) {
}

list(options) {
Expand Down Expand Up @@ -112,7 +109,3 @@ class ConfigClient {
}

}

module.exports.Client = function(docker) {
return new ConfigClient(docker);
};
Loading