1// Copyright 2021 The Bazel Authors. All rights reserved. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package main 16 17import ( 18 "context" 19 "fmt" 20 "go/build" 21 "os" 22 "os/signal" 23 "path" 24 "path/filepath" 25) 26 27func getenvDefault(key, defaultValue string) string { 28 if v, ok := os.LookupEnv(key); ok { 29 return v 30 } 31 return defaultValue 32} 33 34func concatStringsArrays(values ...[]string) []string { 35 ret := []string{} 36 for _, v := range values { 37 ret = append(ret, v...) 38 } 39 return ret 40} 41 42func ensureAbsolutePathFromWorkspace(path string) string { 43 if filepath.IsAbs(path) { 44 return path 45 } 46 return filepath.Join(workspaceRoot, path) 47} 48 49func signalContext(parentCtx context.Context, signals ...os.Signal) (ctx context.Context, stop context.CancelFunc) { 50 ctx, cancel := context.WithCancel(parentCtx) 51 ch := make(chan os.Signal, 1) 52 go func() { 53 select { 54 case <-ch: 55 cancel() 56 case <-ctx.Done(): 57 } 58 }() 59 signal.Notify(ch, signals...) 60 61 return ctx, cancel 62} 63 64func isLocalPattern(pattern string) bool { 65 return build.IsLocalImport(pattern) || filepath.IsAbs(pattern) 66} 67 68func packageID(pattern string) string { 69 pattern = path.Clean(pattern) 70 if filepath.IsAbs(pattern) { 71 if relPath, err := filepath.Rel(workspaceRoot, pattern); err == nil { 72 pattern = relPath 73 } 74 } 75 76 return fmt.Sprintf("//%s", pattern) 77} 78