Skip to content

Commit

Permalink
Run GoFMT
Browse files Browse the repository at this point in the history
  • Loading branch information
alandonham committed Dec 3, 2024
1 parent 72246d8 commit 2c3a2f0
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 146 deletions.
278 changes: 139 additions & 139 deletions go/cmd/ocitool/createlayer_cmd.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion go/cmd/ocitool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions go/pkg/deb2layer/deb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion go/pkg/deb2layer/deb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions go/pkg/ociutil/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
Expand Down

0 comments on commit 2c3a2f0

Please sign in to comment.