@cloud-cli/dns 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -9,6 +9,9 @@ interface DomainAndTarget {
9
9
  declare function add(input: DomainAndTarget): boolean;
10
10
  declare function remove(input: DomainAndTarget): boolean;
11
11
  declare function list(): DomainAndTarget[];
12
+ declare function get(options: {
13
+ domain: string;
14
+ }): DomainAndTarget | null;
12
15
  declare function reload(): Promise<true>;
13
16
  declare function addDnsConfig(options: DNSConfig): void;
14
17
  export declare function parseDNSLine(line: string): {
@@ -20,6 +23,7 @@ declare const _default: {
20
23
  remove: typeof remove;
21
24
  list: typeof list;
22
25
  reload: typeof reload;
26
+ get: typeof get;
23
27
  [init]: typeof addDnsConfig;
24
28
  };
25
29
  export default _default;
package/dist/index.js CHANGED
@@ -35,6 +35,9 @@ function list() {
35
35
  .map(parseDNSLine);
36
36
  return entries;
37
37
  }
38
+ function get(options) {
39
+ return list().filter(d => d.domain === options.domain)[0] || null;
40
+ }
38
41
  async function reload() {
39
42
  const cmd = await exec('systemctl', ['restart', 'dnsmasq']);
40
43
  return cmd.ok || Promise.reject(new Error('Failed to reload'));
@@ -46,11 +49,10 @@ function addDnsConfig(options) {
46
49
  }
47
50
  function save(list) {
48
51
  const txt = list.map(line => `address=/${line.domain}/${line.target}`);
49
- console.log('save', filePath, txt);
50
52
  fs.writeFileSync(filePath, txt.join('\n'));
51
53
  }
52
54
  export function parseDNSLine(line) {
53
55
  const parts = line.match(lineParse);
54
56
  return { domain: parts[1], target: parts[2] };
55
57
  }
56
- export default { add, remove, list, reload, [init]: addDnsConfig };
58
+ export default { add, remove, list, reload, get, [init]: addDnsConfig };
@@ -25,6 +25,15 @@ describe('dns', () => {
25
25
  { domain: 'foo', target: '5.6.7.8' },
26
26
  ]);
27
27
  });
28
+ it('should get a DNS entry by domain', () => {
29
+ let fileExists = false;
30
+ const buffer = `address=/test.com/1.2.3.4\naddress=/foo.com/5.6.7.8`;
31
+ jest.spyOn(fs, 'existsSync').mockImplementation(() => fileExists);
32
+ jest.spyOn(fs, 'readFileSync').mockImplementation(() => buffer);
33
+ expect(dns.get({ domain: 'foo.com' })).toBe(null);
34
+ fileExists = true;
35
+ expect(dns.get({ domain: 'foo.com' })).toEqual({ domain: 'foo.com', target: '5.6.7.8' });
36
+ });
28
37
  it('should add and remove local DNS entries', () => {
29
38
  let text = '';
30
39
  jest.spyOn(fs, 'writeFileSync').mockImplementation((_file, value) => { text = value; });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloud-cli/dns",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "type": "module",
package/src/index.spec.ts CHANGED
@@ -32,6 +32,18 @@ describe('dns', () => {
32
32
  ])
33
33
  });
34
34
 
35
+ it('should get a DNS entry by domain', () => {
36
+ let fileExists = false;
37
+ const buffer = `address=/test.com/1.2.3.4\naddress=/foo.com/5.6.7.8`;
38
+ jest.spyOn(fs, 'existsSync').mockImplementation(() => fileExists);
39
+ jest.spyOn(fs, 'readFileSync').mockImplementation(() => buffer);
40
+
41
+ expect(dns.get({ domain: 'foo.com' })).toBe(null);
42
+
43
+ fileExists = true;
44
+ expect(dns.get({ domain: 'foo.com' })).toEqual({ domain: 'foo.com', target: '5.6.7.8' });
45
+ });
46
+
35
47
  it('should add and remove local DNS entries', () => {
36
48
  let text = '';
37
49
  jest.spyOn(fs, 'writeFileSync').mockImplementation((_file, value: string) => { text = value; });
package/src/index.ts CHANGED
@@ -54,6 +54,10 @@ function list(): DomainAndTarget[] {
54
54
  return entries;
55
55
  }
56
56
 
57
+ function get(options: { domain: string; }): DomainAndTarget | null {
58
+ return list().filter(d => d.domain === options.domain)[0] || null;
59
+ }
60
+
57
61
  async function reload() {
58
62
  const cmd = await exec('systemctl', ['restart', 'dnsmasq']);
59
63
  return cmd.ok || Promise.reject(new Error('Failed to reload'));
@@ -67,7 +71,6 @@ function addDnsConfig(options: DNSConfig) {
67
71
 
68
72
  function save(list: DomainAndTarget[]) {
69
73
  const txt = list.map(line => `address=/${line.domain}/${line.target}`);
70
- console.log('save', filePath, txt);
71
74
  fs.writeFileSync(filePath, txt.join('\n'));
72
75
  }
73
76
 
@@ -76,4 +79,4 @@ export function parseDNSLine(line: string) {
76
79
  return { domain: parts[1], target: parts[2] }
77
80
  }
78
81
 
79
- export default { add, remove, list, reload, [init]: addDnsConfig }
82
+ export default { add, remove, list, reload, get, [init]: addDnsConfig }