/**
 * Copyright 2021 Opstrace, Inc.
 *
 * 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
 *
 *      http://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 fs from "fs" ; export interface BUILD_INFO_TYPE { BRANCH_NAME : string ; VERSION_STRING : string ; COMMIT : string ; BUILD_TIME_RFC3339 : string ; BUILD_HOSTNAME : string ; } export let BUILD_INFO ; const CANDIDATE_PATHS = [ "/snapshot/opstrace/buildinfo.json" , "/snapshot/build/buildinfo.json" , "/buildinfo.json" ] ; function parseFile ( p ) { BUILD_INFO = JSON . parse ( fs . readFileSync ( p , "utf8" ) ) ; } function probeAndParseFile ( p ) { let exists = false ; try { if ( fs . existsSync ( p ) ) { exists = true ; } } catch ( err ) { } if ( exists ) { parseFile ( p ) ; return true ; } return false ; } function readBuildInfo ( ) { for ( const p of CANDIDATE_PATHS ) { if ( probeAndParseFile ( p ) ) return ; } if ( ! process . env . OPSTRACE_BUILDINFO_PATH ) { throw Error ( `environment variable OPSTRACE_BUILDINFO_PATH not set ` + `and none of [${ JSON . stringify ( CANDIDATE_PATHS ) }] found/readable. ` + "For local development, run 'make set-build-info-constants' at the " + "root of the repository to generate buildinfo.json, and point " + "OPSTRACE_BUILDINFO_PATH to it." ) ; } try { parseFile ( process . env . OPSTRACE_BUILDINFO_PATH ) ; } catch ( err ) { throw Error ( `environment variable OPSTRACE_BUILDINFO_PATH set ` + `but saw the following error while trying to read file:\n${ err }` ) ; } } readBuildInfo ( ) ;