<commit_before>/**
 * @license
 * Copyright 2018 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import * as analytics from '../analytics';
import {Action, Category} from '../analytics';
import * as util from '../util';

export const addBucketPrefix = (bucket: string) => `gs://${bucket}`;

export const checkGsutilInstalled = async (): Promise<boolean> => {
  try {
    await util.exec('which gsutil', {}, false);
  } catch (e) {
    analytics.trackEvent(Category.EXECUTION, Action.GSUTIL_NOT_INSTALLED);
    throw new Error(
      '\nERROR: gsutil is not installed, but is needed for the viz template. \
Please follow installation instructions at\n\
https://cloud.google.com/storage/docs/gsutil_install\nExiting template \
creation, no files have been created.'
    );
  }
  return true;
};

export const parseBucketName = (bucketPath: string): string | undefined => {
  const matches = bucketPath.match(/(gs:\/\/[^/\s]+)/);
  return matches && matches[0];
};

export const hasBucketPermissions = async (
  gcsPath: string
): Promise<boolean | string> => {
  const gcsRootBucket = parseBucketName(gcsPath);
  if (!gcsRootBucket) {
    return `${gcsPath} is an invalid gcs bucket name.`;
  }
  await util.exec(`gsutil acl get ${gcsRootBucket}`, {}, false);
  return true;
};<commit_msg>be more explicit about gsutil failures.<commit_after>/**
 * @license
 * Copyright 2018 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import * as analytics from '../analytics';
import {Action, Category} from '../analytics';
import * as util from '../util';

export const addBucketPrefix = (bucket: string) => `gs://${bucket}`;

export const checkGsutilInstalled = async (): Promise<boolean> => {
  try {
    await util.exec('which gsutil', {}, false);
  } catch (e) {
    analytics.trackEvent(Category.EXECUTION, Action.GSUTIL_NOT_INSTALLED);
    throw new Error(
      '\nERROR: gsutil is not installed, but is needed for the viz template. \
Please follow installation instructions at\n\
https://cloud.google.com/storage/docs/gsutil_install\nExiting template \
creation, no files have been created.'
    );
  }
  return true;
};

export const parseBucketName = (bucketPath: string): string | undefined => {
  const matches = bucketPath.match(/(gs:\/\/[^/\s]+)/);
  return matches && matches[0];
};

export const hasBucketPermissions = async (
  gcsPath: string
): Promise<boolean | string> => {
  const gcsRootBucket = parseBucketName(gcsPath);
  if (!gcsRootBucket) {
    return `${gcsPath} is an invalid gcs bucket name.`;
  }
  try {
    await util.exec(`gsutil acl get ${gcsRootBucket}`, {}, false);
    return true;
  } catch (e) {
    return e.message;
  }
};
{'fraction_non_alphanumeric': 0.12297297297297298, 'fraction_numerical': 0.004864864864864865, 'mean_word_length': 3.863337713534823, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, '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': '5964828', 'n_tokens_mistral': 1070, 'n_tokens_neox': 1059, 'n_words': 392}