From f58597ade9e73ae0e296defaf13375afb92839c4 Mon Sep 17 00:00:00 2001 From: Alexei Dodon Date: Sat, 16 Sep 2023 01:17:01 +0300 Subject: [PATCH] refactor: Reduce zli binary size (#1805) Signed-off-by: Alexei Dodon --- .github/workflows/compare-binary-size.yml | 26 +++++++++++- Makefile | 4 +- cmd/zb/helper.go | 36 +--------------- cmd/zli/main.go | 5 ++- cmd/zli/main_test.go | 5 ++- cmd/zot/main.go | 2 +- cmd/zot/main_test.go | 2 +- codecov.yml | 1 - pkg/cli/{ => client}/cli.go | 2 +- pkg/cli/{ => client}/client.go | 2 +- pkg/cli/{ => client}/config_cmd.go | 2 +- .../config_cmd_internal_test.go} | 2 +- .../cve_cmd_internal_test.go} | 2 +- pkg/cli/{ => client}/cves_cmd.go | 2 +- pkg/cli/{ => client}/cves_sub_cmd.go | 2 +- pkg/cli/{ => client}/discover.go | 2 +- .../elevated_internal_test.go} | 4 +- pkg/cli/{ => client}/gql_queries.go | 2 +- .../gql_queries_internal_test.go} | 2 +- pkg/cli/{ => client}/image_cmd.go | 2 +- .../image_cmd_internal_test.go} | 12 +++--- pkg/cli/{ => client}/image_sub_cmd.go | 2 +- .../internal_test.go} | 10 ++--- pkg/cli/{ => client}/repo_cmd.go | 2 +- .../repo_internal_test.go} | 2 +- pkg/cli/{ => client}/repo_sub_cmd.go | 2 +- pkg/cli/client/root.go | 42 +++++++++++++++++++ pkg/cli/client/root_test.go | 31 ++++++++++++++ pkg/cli/{ => client}/search_cmd.go | 2 +- .../search_cmd_internal_test.go} | 2 +- pkg/cli/{ => client}/search_functions.go | 2 +- .../search_functions_internal_test.go} | 2 +- pkg/cli/{ => client}/search_sub_cmd.go | 2 +- pkg/cli/{ => client}/service.go | 2 +- pkg/cli/{ => client}/utils.go | 2 +- .../utils_internal_test.go} | 2 +- pkg/cli/minimal.go | 9 ---- pkg/cli/{ => server}/config_reloader.go | 2 +- pkg/cli/{ => server}/config_reloader_test.go | 4 +- pkg/cli/{ => server}/extensions_test.go | 4 +- pkg/cli/{ => server}/root.go | 32 +------------- pkg/cli/{ => server}/root_test.go | 32 ++++---------- pkg/cli/{ => server}/stress_test.go | 4 +- pkg/extensions/sync/sync_test.go | 2 +- pkg/storage/s3/s3_test.go | 6 +-- 45 files changed, 166 insertions(+), 155 deletions(-) rename pkg/cli/{ => client}/cli.go (96%) rename pkg/cli/{ => client}/client.go (99%) rename pkg/cli/{ => client}/config_cmd.go (99%) rename pkg/cli/{config_cmd_test.go => client/config_cmd_internal_test.go} (99%) rename pkg/cli/{cve_cmd_test.go => client/cve_cmd_internal_test.go} (99%) rename pkg/cli/{ => client}/cves_cmd.go (98%) rename pkg/cli/{ => client}/cves_sub_cmd.go (99%) rename pkg/cli/{ => client}/discover.go (99%) rename pkg/cli/{client_elevated_test.go => client/elevated_internal_test.go} (97%) rename pkg/cli/{ => client}/gql_queries.go (99%) rename pkg/cli/{gql_queries_test.go => client/gql_queries_internal_test.go} (98%) rename pkg/cli/{ => client}/image_cmd.go (99%) rename pkg/cli/{image_cmd_test.go => client/image_cmd_internal_test.go} (99%) rename pkg/cli/{ => client}/image_sub_cmd.go (99%) rename pkg/cli/{client_test.go => client/internal_test.go} (97%) rename pkg/cli/{ => client}/repo_cmd.go (98%) rename pkg/cli/{repo_test.go => client/repo_internal_test.go} (98%) rename pkg/cli/{ => client}/repo_sub_cmd.go (98%) create mode 100644 pkg/cli/client/root.go create mode 100644 pkg/cli/client/root_test.go rename pkg/cli/{ => client}/search_cmd.go (98%) rename pkg/cli/{search_cmd_test.go => client/search_cmd_internal_test.go} (99%) rename pkg/cli/{ => client}/search_functions.go (99%) rename pkg/cli/{search_functions_test.go => client/search_functions_internal_test.go} (99%) rename pkg/cli/{ => client}/search_sub_cmd.go (99%) rename pkg/cli/{ => client}/service.go (99%) rename pkg/cli/{ => client}/utils.go (99%) rename pkg/cli/{client_utils_test.go => client/utils_internal_test.go} (99%) delete mode 100644 pkg/cli/minimal.go rename pkg/cli/{ => server}/config_reloader.go (99%) rename pkg/cli/{ => server}/config_reloader_test.go (99%) rename pkg/cli/{ => server}/extensions_test.go (99%) rename pkg/cli/{ => server}/root.go (97%) rename pkg/cli/{ => server}/root_test.go (98%) rename pkg/cli/{ => server}/stress_test.go (98%) diff --git a/.github/workflows/compare-binary-size.yml b/.github/workflows/compare-binary-size.yml index e39782c3..248f6463 100644 --- a/.github/workflows/compare-binary-size.yml +++ b/.github/workflows/compare-binary-size.yml @@ -33,7 +33,7 @@ jobs: echo "PR binary size: $BINSIZE Bytes" echo "main branch binary size: $BINSIZE_MAIN Bytes" - [[ $BINSIZE -eq $BINSIZE_MAIN ]] && echo "zot-minimal binary size is not affected by PR" && exit 0 + [[ $BINSIZE -eq $BINSIZE_MAIN ]] && echo "zot-minimal binary size is not affected by PR" && exit 0 if [[ $BINSIZE -gt $BINSIZE_MAIN ]]; then \ PERCENTAGE=$(echo "scale=2; (($BINSIZE-$BINSIZE_MAIN)*100)/$BINSIZE_MAIN" | bc); \ @@ -58,7 +58,7 @@ jobs: echo "PR binary size: $BINSIZE Bytes" echo "main branch binary size: $BINSIZE_MAIN Bytes" - [[ $BINSIZE -eq $BINSIZE_MAIN ]] && echo "zb binary size is not affected by PR" && exit 0 + [[ $BINSIZE -eq $BINSIZE_MAIN ]] && echo "zb binary size is not affected by PR" && exit 0 if [[ $BINSIZE -gt $BINSIZE_MAIN ]]; then \ PERCENTAGE=$(echo "scale=2; (($BINSIZE-$BINSIZE_MAIN)*100)/$BINSIZE_MAIN" | bc); \ @@ -68,6 +68,28 @@ jobs: PERCENTAGE=$(echo "scale=2; (($BINSIZE_MAIN-$BINSIZE)*100)/$BINSIZE_MAIN" | bc); \ echo "zb binary decreased by $PERCENTAGE% comparing with main"; \ fi + - if: always() + name: Check if zli binary increased with more than 1% + run: | + echo "Building zli and check size" + cd $GITHUB_WORKSPACE + make cli + BINSIZE=$(stat -c%s "bin/zli-linux-amd64") + echo "Building zli on main branch and check size" + cd zot_main + make cli + BINSIZE_MAIN=$(stat -c%s "bin/zli-linux-amd64") + echo "PR binary size: $BINSIZE Bytes" + echo "main branch binary size: $BINSIZE_MAIN Bytes" + [[ $BINSIZE -eq $BINSIZE_MAIN ]] && echo "zli binary size is not affected by PR" && exit 0 + if [[ $BINSIZE -gt $BINSIZE_MAIN ]]; then \ + PERCENTAGE=$(echo "scale=2; (($BINSIZE-$BINSIZE_MAIN)*100)/$BINSIZE_MAIN" | bc); \ + echo "zli binary increased by $PERCENTAGE% comparing with main"; \ + if ((`bc <<< "$PERCENTAGE>=1.0"`)); then exit 1; fi; \ + else \ + PERCENTAGE=$(echo "scale=2; (($BINSIZE_MAIN-$BINSIZE)*100)/$BINSIZE_MAIN" | bc); \ + echo "zli binary decreased by $PERCENTAGE% comparing with main"; \ + fi diff --git a/Makefile b/Makefile index f9b93a39..08c6268b 100644 --- a/Makefile +++ b/Makefile @@ -137,12 +137,12 @@ test: check-skopeo $(TESTDATA) $(ORAS) rm -rf /tmp/getter*; rm -rf /tmp/trivy* go test -failfast -tags dev,containers_image_openpgp -v -trimpath -race -cover -coverpkg ./... -coverprofile=coverage-dev-minimal.txt -covermode=atomic ./pkg/test/... ./pkg/storage/... ./pkg/extensions/sync/... -run ^TestInject rm -rf /tmp/getter*; rm -rf /tmp/trivy* - go test -failfast -tags stress,$(BUILD_LABELS),containers_image_openpgp -v -trimpath -race -timeout 15m ./pkg/cli/stress_test.go + go test -failfast -tags stress,$(BUILD_LABELS),containers_image_openpgp -v -trimpath -race -timeout 15m ./pkg/cli/server/stress_test.go .PHONY: privileged-test privileged-test: $(if $(findstring ui,$(BUILD_LABELS)), ui) privileged-test: check-skopeo $(TESTDATA) - go test -failfast -tags needprivileges,$(BUILD_LABELS),containers_image_openpgp -v -trimpath -race -timeout 15m -cover -coverpkg ./... -coverprofile=coverage-dev-needprivileges.txt -covermode=atomic ./pkg/storage/... ./pkg/cli/... -run ^TestElevatedPrivileges + go test -failfast -tags needprivileges,$(BUILD_LABELS),containers_image_openpgp -v -trimpath -race -timeout 15m -cover -coverpkg ./... -coverprofile=coverage-dev-needprivileges.txt -covermode=atomic ./pkg/storage/local/... ./pkg/cli/client/... -run ^TestElevatedPrivileges $(TESTDATA): check-skopeo mkdir -p ${TESTDATA}; \ diff --git a/cmd/zb/helper.go b/cmd/zb/helper.go index ba24cfd9..68a945b9 100644 --- a/cmd/zb/helper.go +++ b/cmd/zb/helper.go @@ -2,12 +2,10 @@ package main import ( "bytes" - crand "crypto/rand" "encoding/json" "fmt" "io" "log" - "math/big" "math/rand" "net/http" "os" @@ -24,6 +22,7 @@ import ( zerr "zotregistry.io/zot/errors" "zotregistry.io/zot/pkg/common" testc "zotregistry.io/zot/pkg/test/common" + "zotregistry.io/zot/pkg/test/image-utils" ) func makeHTTPGetRequest(url string, resultPtr interface{}, client *resty.Client) error { @@ -1023,21 +1022,7 @@ func loadOrStore(statusRequests *sync.Map, key string, value int) int { //nolint // TO DO: replace with pkg/test/images when available. func getRandomImageConfig() ([]byte, godigest.Digest) { - const maxLen = 16 - - randomAuthor := randomString(maxLen) - - config := ispec.Image{ - Platform: ispec.Platform{ - Architecture: "amd64", - OS: "linux", - }, - RootFS: ispec.RootFS{ - Type: "layers", - DiffIDs: []godigest.Digest{}, - }, - Author: randomAuthor, - } + config := image.GetDefaultConfig() configBlobContent, err := json.MarshalIndent(&config, "", "\t") if err != nil { @@ -1048,20 +1033,3 @@ func getRandomImageConfig() ([]byte, godigest.Digest) { return configBlobContent, configBlobDigestRaw } - -func randomString(n int) string { - const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-" - - ret := make([]byte, n) - - for count := 0; count < n; count++ { - num, err := crand.Int(crand.Reader, big.NewInt(int64(len(letters)))) - if err != nil { - panic(err) - } - - ret[count] = letters[num.Int64()] - } - - return string(ret) -} diff --git a/cmd/zli/main.go b/cmd/zli/main.go index c68df568..02d89194 100644 --- a/cmd/zli/main.go +++ b/cmd/zli/main.go @@ -1,9 +1,12 @@ +//go:build search +// +build search + package main import ( "os" - "zotregistry.io/zot/pkg/cli" + cli "zotregistry.io/zot/pkg/cli/client" ) func main() { diff --git a/cmd/zli/main_test.go b/cmd/zli/main_test.go index 9e1af2e8..f798e91c 100644 --- a/cmd/zli/main_test.go +++ b/cmd/zli/main_test.go @@ -1,3 +1,6 @@ +//go:build search +// +build search + package main_test import ( @@ -7,7 +10,7 @@ import ( "zotregistry.io/zot/pkg/api" "zotregistry.io/zot/pkg/api/config" - "zotregistry.io/zot/pkg/cli" + cli "zotregistry.io/zot/pkg/cli/client" ) func TestIntegration(t *testing.T) { diff --git a/cmd/zot/main.go b/cmd/zot/main.go index 16d2e152..ec6d0b97 100644 --- a/cmd/zot/main.go +++ b/cmd/zot/main.go @@ -3,7 +3,7 @@ package main import ( "os" - "zotregistry.io/zot/pkg/cli" + cli "zotregistry.io/zot/pkg/cli/server" ) func main() { diff --git a/cmd/zot/main_test.go b/cmd/zot/main_test.go index ccfbb4ac..500b8b71 100644 --- a/cmd/zot/main_test.go +++ b/cmd/zot/main_test.go @@ -7,7 +7,7 @@ import ( "zotregistry.io/zot/pkg/api" "zotregistry.io/zot/pkg/api/config" - "zotregistry.io/zot/pkg/cli" + cli "zotregistry.io/zot/pkg/cli/server" ) func TestIntegration(t *testing.T) { diff --git a/codecov.yml b/codecov.yml index 915464c2..913bdf3a 100644 --- a/codecov.yml +++ b/codecov.yml @@ -2,7 +2,6 @@ ignore: - "*_gen.go" - "./pkg/extensions/search/gql_generated" - "./pkg/extensions/minimal.go" - - "./pkg/cli/minimal.go" - "./cmd/zb/*.go" - "./pkg/test/mocks/*.go" - "./swagger/*.go" diff --git a/pkg/cli/cli.go b/pkg/cli/client/cli.go similarity index 96% rename from pkg/cli/cli.go rename to pkg/cli/client/cli.go index db5b6de2..1c0171a7 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/client/cli.go @@ -1,7 +1,7 @@ //go:build search // +build search -package cli +package client import "github.com/spf13/cobra" diff --git a/pkg/cli/client.go b/pkg/cli/client/client.go similarity index 99% rename from pkg/cli/client.go rename to pkg/cli/client/client.go index 3e6775ee..fc74d77c 100644 --- a/pkg/cli/client.go +++ b/pkg/cli/client/client.go @@ -1,7 +1,7 @@ //go:build search // +build search -package cli +package client import ( "bytes" diff --git a/pkg/cli/config_cmd.go b/pkg/cli/client/config_cmd.go similarity index 99% rename from pkg/cli/config_cmd.go rename to pkg/cli/client/config_cmd.go index f8391054..cf32d0c3 100644 --- a/pkg/cli/config_cmd.go +++ b/pkg/cli/client/config_cmd.go @@ -1,7 +1,7 @@ //go:build search // +build search -package cli +package client import ( "errors" diff --git a/pkg/cli/config_cmd_test.go b/pkg/cli/client/config_cmd_internal_test.go similarity index 99% rename from pkg/cli/config_cmd_test.go rename to pkg/cli/client/config_cmd_internal_test.go index dd9df9e2..7ba519d2 100644 --- a/pkg/cli/config_cmd_test.go +++ b/pkg/cli/client/config_cmd_internal_test.go @@ -1,7 +1,7 @@ //go:build search // +build search -package cli //nolint:testpackage +package client import ( "bytes" diff --git a/pkg/cli/cve_cmd_test.go b/pkg/cli/client/cve_cmd_internal_test.go similarity index 99% rename from pkg/cli/cve_cmd_test.go rename to pkg/cli/client/cve_cmd_internal_test.go index 95b4a281..c6807c25 100644 --- a/pkg/cli/cve_cmd_test.go +++ b/pkg/cli/client/cve_cmd_internal_test.go @@ -1,7 +1,7 @@ //go:build search // +build search -package cli //nolint:testpackage +package client import ( "bytes" diff --git a/pkg/cli/cves_cmd.go b/pkg/cli/client/cves_cmd.go similarity index 98% rename from pkg/cli/cves_cmd.go rename to pkg/cli/client/cves_cmd.go index da592432..88cb0421 100644 --- a/pkg/cli/cves_cmd.go +++ b/pkg/cli/client/cves_cmd.go @@ -1,7 +1,7 @@ //go:build search // +build search -package cli +package client import ( "github.com/spf13/cobra" diff --git a/pkg/cli/cves_sub_cmd.go b/pkg/cli/client/cves_sub_cmd.go similarity index 99% rename from pkg/cli/cves_sub_cmd.go rename to pkg/cli/client/cves_sub_cmd.go index 6baf5401..4f07cf08 100644 --- a/pkg/cli/cves_sub_cmd.go +++ b/pkg/cli/client/cves_sub_cmd.go @@ -1,7 +1,7 @@ //go:build search // +build search -package cli +package client import ( "fmt" diff --git a/pkg/cli/discover.go b/pkg/cli/client/discover.go similarity index 99% rename from pkg/cli/discover.go rename to pkg/cli/client/discover.go index e922fd1c..76979589 100644 --- a/pkg/cli/discover.go +++ b/pkg/cli/client/discover.go @@ -1,7 +1,7 @@ //go:build search // +build search -package cli +package client import ( "context" diff --git a/pkg/cli/client_elevated_test.go b/pkg/cli/client/elevated_internal_test.go similarity index 97% rename from pkg/cli/client_elevated_test.go rename to pkg/cli/client/elevated_internal_test.go index 8d867bf1..bbe55fdc 100644 --- a/pkg/cli/client_elevated_test.go +++ b/pkg/cli/client/elevated_internal_test.go @@ -1,7 +1,7 @@ //go:build search && needprivileges // +build search,needprivileges -package cli //nolint:testpackage +package client import ( "bytes" @@ -33,7 +33,7 @@ func TestElevatedPrivilegesTLSNewControllerPrivilegedCert(t *testing.T) { defer exec.Command("rm", "-rf", "/etc/containers/certs.d/127.0.0.1:8089/") workDir, _ := os.Getwd() - _ = os.Chdir("../../test/data") + _ = os.Chdir("../../../test/data") clientGlob, _ := filepath.Glob("client.*") caGlob, _ := filepath.Glob("ca.*") diff --git a/pkg/cli/gql_queries.go b/pkg/cli/client/gql_queries.go similarity index 99% rename from pkg/cli/gql_queries.go rename to pkg/cli/client/gql_queries.go index e006e3a5..bfa84e1e 100644 --- a/pkg/cli/gql_queries.go +++ b/pkg/cli/client/gql_queries.go @@ -1,7 +1,7 @@ //go:build search // +build search -package cli +package client type GQLField struct { Name string diff --git a/pkg/cli/gql_queries_test.go b/pkg/cli/client/gql_queries_internal_test.go similarity index 98% rename from pkg/cli/gql_queries_test.go rename to pkg/cli/client/gql_queries_internal_test.go index a9ae1861..d60e08b1 100644 --- a/pkg/cli/gql_queries_test.go +++ b/pkg/cli/client/gql_queries_internal_test.go @@ -1,7 +1,7 @@ //go:build search // +build search -package cli //nolint:testpackage +package client import ( "io" diff --git a/pkg/cli/image_cmd.go b/pkg/cli/client/image_cmd.go similarity index 99% rename from pkg/cli/image_cmd.go rename to pkg/cli/client/image_cmd.go index 842de758..c3a8291d 100644 --- a/pkg/cli/image_cmd.go +++ b/pkg/cli/client/image_cmd.go @@ -1,7 +1,7 @@ //go:build search // +build search -package cli +package client import ( "strconv" diff --git a/pkg/cli/image_cmd_test.go b/pkg/cli/client/image_cmd_internal_test.go similarity index 99% rename from pkg/cli/image_cmd_test.go rename to pkg/cli/client/image_cmd_internal_test.go index e43a88a7..cce35f79 100644 --- a/pkg/cli/image_cmd_test.go +++ b/pkg/cli/client/image_cmd_internal_test.go @@ -1,7 +1,7 @@ //go:build search // +build search -package cli //nolint:testpackage +package client import ( "bytes" @@ -2686,11 +2686,11 @@ func makeConfigFile(content string) string { func getTestSearchConfig(url string, searchService SearchService) searchConfig { var ( - user string = "" - outputFormat string = "" - verbose bool = false - debug bool = false - verifyTLS bool = false + user string + outputFormat string + verbose bool + debug bool + verifyTLS bool ) return searchConfig{ diff --git a/pkg/cli/image_sub_cmd.go b/pkg/cli/client/image_sub_cmd.go similarity index 99% rename from pkg/cli/image_sub_cmd.go rename to pkg/cli/client/image_sub_cmd.go index b96c0a52..17b6b3fa 100644 --- a/pkg/cli/image_sub_cmd.go +++ b/pkg/cli/client/image_sub_cmd.go @@ -1,7 +1,7 @@ //go:build search // +build search -package cli +package client import ( "fmt" diff --git a/pkg/cli/client_test.go b/pkg/cli/client/internal_test.go similarity index 97% rename from pkg/cli/client_test.go rename to pkg/cli/client/internal_test.go index a5c54efc..b184de49 100644 --- a/pkg/cli/client_test.go +++ b/pkg/cli/client/internal_test.go @@ -1,7 +1,7 @@ //go:build search // +build search -package cli //nolint:testpackage +package client import ( "bytes" @@ -35,10 +35,10 @@ const ( SecurePort3 = "8090" username = "test" passphrase = "test" - ServerCert = "../../test/data/server.cert" - ServerKey = "../../test/data/server.key" - CACert = "../../test/data/ca.crt" - sourceCertsDir = "../../test/data" + ServerCert = "../../../test/data/server.cert" + ServerKey = "../../../test/data/server.key" + CACert = "../../../test/data/ca.crt" + sourceCertsDir = "../../../test/data" certsDir1 = "/.config/containers/certs.d/127.0.0.1:8088/" ) diff --git a/pkg/cli/repo_cmd.go b/pkg/cli/client/repo_cmd.go similarity index 98% rename from pkg/cli/repo_cmd.go rename to pkg/cli/client/repo_cmd.go index 248d04bd..972c34c6 100644 --- a/pkg/cli/repo_cmd.go +++ b/pkg/cli/client/repo_cmd.go @@ -1,7 +1,7 @@ //go:build search // +build search -package cli +package client import ( "github.com/spf13/cobra" diff --git a/pkg/cli/repo_test.go b/pkg/cli/client/repo_internal_test.go similarity index 98% rename from pkg/cli/repo_test.go rename to pkg/cli/client/repo_internal_test.go index f585c42f..4fec85d3 100644 --- a/pkg/cli/repo_test.go +++ b/pkg/cli/client/repo_internal_test.go @@ -1,7 +1,7 @@ //go:build search // +build search -package cli //nolint:testpackage +package client import ( "bytes" diff --git a/pkg/cli/repo_sub_cmd.go b/pkg/cli/client/repo_sub_cmd.go similarity index 98% rename from pkg/cli/repo_sub_cmd.go rename to pkg/cli/client/repo_sub_cmd.go index b8ff046f..be98813b 100644 --- a/pkg/cli/repo_sub_cmd.go +++ b/pkg/cli/client/repo_sub_cmd.go @@ -1,7 +1,7 @@ //go:build search // +build search -package cli +package client import ( "fmt" diff --git a/pkg/cli/client/root.go b/pkg/cli/client/root.go new file mode 100644 index 00000000..c9d879ab --- /dev/null +++ b/pkg/cli/client/root.go @@ -0,0 +1,42 @@ +//go:build search +// +build search + +package client + +import ( + distspec "github.com/opencontainers/distribution-spec/specs-go" + "github.com/rs/zerolog/log" + "github.com/spf13/cobra" + + "zotregistry.io/zot/pkg/api/config" + "zotregistry.io/zot/pkg/cli/cmdflags" +) + +// "zli" - client-side cli. +func NewCliRootCmd() *cobra.Command { + showVersion := false + + rootCmd := &cobra.Command{ + Use: "zli", + Short: "`zli`", + Long: "`zli`", + Run: func(cmd *cobra.Command, args []string) { + if showVersion { + log.Info().Str("distribution-spec", distspec.Version).Str("commit", config.Commit). + Str("binary-type", config.BinaryType).Str("go version", config.GoVersion).Msg("version") + } else { + _ = cmd.Usage() + cmd.SilenceErrors = false + } + }, + } + + rootCmd.SilenceUsage = true + + // additional cmds + enableCli(rootCmd) + // "version" + rootCmd.Flags().BoolVarP(&showVersion, cmdflags.VersionFlag, "v", false, "show the version and exit") + + return rootCmd +} diff --git a/pkg/cli/client/root_test.go b/pkg/cli/client/root_test.go new file mode 100644 index 00000000..5be262a7 --- /dev/null +++ b/pkg/cli/client/root_test.go @@ -0,0 +1,31 @@ +//go:build search +// +build search + +package client_test + +import ( + "os" + "testing" + + . "github.com/smartystreets/goconvey/convey" + + "zotregistry.io/zot/pkg/cli/client" +) + +func TestCliUsage(t *testing.T) { + oldArgs := os.Args + + defer func() { os.Args = oldArgs }() + + Convey("Test usage", t, func(c C) { + os.Args = []string{"cli_test", "help"} + err := client.NewCliRootCmd().Execute() + So(err, ShouldBeNil) + }) + + Convey("Test version", t, func(c C) { + os.Args = []string{"cli_test", "--version"} + err := client.NewCliRootCmd().Execute() + So(err, ShouldBeNil) + }) +} diff --git a/pkg/cli/search_cmd.go b/pkg/cli/client/search_cmd.go similarity index 98% rename from pkg/cli/search_cmd.go rename to pkg/cli/client/search_cmd.go index d00baeb4..3ef9f950 100644 --- a/pkg/cli/search_cmd.go +++ b/pkg/cli/client/search_cmd.go @@ -1,7 +1,7 @@ //go:build search // +build search -package cli +package client import ( "github.com/spf13/cobra" diff --git a/pkg/cli/search_cmd_test.go b/pkg/cli/client/search_cmd_internal_test.go similarity index 99% rename from pkg/cli/search_cmd_test.go rename to pkg/cli/client/search_cmd_internal_test.go index db79cd41..e267c80a 100644 --- a/pkg/cli/search_cmd_test.go +++ b/pkg/cli/client/search_cmd_internal_test.go @@ -1,7 +1,7 @@ //go:build search // +build search -package cli //nolint:testpackage +package client import ( "bytes" diff --git a/pkg/cli/search_functions.go b/pkg/cli/client/search_functions.go similarity index 99% rename from pkg/cli/search_functions.go rename to pkg/cli/client/search_functions.go index feb51ea9..e400742f 100644 --- a/pkg/cli/search_functions.go +++ b/pkg/cli/client/search_functions.go @@ -1,7 +1,7 @@ //go:build search // +build search -package cli +package client import ( "context" diff --git a/pkg/cli/search_functions_test.go b/pkg/cli/client/search_functions_internal_test.go similarity index 99% rename from pkg/cli/search_functions_test.go rename to pkg/cli/client/search_functions_internal_test.go index 286318ce..3a703ed9 100644 --- a/pkg/cli/search_functions_test.go +++ b/pkg/cli/client/search_functions_internal_test.go @@ -3,7 +3,7 @@ // //nolint:dupl -package cli //nolint:testpackage +package client import ( "bytes" diff --git a/pkg/cli/search_sub_cmd.go b/pkg/cli/client/search_sub_cmd.go similarity index 99% rename from pkg/cli/search_sub_cmd.go rename to pkg/cli/client/search_sub_cmd.go index 341f55e3..94005084 100644 --- a/pkg/cli/search_sub_cmd.go +++ b/pkg/cli/client/search_sub_cmd.go @@ -1,7 +1,7 @@ //go:build search // +build search -package cli +package client import ( "fmt" diff --git a/pkg/cli/service.go b/pkg/cli/client/service.go similarity index 99% rename from pkg/cli/service.go rename to pkg/cli/client/service.go index d869c293..2e999f4f 100644 --- a/pkg/cli/service.go +++ b/pkg/cli/client/service.go @@ -1,7 +1,7 @@ //go:build search // +build search -package cli +package client import ( "context" diff --git a/pkg/cli/utils.go b/pkg/cli/client/utils.go similarity index 99% rename from pkg/cli/utils.go rename to pkg/cli/client/utils.go index 2ef07fd4..a6570193 100644 --- a/pkg/cli/utils.go +++ b/pkg/cli/client/utils.go @@ -1,7 +1,7 @@ //go:build search // +build search -package cli +package client import ( "context" diff --git a/pkg/cli/client_utils_test.go b/pkg/cli/client/utils_internal_test.go similarity index 99% rename from pkg/cli/client_utils_test.go rename to pkg/cli/client/utils_internal_test.go index 63c28966..e23922a9 100644 --- a/pkg/cli/client_utils_test.go +++ b/pkg/cli/client/utils_internal_test.go @@ -1,7 +1,7 @@ //go:build search // +build search -package cli //nolint:testpackage +package client import ( "context" diff --git a/pkg/cli/minimal.go b/pkg/cli/minimal.go deleted file mode 100644 index 49d0762b..00000000 --- a/pkg/cli/minimal.go +++ /dev/null @@ -1,9 +0,0 @@ -//go:build !search -// +build !search - -package cli - -import "github.com/spf13/cobra" - -func enableCli(rootCmd *cobra.Command) { -} diff --git a/pkg/cli/config_reloader.go b/pkg/cli/server/config_reloader.go similarity index 99% rename from pkg/cli/config_reloader.go rename to pkg/cli/server/config_reloader.go index e5559c29..91e22105 100644 --- a/pkg/cli/config_reloader.go +++ b/pkg/cli/server/config_reloader.go @@ -1,4 +1,4 @@ -package cli +package server import ( "context" diff --git a/pkg/cli/config_reloader_test.go b/pkg/cli/server/config_reloader_test.go similarity index 99% rename from pkg/cli/config_reloader_test.go rename to pkg/cli/server/config_reloader_test.go index a1b1ee05..ff3b4fd8 100644 --- a/pkg/cli/config_reloader_test.go +++ b/pkg/cli/server/config_reloader_test.go @@ -1,7 +1,7 @@ //go:build search // +build search -package cli_test +package server_test import ( "fmt" @@ -13,7 +13,7 @@ import ( . "github.com/smartystreets/goconvey/convey" "golang.org/x/crypto/bcrypt" - "zotregistry.io/zot/pkg/cli" + cli "zotregistry.io/zot/pkg/cli/server" "zotregistry.io/zot/pkg/test" ) diff --git a/pkg/cli/extensions_test.go b/pkg/cli/server/extensions_test.go similarity index 99% rename from pkg/cli/extensions_test.go rename to pkg/cli/server/extensions_test.go index 249d7c65..e207ffc5 100644 --- a/pkg/cli/extensions_test.go +++ b/pkg/cli/server/extensions_test.go @@ -1,7 +1,7 @@ //go:build sync && scrub && metrics && search && userprefs && mgmt && imagetrust // +build sync,scrub,metrics,search,userprefs,mgmt,imagetrust -package cli_test +package server_test import ( "fmt" @@ -13,7 +13,7 @@ import ( . "github.com/smartystreets/goconvey/convey" "gopkg.in/resty.v1" - "zotregistry.io/zot/pkg/cli" + cli "zotregistry.io/zot/pkg/cli/server" . "zotregistry.io/zot/pkg/test" ) diff --git a/pkg/cli/root.go b/pkg/cli/server/root.go similarity index 97% rename from pkg/cli/root.go rename to pkg/cli/server/root.go index 762b3fdf..456a25ea 100644 --- a/pkg/cli/root.go +++ b/pkg/cli/server/root.go @@ -1,4 +1,4 @@ -package cli +package server import ( "context" @@ -22,7 +22,6 @@ import ( "zotregistry.io/zot/pkg/api" "zotregistry.io/zot/pkg/api/config" "zotregistry.io/zot/pkg/api/constants" - "zotregistry.io/zot/pkg/cli/cmdflags" extconf "zotregistry.io/zot/pkg/extensions/config" "zotregistry.io/zot/pkg/extensions/monitoring" zlog "zotregistry.io/zot/pkg/log" @@ -191,35 +190,6 @@ func NewServerRootCmd() *cobra.Command { return rootCmd } -// "zli" - client-side cli. -func NewCliRootCmd() *cobra.Command { - showVersion := false - - rootCmd := &cobra.Command{ - Use: "zli", - Short: "`zli`", - Long: "`zli`", - Run: func(cmd *cobra.Command, args []string) { - if showVersion { - log.Info().Str("distribution-spec", distspec.Version).Str("commit", config.Commit). - Str("binary-type", config.BinaryType).Str("go version", config.GoVersion).Msg("version") - } else { - _ = cmd.Usage() - cmd.SilenceErrors = false - } - }, - } - - rootCmd.SilenceUsage = true - - // additional cmds - enableCli(rootCmd) - // "version" - rootCmd.Flags().BoolVarP(&showVersion, cmdflags.VersionFlag, "v", false, "show the version and exit") - - return rootCmd -} - func validateStorageConfig(cfg *config.Config, log zlog.Logger) error { expConfigMap := make(map[string]config.StorageConfig, 0) diff --git a/pkg/cli/root_test.go b/pkg/cli/server/root_test.go similarity index 98% rename from pkg/cli/root_test.go rename to pkg/cli/server/root_test.go index f6859e4f..3cb29558 100644 --- a/pkg/cli/root_test.go +++ b/pkg/cli/server/root_test.go @@ -1,4 +1,4 @@ -package cli_test +package server_test import ( "encoding/json" @@ -12,7 +12,7 @@ import ( "zotregistry.io/zot/pkg/api" "zotregistry.io/zot/pkg/api/config" - "zotregistry.io/zot/pkg/cli" + cli "zotregistry.io/zot/pkg/cli/server" storageConstants "zotregistry.io/zot/pkg/storage/constants" . "zotregistry.io/zot/pkg/test" ) @@ -35,24 +35,6 @@ func TestServerUsage(t *testing.T) { }) } -func TestCliUsage(t *testing.T) { - oldArgs := os.Args - - defer func() { os.Args = oldArgs }() - - Convey("Test usage", t, func(c C) { - os.Args = []string{"cli_test", "help"} - err := cli.NewCliRootCmd().Execute() - So(err, ShouldBeNil) - }) - - Convey("Test version", t, func(c C) { - os.Args = []string{"cli_test", "--version"} - err := cli.NewCliRootCmd().Execute() - So(err, ShouldBeNil) - }) -} - func TestServe(t *testing.T) { oldArgs := os.Args @@ -1335,7 +1317,7 @@ func TestServeAPIKey(t *testing.T) { func TestLoadConfig(t *testing.T) { Convey("Test viper load config", t, func(c C) { config := config.New() - err := cli.LoadConfiguration(config, "../../examples/config-policy.json") + err := cli.LoadConfiguration(config, "../../../examples/config-policy.json") So(err, ShouldBeNil) }) Convey("Test subpath config combination", t, func(c C) { @@ -1445,18 +1427,18 @@ func TestLoadConfig(t *testing.T) { func TestGC(t *testing.T) { Convey("Test GC config", t, func(c C) { config := config.New() - err := cli.LoadConfiguration(config, "../../examples/config-multiple.json") + err := cli.LoadConfiguration(config, "../../../examples/config-multiple.json") So(err, ShouldBeNil) So(config.Storage.GCDelay, ShouldEqual, storageConstants.DefaultGCDelay) - err = cli.LoadConfiguration(config, "../../examples/config-gc.json") + err = cli.LoadConfiguration(config, "../../../examples/config-gc.json") So(err, ShouldBeNil) So(config.Storage.GCDelay, ShouldNotEqual, storageConstants.DefaultGCDelay) - err = cli.LoadConfiguration(config, "../../examples/config-gc-periodic.json") + err = cli.LoadConfiguration(config, "../../../examples/config-gc-periodic.json") So(err, ShouldBeNil) }) Convey("Test GC config corner cases", t, func(c C) { - contents, err := os.ReadFile("../../examples/config-gc.json") + contents, err := os.ReadFile("../../../examples/config-gc.json") So(err, ShouldBeNil) Convey("GC delay without GC", func() { diff --git a/pkg/cli/stress_test.go b/pkg/cli/server/stress_test.go similarity index 98% rename from pkg/cli/stress_test.go rename to pkg/cli/server/stress_test.go index 77b630db..27083216 100644 --- a/pkg/cli/stress_test.go +++ b/pkg/cli/server/stress_test.go @@ -1,7 +1,7 @@ //go:build stress // +build stress -package cli_test +package server_test import ( "fmt" @@ -16,7 +16,7 @@ import ( "zotregistry.io/zot/pkg/api" "zotregistry.io/zot/pkg/api/config" - "zotregistry.io/zot/pkg/cli" + cli "zotregistry.io/zot/pkg/cli/server" "zotregistry.io/zot/pkg/test" ) diff --git a/pkg/extensions/sync/sync_test.go b/pkg/extensions/sync/sync_test.go index 5cdd1648..58ce23a1 100644 --- a/pkg/extensions/sync/sync_test.go +++ b/pkg/extensions/sync/sync_test.go @@ -38,7 +38,7 @@ import ( "zotregistry.io/zot/pkg/api" "zotregistry.io/zot/pkg/api/config" "zotregistry.io/zot/pkg/api/constants" - "zotregistry.io/zot/pkg/cli" + cli "zotregistry.io/zot/pkg/cli/server" zcommon "zotregistry.io/zot/pkg/common" extconf "zotregistry.io/zot/pkg/extensions/config" syncconf "zotregistry.io/zot/pkg/extensions/config/sync" diff --git a/pkg/storage/s3/s3_test.go b/pkg/storage/s3/s3_test.go index 6a1036a3..6fbb9295 100644 --- a/pkg/storage/s3/s3_test.go +++ b/pkg/storage/s3/s3_test.go @@ -76,7 +76,7 @@ func createMockStorage(rootDir string, cacheDir string, dedupe bool, store drive var cacheDriver cache.Cache - // from pkg/cli/root.go/applyDefaultValues, s3 magic + // from pkg/cli/server/root.go/applyDefaultValues, s3 magic if _, err := os.Stat(path.Join(cacheDir, storageConstants.BoltdbName+storageConstants.DBExtensionName)); dedupe || (!dedupe && err == nil) { cacheDriver, _ = storage.Create("boltdb", cache.BoltDBDriverParameters{ @@ -151,7 +151,7 @@ func createObjectsStore(rootDir string, cacheDir string, dedupe bool) ( var err error - // from pkg/cli/root.go/applyDefaultValues, s3 magic + // from pkg/cli/server/root.go/applyDefaultValues, s3 magic s3CacheDBPath := path.Join(cacheDir, storageConstants.BoltdbName+storageConstants.DBExtensionName) if _, err = os.Stat(s3CacheDBPath); dedupe || (!dedupe && err == nil) { cacheDriver, _ = storage.Create("boltdb", cache.BoltDBDriverParameters{ @@ -179,7 +179,7 @@ func createObjectsStoreDynamo(rootDir string, cacheDir string, dedupe bool, tabl var cacheDriver cache.Cache - // from pkg/cli/root.go/applyDefaultValues, s3 magic + // from pkg/cli/server/root.go/applyDefaultValues, s3 magic tableName = strings.ReplaceAll(tableName, "/", "") cacheDriver, _ = storage.Create("dynamodb", cache.DynamoDBDriverParameters{