kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/extractors/bazel/extutil/extutil.go (about)

     1  /*
     2   * Copyright 2018 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  // Package extutil implements shared code for extracting and writing output
    18  // from Bazel actions to .kzip files. This is a temporary
    19  // measure to support migrating to .kzip output.
    20  package extutil // import "kythe.io/kythe/go/extractors/bazel/extutil"
    21  
    22  import (
    23  	"context"
    24  	"fmt"
    25  	"path/filepath"
    26  
    27  	"kythe.io/kythe/go/extractors/bazel"
    28  )
    29  
    30  // ExtractAndWrite extracts a spawn action through c and writes the results to
    31  // the specified output file. The output format is based on the file extension:
    32  //
    33  //	.kzip     -- writes a kzip file
    34  //	otherwise -- reports an error
    35  //
    36  // Deprecated: use bazel.ExtractToKzip
    37  func ExtractAndWrite(ctx context.Context, c *bazel.Config, ai *bazel.ActionInfo, outputPath string) error {
    38  	switch ext := filepath.Ext(outputPath); ext {
    39  	case ".kzip":
    40  		w, err := bazel.NewKZIP(outputPath)
    41  		if err != nil {
    42  			return fmt.Errorf("creating kzip writer: %v", err)
    43  		}
    44  		if _, err := c.ExtractToFile(ctx, ai, w); err != nil {
    45  			return fmt.Errorf("extracting: %v", err)
    46  		}
    47  		if err := w.Close(); err != nil {
    48  			return fmt.Errorf("closing output: %v", err)
    49  		}
    50  
    51  	default:
    52  		return fmt.Errorf("unknown output extension %q", ext)
    53  	}
    54  	return nil
    55  }