...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/operator/scripts/utils/cmd.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/operator/scripts/utils

     1  // Copyright 2022 Google LLC
     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  
    15  package utils
    16  
    17  import (
    18  	"fmt"
    19  	"os/exec"
    20  	"path"
    21  )
    22  
    23  func DownloadAndExtractTarballAt(gcsPath, outputDir string) error {
    24  	if err := DownloadObjectFromGCS(gcsPath, outputDir); err != nil {
    25  		return fmt.Errorf("error downloading tarball at '%v': %v", gcsPath, err)
    26  	}
    27  	tarballName := path.Base(gcsPath)
    28  	tarballPath := path.Join(outputDir, tarballName)
    29  	if err := ExtractTarball(tarballPath, outputDir); err != nil {
    30  		return fmt.Errorf("error extracting tarball: %v", err)
    31  	}
    32  	return nil
    33  }
    34  
    35  func DownloadObjectFromGCS(gcsPath, outputDir string) error {
    36  	cmd := exec.Command("gsutil", "cp", gcsPath, outputDir)
    37  	return Execute(cmd)
    38  }
    39  
    40  func ExtractTarball(tarballPath, outputDir string) error {
    41  	cmd := exec.Command("tar", "-xvzf", tarballPath, "--directory", outputDir)
    42  	return Execute(cmd)
    43  }
    44  
    45  func KustomizeBuild(path, output string) error {
    46  	cmd := exec.Command("kustomize", "build", path, "--output", output)
    47  	return Execute(cmd)
    48  }
    49  
    50  func Copy(source, dest string) error {
    51  	cmd := exec.Command("cp", source, dest)
    52  	return Execute(cmd)
    53  }
    54  
    55  func Execute(cmd *exec.Cmd) error {
    56  	_, err := ExecuteAndCaptureOutput(cmd)
    57  	return err
    58  }
    59  
    60  func ExecuteAndCaptureOutput(cmd *exec.Cmd) (stdout string, err error) {
    61  	out, err := cmd.CombinedOutput()
    62  	if err != nil {
    63  		return "", fmt.Errorf("%v: %v", err, string(out))
    64  	}
    65  	return string(out), nil
    66  }
    67  

View as plain text