From 2c3a2f02706b6d4fa8988cce8b1f7dc41030cf91 Mon Sep 17 00:00:00 2001 From: Alan Donham Date: Tue, 3 Dec 2024 12:06:55 -0500 Subject: [PATCH] Run GoFMT --- go/cmd/ocitool/createlayer_cmd.go | 278 +++++++++++++++--------------- go/cmd/ocitool/main.go | 2 +- go/pkg/deb2layer/deb.go | 4 +- go/pkg/deb2layer/deb_test.go | 2 +- go/pkg/ociutil/diff.go | 6 +- 5 files changed, 146 insertions(+), 146 deletions(-) diff --git a/go/cmd/ocitool/createlayer_cmd.go b/go/cmd/ocitool/createlayer_cmd.go index 42546a5..eb4c9a5 100644 --- a/go/cmd/ocitool/createlayer_cmd.go +++ b/go/cmd/ocitool/createlayer_cmd.go @@ -1,155 +1,155 @@ package main import ( - "archive/tar" - "compress/gzip" - "encoding/json" - "fmt" - "io" - "os" - "path" - "path/filepath" - - "github.com/DataDog/zstd" - "github.com/DataDog/rules_oci/go/internal/flagutil" - "github.com/DataDog/rules_oci/go/internal/tarutil" - "github.com/DataDog/rules_oci/go/pkg/ociutil" - "github.com/DataDog/rules_oci/go/pkg/layer" - "github.com/opencontainers/go-digest" - ocispec "github.com/opencontainers/image-spec/specs-go/v1" - "github.com/urfave/cli/v2" + "archive/tar" + "compress/gzip" + "encoding/json" + "fmt" + "io" + "os" + "path" + "path/filepath" + + "github.com/DataDog/rules_oci/go/internal/flagutil" + "github.com/DataDog/rules_oci/go/internal/tarutil" + "github.com/DataDog/rules_oci/go/pkg/layer" + "github.com/DataDog/rules_oci/go/pkg/ociutil" + "github.com/DataDog/zstd" + "github.com/opencontainers/go-digest" + ocispec "github.com/opencontainers/image-spec/specs-go/v1" + "github.com/urfave/cli/v2" ) func CreateLayerCmd(c *cli.Context) error { - config, err := parseConfig(c) - if err != nil { - return fmt.Errorf("problem parsing config: %w", err) - } - - dir := config.Directory - files := config.Files - - out, err := os.Create(config.OutputLayer) - if err != nil { - return err - } - - digester := digest.SHA256.Digester() - wc := ociutil.NewWriterCounter(io.MultiWriter(out, digester.Hash())) - var tw *tar.Writer - var zstdWriter *zstd.Writer - var gzipWriter *gzip.Writer - var mediaType string - - if(config.UseZstd){ - zstdWriter = zstd.NewWriter(wc) - mediaType = ocispec.MediaTypeImageLayerZstd - tw = tar.NewWriter(zstdWriter) - } else { - gzipWriter = gzip.NewWriter(wc) - gzipWriter.Name = path.Base(out.Name()) - mediaType = ocispec.MediaTypeImageLayerGzip - tw = tar.NewWriter(gzipWriter) - } - - defer tw.Close() - - for _, filePath := range files { - err = tarutil.AppendFileToTarWriter(filePath, filepath.Join(dir, filepath.Base(filePath)), tw) - if err != nil { - return err - } - } - - for filePath, storePath := range config.FileMapping { - err = tarutil.AppendFileToTarWriter(filePath, storePath, tw) - if err != nil { - return err - } - } - - for k, v := range config.SymlinkMapping { - err = tw.WriteHeader(&tar.Header{ - Typeflag: tar.TypeSymlink, - Name: k, - Linkname: v, - }) - if err != nil { - return fmt.Errorf("failed to create symlink: %w", err) - } - } - - // Need to flush before we count bytes and digest, might as well close since - // it's not needed anymore. - tw.Close() - if(config.UseZstd){ - zstdWriter.Close() - } else { - gzipWriter.Close() - } - - desc := ocispec.Descriptor{ - MediaType: mediaType, - Size: int64(wc.Count()), - Digest: digester.Digest(), - } - - bazelLabel := config.BazelLabel - if bazelLabel != "" { - desc.Annotations = map[string]string{ - // This will also be added to the image config layer history by append-layers - layer.AnnotationArtifactDescription: bazelLabel, - } - } - - err = ociutil.WriteDescriptorToFile(config.Descriptor, desc) - if err != nil { - return err - } - - return nil + config, err := parseConfig(c) + if err != nil { + return fmt.Errorf("problem parsing config: %w", err) + } + + dir := config.Directory + files := config.Files + + out, err := os.Create(config.OutputLayer) + if err != nil { + return err + } + + digester := digest.SHA256.Digester() + wc := ociutil.NewWriterCounter(io.MultiWriter(out, digester.Hash())) + var tw *tar.Writer + var zstdWriter *zstd.Writer + var gzipWriter *gzip.Writer + var mediaType string + + if config.UseZstd { + zstdWriter = zstd.NewWriter(wc) + mediaType = ocispec.MediaTypeImageLayerZstd + tw = tar.NewWriter(zstdWriter) + } else { + gzipWriter = gzip.NewWriter(wc) + gzipWriter.Name = path.Base(out.Name()) + mediaType = ocispec.MediaTypeImageLayerGzip + tw = tar.NewWriter(gzipWriter) + } + + defer tw.Close() + + for _, filePath := range files { + err = tarutil.AppendFileToTarWriter(filePath, filepath.Join(dir, filepath.Base(filePath)), tw) + if err != nil { + return err + } + } + + for filePath, storePath := range config.FileMapping { + err = tarutil.AppendFileToTarWriter(filePath, storePath, tw) + if err != nil { + return err + } + } + + for k, v := range config.SymlinkMapping { + err = tw.WriteHeader(&tar.Header{ + Typeflag: tar.TypeSymlink, + Name: k, + Linkname: v, + }) + if err != nil { + return fmt.Errorf("failed to create symlink: %w", err) + } + } + + // Need to flush before we count bytes and digest, might as well close since + // it's not needed anymore. + tw.Close() + if config.UseZstd { + zstdWriter.Close() + } else { + gzipWriter.Close() + } + + desc := ocispec.Descriptor{ + MediaType: mediaType, + Size: int64(wc.Count()), + Digest: digester.Digest(), + } + + bazelLabel := config.BazelLabel + if bazelLabel != "" { + desc.Annotations = map[string]string{ + // This will also be added to the image config layer history by append-layers + layer.AnnotationArtifactDescription: bazelLabel, + } + } + + err = ociutil.WriteDescriptorToFile(config.Descriptor, desc) + if err != nil { + return err + } + + return nil } type createLayerConfig struct { - BazelLabel string `json:"bazel-label" toml:"bazel-label" yaml:"bazel-label"` - Descriptor string `json:"outd" toml:"outd" yaml:"outd"` - Directory string `json:"dir" toml:"dir" yaml:"dir"` - Files []string `json:"file" toml:"file" yaml:"file"` - FileMapping map[string]string `json:"file-map" toml:"file-map" yaml:"file-map"` - OutputLayer string `json:"out" toml:"out" yaml:"out"` - SymlinkMapping map[string]string `json:"symlink" toml:"symlink" yaml:"symlink"` - UseZstd bool `json:"zstd-compression" toml:"zstd-compression" yaml:"zstd-compression"` + BazelLabel string `json:"bazel-label" toml:"bazel-label" yaml:"bazel-label"` + Descriptor string `json:"outd" toml:"outd" yaml:"outd"` + Directory string `json:"dir" toml:"dir" yaml:"dir"` + Files []string `json:"file" toml:"file" yaml:"file"` + FileMapping map[string]string `json:"file-map" toml:"file-map" yaml:"file-map"` + OutputLayer string `json:"out" toml:"out" yaml:"out"` + SymlinkMapping map[string]string `json:"symlink" toml:"symlink" yaml:"symlink"` + UseZstd bool `json:"zstd-compression" toml:"zstd-compression" yaml:"zstd-compression"` } func newCreateLayerConfig(c *cli.Context) *createLayerConfig { - return &createLayerConfig{ - BazelLabel: c.String("bazel-label"), - Directory: c.String("dir"), - Files: c.StringSlice("file"), - FileMapping: c.Generic("file-map").(*flagutil.KeyValueFlag).Map, - OutputLayer: c.String("out"), - Descriptor: c.String("outd"), - SymlinkMapping: c.Generic("symlink").(*flagutil.KeyValueFlag).Map, - UseZstd: c.Bool("zstd-compression"), - } + return &createLayerConfig{ + BazelLabel: c.String("bazel-label"), + Directory: c.String("dir"), + Files: c.StringSlice("file"), + FileMapping: c.Generic("file-map").(*flagutil.KeyValueFlag).Map, + OutputLayer: c.String("out"), + Descriptor: c.String("outd"), + SymlinkMapping: c.Generic("symlink").(*flagutil.KeyValueFlag).Map, + UseZstd: c.Bool("zstd-compression"), + } } func parseConfig(c *cli.Context) (*createLayerConfig, error) { - configFile := c.Path("configuration-file") - if configFile == "" { - return newCreateLayerConfig(c), nil - } - - file, err := os.ReadFile(configFile) - if err != nil { - return nil, fmt.Errorf("problem reading config file: %w", err) - } - - var config createLayerConfig - err = json.Unmarshal(file, &config) - if err != nil { - return nil, fmt.Errorf("problem parsing config file as JSON: %w", err) - } - - return &config, nil + configFile := c.Path("configuration-file") + if configFile == "" { + return newCreateLayerConfig(c), nil + } + + file, err := os.ReadFile(configFile) + if err != nil { + return nil, fmt.Errorf("problem reading config file: %w", err) + } + + var config createLayerConfig + err = json.Unmarshal(file, &config) + if err != nil { + return nil, fmt.Errorf("problem parsing config file as JSON: %w", err) + } + + return &config, nil } diff --git a/go/cmd/ocitool/main.go b/go/cmd/ocitool/main.go index 4370700..d14ec50 100644 --- a/go/cmd/ocitool/main.go +++ b/go/cmd/ocitool/main.go @@ -70,7 +70,7 @@ var app = &cli.App{ Name: "file-map", Value: &flagutil.KeyValueFlag{}, }, - &cli.BoolFlag{ + &cli.BoolFlag{ Name: "zstd-compression", Usage: "Use the zstd library for compression.", Value: false, diff --git a/go/pkg/deb2layer/deb.go b/go/pkg/deb2layer/deb.go index fe769cf..00ea67d 100644 --- a/go/pkg/deb2layer/deb.go +++ b/go/pkg/deb2layer/deb.go @@ -326,8 +326,8 @@ func extToReader(ext string, inReader io.Reader) (io.Reader, error) { } case ".tar": outReader = tar.NewReader(inReader) - case ".zst": - outReader = zstd.NewReader(inReader) + case ".zst": + outReader = zstd.NewReader(inReader) default: return nil, fmt.Errorf("unknown extension %q", ext) } diff --git a/go/pkg/deb2layer/deb_test.go b/go/pkg/deb2layer/deb_test.go index 5fa119c..2eb60c9 100644 --- a/go/pkg/deb2layer/deb_test.go +++ b/go/pkg/deb2layer/deb_test.go @@ -20,7 +20,7 @@ var ( // TestDebToLayer checks that the expected files are in the resulting layer // based on a deb file created by Bazel's rules_pkg func TestDebToLayer(t *testing.T) { - // XXX Fix generating deb package + // XXX Fix generating deb package f, err := os.Open("testdeb.deb") if err != nil { t.Fatalf("%v", err) diff --git a/go/pkg/ociutil/diff.go b/go/pkg/ociutil/diff.go index 4f57255..dc5ff73 100644 --- a/go/pkg/ociutil/diff.go +++ b/go/pkg/ociutil/diff.go @@ -4,7 +4,7 @@ import ( "compress/gzip" "context" "fmt" - "github.com/DataDog/zstd" + "github.com/DataDog/zstd" "github.com/containerd/containerd/content" "github.com/opencontainers/go-digest" ocispec "github.com/opencontainers/image-spec/specs-go/v1" @@ -28,8 +28,8 @@ func GetLayerDiffID(ctx context.Context, store content.Store, desc ocispec.Descr } return digest.SHA256.FromReader(gr) - case ocispec.MediaTypeImageLayerZstd: - r, err := store.ReaderAt(ctx, desc) + case ocispec.MediaTypeImageLayerZstd: + r, err := store.ReaderAt(ctx, desc) if err != nil { return "", fmt.Errorf("failed to get reader for layer: %w", err) }