refactor: Reduce zli binary size (#1805)

Signed-off-by: Alexei Dodon <adodon@cisco.com>
This commit is contained in:
Alexei Dodon
2023-09-16 01:17:01 +03:00
committed by GitHub
parent 8e18917b07
commit f58597ade9
45 changed files with 166 additions and 155 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
import "github.com/spf13/cobra"
@@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
import (
"bytes"
@@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
import (
"errors"
@@ -1,7 +1,7 @@
//go:build search
// +build search
package cli //nolint:testpackage
package client
import (
"bytes"
@@ -1,7 +1,7 @@
//go:build search
// +build search
package cli //nolint:testpackage
package client
import (
"bytes"
@@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
import (
"github.com/spf13/cobra"
@@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
import (
"fmt"
@@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
import (
"context"
@@ -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.*")
@@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
type GQLField struct {
Name string
@@ -1,7 +1,7 @@
//go:build search
// +build search
package cli //nolint:testpackage
package client
import (
"io"
@@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
import (
"strconv"
@@ -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{
@@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
import (
"fmt"
@@ -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/"
)
@@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
import (
"github.com/spf13/cobra"
@@ -1,7 +1,7 @@
//go:build search
// +build search
package cli //nolint:testpackage
package client
import (
"bytes"
@@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
import (
"fmt"
+42
View File
@@ -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
}
+31
View File
@@ -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)
})
}
@@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
import (
"github.com/spf13/cobra"
@@ -1,7 +1,7 @@
//go:build search
// +build search
package cli //nolint:testpackage
package client
import (
"bytes"
@@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
import (
"context"
@@ -3,7 +3,7 @@
//
//nolint:dupl
package cli //nolint:testpackage
package client
import (
"bytes"
@@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
import (
"fmt"
@@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
import (
"context"
+1 -1
View File
@@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
import (
"context"
@@ -1,7 +1,7 @@
//go:build search
// +build search
package cli //nolint:testpackage
package client
import (
"context"
-9
View File
@@ -1,9 +0,0 @@
//go:build !search
// +build !search
package cli
import "github.com/spf13/cobra"
func enableCli(rootCmd *cobra.Command) {
}
@@ -1,4 +1,4 @@
package cli
package server
import (
"context"
@@ -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"
)
@@ -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"
)
+1 -31
View File
@@ -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)
@@ -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() {
@@ -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"
)
+1 -1
View File
@@ -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"
+3 -3
View File
@@ -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{