kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/extractors/cmd/bazel/copy_kzip/copy_kzip.go (about) 1 /* 2 * Copyright 2020 The Kythe Authors. All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 // Program copy_kzip implements a Bazel extra action that directly copies a Kythe 18 // compilation record produced by a "spawn" action. 19 package main 20 21 import ( 22 "context" 23 "flag" 24 "io" 25 "time" 26 27 "kythe.io/kythe/go/extractors/bazel" 28 "kythe.io/kythe/go/platform/vfs" 29 "kythe.io/kythe/go/util/log" 30 ) 31 32 var ( 33 outputPath = flag.String("output", "", "Path of output index file (required)") 34 extraAction = flag.String("extra_action", "", "Path of the SpawnAction file (required)") 35 ) 36 37 func main() { 38 flag.Parse() 39 40 // Verify that required flags are set. 41 if *outputPath == "" { 42 log.Fatal("You must provide a non-empty --output file path") 43 } 44 if *extraAction == "" { 45 log.Fatal("You must provide a non-empty --extra_action file path") 46 } 47 48 info, err := bazel.LoadAction(*extraAction) 49 if err != nil { 50 log.Fatalf("Unable to load extra action: %v", err) 51 } 52 start := time.Now() 53 ai, err := bazel.SpawnAction(info) 54 if err != nil { 55 log.Fatalf("Unable to read SpawnInfo: %v", err) 56 } 57 if len(ai.Outputs) != 1 { 58 log.Fatalf("Can only copy a single output kzip, not %d", len(ai.Outputs)) 59 } 60 61 if err := copyFile(ai.Outputs[0], *outputPath); err != nil { 62 log.Fatalf("Unable to copy input to output: %v", err) 63 } 64 65 log.Infof("Finished extracting [%v elapsed]", time.Since(start)) 66 } 67 68 func copyFile(input, output string) error { 69 ctx := context.Background() 70 f, err := vfs.Open(ctx, input) 71 if err != nil { 72 return err 73 } 74 defer f.Close() 75 76 o, err := vfs.Create(ctx, output) 77 if err != nil { 78 return err 79 } 80 81 if _, err := io.Copy(o, f); err != nil { 82 o.Close() 83 return err 84 } 85 86 return o.Close() 87 }