From da57c2795e2042951515a7d3a5ce6f37da873bef Mon Sep 17 00:00:00 2001
From: cuisongliu <cuisongliu@qq.com>
Date: Tue, 10 Oct 2023 11:38:27 +0800
Subject: [PATCH] feature(main): add .sealignore file

Signed-off-by: cuisongliu <cuisongliu@qq.com>
---
 pkg/buildimage/images.go               | 15 +++++++++++
 pkg/buildimage/images_test.go          | 36 ++++++++++++++++++++++++++
 pkg/buildimage/test/ignore/.sealignore |  1 +
 pkg/registry/commands/save.go          |  9 +++++++
 4 files changed, 61 insertions(+)
 create mode 100644 pkg/buildimage/test/ignore/.sealignore

diff --git a/pkg/buildimage/images.go b/pkg/buildimage/images.go
index 0bcf0a3..7072451 100644
--- a/pkg/buildimage/images.go
+++ b/pkg/buildimage/images.go
@@ -120,3 +120,18 @@ func List(dir string) ([]string, error) {
 	list = list.Insert(shimImgs...)
 	return list.List(), nil
 }
+
+func Filter(images []string, ignoreFile string) ([]string, error) {
+	ignore, err := file.ReadLines(ignoreFile)
+	if err != nil {
+		return nil, err
+	}
+	ignoreSet := sets.NewString(ignore...)
+	var res []string
+	for _, img := range images {
+		if !ignoreSet.Has(img) {
+			res = append(res, img)
+		}
+	}
+	return res, nil
+}
diff --git a/pkg/buildimage/images_test.go b/pkg/buildimage/images_test.go
index bc4783a..e3c117c 100644
--- a/pkg/buildimage/images_test.go
+++ b/pkg/buildimage/images_test.go
@@ -15,6 +15,7 @@
 package buildimage
 
 import (
+	"reflect"
 	"testing"
 )
 
@@ -26,3 +27,38 @@ func TestParseYamlImages(t *testing.T) {
 	}
 	t.Logf("%v", data)
 }
+
+func TestFilter(t *testing.T) {
+	type args struct {
+		images     []string
+		ignoreFile string
+	}
+	tests := []struct {
+		name    string
+		args    args
+		want    []string
+		wantErr bool
+	}{
+		{
+			name: "filter",
+			args: args{
+				images:     []string{"docker.io/cilium/istio_proxy", "quay.io/cilium/cilium:v1.12.0", "quay.io/cilium/operator-generic:v1.12.0"},
+				ignoreFile: "test/ignore/.sealignore",
+			},
+			want:    []string{"docker.io/cilium/istio_proxy", "quay.io/cilium/cilium:v1.12.0"},
+			wantErr: false,
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			got, err := Filter(tt.args.images, tt.args.ignoreFile)
+			if (err != nil) != tt.wantErr {
+				t.Errorf("Filter() error = %v, wantErr %v", err, tt.wantErr)
+				return
+			}
+			if !reflect.DeepEqual(got, tt.want) {
+				t.Errorf("Filter() got = %v, want %v", got, tt.want)
+			}
+		})
+	}
+}
diff --git a/pkg/buildimage/test/ignore/.sealignore b/pkg/buildimage/test/ignore/.sealignore
new file mode 100644
index 0000000..bbd17df
--- /dev/null
+++ b/pkg/buildimage/test/ignore/.sealignore
@@ -0,0 +1 @@
+quay.io/cilium/operator-generic:v1.12.0
diff --git a/pkg/registry/commands/save.go b/pkg/registry/commands/save.go
index dafc470..369132e 100644
--- a/pkg/registry/commands/save.go
+++ b/pkg/registry/commands/save.go
@@ -20,6 +20,8 @@ import (
 	"context"
 	"errors"
 	"fmt"
+	"github.com/labring/sreg/pkg/utils/file"
+	"path"
 
 	"github.com/labring/sreg/pkg/registry/save"
 
@@ -84,6 +86,13 @@ func NewRegistryImageSaveCmd(examplePrefix string) *cobra.Command {
 				if err != nil {
 					return err
 				}
+				ignore := path.Join(path.Dir(args[0]), ".sealignore")
+				if file.IsExist(ignore) {
+					images, err = buildimage.Filter(images, ignore)
+					if err != nil {
+						return err
+					}
+				}
 				tars, err = buildimage.TarList(args[0])
 				if err != nil {
 					return err