<commit_before>// @flow

import fs from 'fs';
import R from 'ramda';
import { writeFile } from '../util/fs';
import yaml from 'js-yaml';
import findup from 'findup-sync';

// TODO: Type the rest of the config
export type LagoonConfig = {|
  api?: string,
  branches?: boolean | RegExp,
  customer?: number,
  git_url?: string,
  openshift?: number,
  production_environment?: string,
  project?: string,
  pullrequests?: boolean,
  ssh?: string,
|};

export type LagoonConfigInput = {|
  project: string,
  api?: string,
  ssh?: string,
|};

export function createConfig(
  filepath: string,
  inputOptions: LagoonConfigInput,
): Promise<void> {
  const config: LagoonConfig = {
    ...inputOptions,
  };
  const yamlConfig = yaml.safeDump(config);
  return writeFile(filepath, yamlConfig);
}

export function parseConfig(yamlContent: string): LagoonConfig {
  // TODO: Add schema validation in there if necessary
  return yaml.safeLoad(yamlContent);
}

/**
 * Finds and reads the lagoon.yml file
 */
function readConfig(): ?LagoonConfig {
  const configPath = findup('.lagoon.yml');

  if (configPath == null) {
    return null;
  }

  const yamlContent = fs.readFileSync(configPath);
  return parseConfig(yamlContent.toString());
}

export const config = readConfig();

export const getSshConfig = (() => {
  let allSshConfig;
  return () => {
    if (!allSshConfig) {
      const username = 'lagoon';
      const sshConfig = R.prop('ssh', config);

      const host = R.cond([
        [R.prop('SSH_HOST'), R.prop('SSH_HOST')],
        [R.always(sshConfig), R.always(R.head(R.split(':', sshConfig)))],
        [<commit_msg>Fix comment<commit_after>// @flow

import fs from 'fs';
import R from 'ramda';
import { writeFile } from '../util/fs';
import yaml from 'js-yaml';
import findup from 'findup-sync';

// TODO: Type the rest of the config
export type LagoonConfig = {|
  api?: string,
  branches?: boolean | RegExp,
  customer?: number,
  git_url?: string,
  openshift?: number,
  production_environment?: string,
  project?: string,
  pullrequests?: boolean,
  ssh?: string,
|};

export type LagoonConfigInput = {|
  project: string,
  api?: string,
  ssh?: string,
|};

export function createConfig(
  filepath: string,
  inputOptions: LagoonConfigInput,
): Promise<void> {
  const config: LagoonConfig = {
    ...inputOptions,
  };
  const yamlConfig = yaml.safeDump(config);
  return writeFile(filepath, yamlConfig);
}

export function parseConfig(yamlContent: string): LagoonConfig {
  // TODO: Add schema validation in here if necessary
  return yaml.safeLoad(yamlContent);
}

/**
 * Finds and reads the lagoon.yml file
 */
function readConfig(): ?LagoonConfig {
  const configPath = findup('.lagoon.yml');

  if (configPath == null) {
    return null;
  }

  const yamlContent = fs.readFileSync(configPath);
  return parseConfig(yamlContent.toString());
}

export const config = readConfig();

export const getSshConfig = (() => {
  let allSshConfig;
  return () => {
    if (!allSshConfig) {
      const username = 'lagoon';
      const sshConfig = R.prop('ssh', config);

      const host = R.cond([
        [R.prop('SSH_HOST'), R.prop('SSH_HOST')],
        [R.always(sshConfig), R.always(R.head(R.split(':', sshConfig)))],
        [
{'fraction_non_alphanumeric': 0.14351709270095472, 'fraction_numerical': 0.0, 'mean_word_length': 3.804733727810651, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '319651', 'n_tokens_mistral': 1084, 'n_tokens_neox': 1031, 'n_words': 285}