refactor: Generate and normalise PortMaps from compose ports in-class

Change-type: patch
Signed-off-by: Cameron Diver <cameron@balena.io>
This commit is contained in:
Cameron Diver 2019-01-02 12:19:30 +00:00
parent e82749e63a
commit dc34025545
No known key found for this signature in database
GPG Key ID: 49690ED87032539F
3 changed files with 8 additions and 10 deletions

View File

@ -30,7 +30,7 @@ interface PortRange {
export class PortMap {
private ports: PortRange;
public constructor(portStrOrObj: string | PortRange) {
private constructor(portStrOrObj: string | PortRange) {
if (_.isString(portStrOrObj)) {
this.parsePortString(portStrOrObj);
} else {
@ -139,10 +139,11 @@ export class PortMap {
}, []);
}
public static normaliseComposePorts(portMaps: PortMap[]): PortMap[] {
return _.sortBy(portMaps, p => {
return p.ports.internalStart;
});
public static fromComposePorts(ports: string[]): PortMap[] {
return _(ports)
.map(p => new PortMap(p))
.sortBy(p => p.ports.internalStart)
.value();
}
private parsePortString(portStr: string): void {

View File

@ -253,9 +253,7 @@ export class Service {
let portMaps: PortMap[] = [];
if (config.ports != null) {
portMaps = PortMap.normaliseComposePorts(
_.map(config.ports, p => new PortMap(p)),
);
portMaps = PortMap.fromComposePorts(config.ports);
}
delete config.ports;

View File

@ -248,8 +248,7 @@ describe 'Ports', ->
portBindings = require('./data/ports/not-ascending/port-bindings.json')
compose = require('./data/ports/not-ascending/compose.json')
portMapsCurrent = PortMap.fromDockerOpts(portBindings)
portMapsTarget = compose.ports.map((p) -> new PortMap(p))
portMapsTarget = PortMap.normaliseComposePorts(portMapsTarget)
portMapsTarget = PortMap.fromComposePorts(compose.ports)
expect(portMapsTarget).to.deep.equal(portMapsCurrent)