|
|
|
@ -17,7 +17,10 @@ limitations under the License.
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bufio"
|
|
|
|
|
"bytes"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
@ -54,7 +57,7 @@ var excludeOCI = append(strings.Split(sourceignore.ExcludeVCS, ","), strings.Spl
|
|
|
|
|
var buildArtifactArgs buildArtifactFlags
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
buildArtifactCmd.Flags().StringVar(&buildArtifactArgs.path, "path", "", "Path to the directory where the Kubernetes manifests are located.")
|
|
|
|
|
buildArtifactCmd.Flags().StringVarP(&buildArtifactArgs.path, "path", "p", "", "Path to the directory where the Kubernetes manifests are located.")
|
|
|
|
|
buildArtifactCmd.Flags().StringVarP(&buildArtifactArgs.output, "output", "o", "artifact.tgz", "Path to where the artifact tgz file should be written.")
|
|
|
|
|
buildArtifactCmd.Flags().StringSliceVar(&buildArtifactArgs.ignorePaths, "ignore-paths", excludeOCI, "set paths to ignore in .gitignore format")
|
|
|
|
|
|
|
|
|
@ -66,18 +69,47 @@ func buildArtifactCmdRun(cmd *cobra.Command, args []string) error {
|
|
|
|
|
return fmt.Errorf("invalid path %q", buildArtifactArgs.path)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, err := os.Stat(buildArtifactArgs.path); err != nil {
|
|
|
|
|
return fmt.Errorf("invalid path '%s', must point to an existing directory or file", buildArtifactArgs.path)
|
|
|
|
|
path := buildArtifactArgs.path
|
|
|
|
|
var err error
|
|
|
|
|
if buildArtifactArgs.path == "-" {
|
|
|
|
|
path, err = saveStdinToFile()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer os.Remove(path)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, err := os.Stat(path); err != nil {
|
|
|
|
|
return fmt.Errorf("invalid path '%s', must point to an existing directory or file", path)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.Actionf("building artifact from %s", buildArtifactArgs.path)
|
|
|
|
|
logger.Actionf("building artifact from %s", path)
|
|
|
|
|
|
|
|
|
|
ociClient := oci.NewLocalClient()
|
|
|
|
|
if err := ociClient.Build(buildArtifactArgs.output, buildArtifactArgs.path, buildArtifactArgs.ignorePaths); err != nil {
|
|
|
|
|
if err := ociClient.Build(buildArtifactArgs.output, path, buildArtifactArgs.ignorePaths); err != nil {
|
|
|
|
|
return fmt.Errorf("bulding artifact failed, error: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.Successf("artifact created at %s", buildArtifactArgs.output)
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func saveStdinToFile() (string, error) {
|
|
|
|
|
b, err := io.ReadAll(bufio.NewReader(os.Stdin))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
b = bytes.TrimRight(b, "\r\n")
|
|
|
|
|
f, err := os.CreateTemp("", "*.yaml")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", fmt.Errorf("unable to create temp dir for stdin")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = f.Write(b)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", fmt.Errorf("error writing stdin to file: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return f.Name(), nil
|
|
|
|
|
}
|
|
|
|
|