• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SAP / cloud-foundry-tools-api / 4173844924

pending completion
4173844924

Pull #161

github

GitHub
Merge 8ead83807 into cd358e984
Pull Request #161: feat: add quote-vcap flag to bind-local

187 of 188 branches covered (99.47%)

Branch coverage included in aggregate %.

1 of 1 new or added line in 1 file covered. (100.0%)

468 of 471 relevant lines covered (99.36%)

11.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/utils.ts
1
/* eslint-disable @typescript-eslint/no-unsafe-return */
2
/* eslint-disable @typescript-eslint/no-explicit-any */
3
import * as _ from "lodash";
1✔
4
import * as os from "os";
1✔
5
import * as fs from "fs";
1✔
6
import * as path from "path";
1✔
7
import { parse } from "comment-json";
1✔
8
import { messages } from "./messages";
1✔
9
import { IServiceQuery, CF_PAGE_SIZE, IServiceFilters, eFilters, eServiceTypes } from "./types";
1✔
10

11
export async function dataContentAsObject(filePath: string) {
1✔
12
  try {
5✔
13
    // eslint-disable-next-line @typescript-eslint/no-unsafe-return
14
    return _.reduce(
5✔
15
      _.split(await fs.promises.readFile(filePath, { encoding: "utf8" }), os.EOL),
16
      (data: any, line: string) => {
17
        const parts = _.split(line, "=");
11✔
18
        if (_.size(parts) > 1) {
11✔
19
          data[_.trim(parts[0])] = _.trim(parts[1]);
6✔
20
        }
21
        // eslint-disable-next-line @typescript-eslint/no-unsafe-return
22
        return data;
11✔
23
      },
24
      {}
25
    );
26
  } catch (error) {
27
    // log error
28
    return {};
1✔
29
  }
30
}
31

32
export function ensureQuery(query?: IServiceQuery): IServiceQuery {
1✔
33
  query = query || {};
121✔
34
  _.defaults(query, { filters: [] });
121✔
35
  // default paging size is 50...
36
  _.defaults(query, { per_page: CF_PAGE_SIZE });
121✔
37
  return query;
121✔
38
}
39

40
export function padQuery(query: IServiceQuery, otherFilters: IServiceFilters[]): IServiceQuery {
1✔
41
  query = ensureQuery(query);
58✔
42
  _.each(otherFilters, (other) => {
58✔
43
    const filter = _.find(query.filters, ["key", other.key]);
57✔
44
    if (!_.size(filter?.value)) {
57✔
45
      query.filters = _.concat(query.filters, [other]);
54✔
46
    }
47
  });
48
  return query;
58✔
49
}
50

51
export function getGuid(resource: any): string {
1✔
52
  return _.get(resource, "guid", "");
61✔
53
}
54

55
export function getName(resource: any): string {
1✔
56
  return _.get(resource, "name", "");
38✔
57
}
58

59
export function getLabel(resource: any): string {
1✔
60
  return _.get(resource, "label", "");
16✔
61
}
62

63
export function getDescription(resource: any): string {
1✔
64
  return _.get(resource, "description", "");
20✔
65
}
66

67
export function getSpaceFieldGUID(spaceField: any): string {
1✔
68
  return _.get(spaceField, "GUID", "");
45✔
69
}
70

71
export function getOrgGUID(resource: any): string {
1✔
72
  return _.get(resource, ["relationships", "organization", "data", "guid"], "");
3✔
73
}
74

75
export function getTags(resource: any): string[] {
1✔
76
  return _.get(resource, "tags", []);
15✔
77
}
78

79
/**
80
 * Combine path to 'cf' configuration file
81
 * @param target: string (optional), determines which config file is looking for
82
 * @returns
83
 */
84
export function cfGetConfigFilePath(target?: string): string {
1✔
85
  const relatives = target ? ["targets", `${target}.config.json`] : [`config.json`];
71✔
86
  /* eslint-disable-next-line @typescript-eslint/no-unsafe-argument */
87
  return path.join(_.get(process, "env.CF_HOME", os.homedir()), ".cf", ...relatives);
71✔
88
}
89

90
export function isUpsType(resource: any): boolean {
1✔
91
  return _.get(resource, "type", eServiceTypes.managed) === eServiceTypes.user_provided;
30✔
92
}
93

94
/**
95
 *  Get json value of config file
96
 * @param target: string (optional), in case a predefined target configuration file exists the value will be fetched from there
97
 * @returns object: json
98
 */
99
export async function cfGetConfigFileJson(target?: string): Promise<unknown> {
1✔
100
  try {
46✔
101
    return parse(await fs.promises.readFile(cfGetConfigFilePath(target), { encoding: "utf8" }));
46✔
102
  } catch (error) {
103
    // empty or non existing file
104
  }
105
}
106

107
/**
108
 *  Get field value from config file
109
 * @param field: string, name of requested field
110
 * @param target: string (optional), in case a predefined target configuration file exists the value will be fetched from there
111
 * @returns object: json
112
 */
113
export async function cfGetConfigFileField(field: string, target?: string): Promise<any> {
1✔
114
  return _.get(await cfGetConfigFileJson(target), `${field}`);
46✔
115
}
116

117
export async function getSpaceGuidThrowIfUndefined(): Promise<string> {
1✔
118
  const space: string = getSpaceFieldGUID(await cfGetConfigFileField("SpaceFields"));
43✔
119
  if (!space) {
43✔
120
    throw new Error(messages.cf_setting_not_set);
4✔
121
  }
122
  return space;
39✔
123
}
124

125
export async function padQuerySpace(query: IServiceQuery, otherFilters?: IServiceFilters[]): Promise<IServiceQuery> {
1✔
126
  query = padQuery(query, otherFilters);
42✔
127
  const filter = _.find(query.filters, ["key", eFilters.space_guids]);
42✔
128
  if (!_.size(filter?.value)) {
42✔
129
    query.filters = _.concat(query.filters, [
24✔
130
      { key: eFilters.space_guids, value: await getSpaceGuidThrowIfUndefined() },
131
    ]);
132
  }
133
  return query;
41✔
134
}
135

136
export function parseRawDictData(data: string): unknown {
1✔
137
  const result: any = {};
9✔
138
  _.each(_.compact(_.split(data, "\n")), (item) => {
9✔
139
    item = _.replace(_.trim(item), /^['"]|['"]$/g, "");
37✔
140
    const sep = _.indexOf(item, ":");
37✔
141
    if (sep > -1) {
37✔
142
      const key = _.toLower(_.trim(_.join(_.slice(item, 0, sep), "")));
26✔
143
      const value = _.trim(_.join(_.slice(item, sep + 1), ""));
26✔
144
      result[`${key}`] = value;
26✔
145
    }
146
  });
147
  return result;
9✔
148
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc