-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add command to interogate the server version and other det…
…ails Signed-off-by: Laurentiu Niculae <[email protected]>
- Loading branch information
1 parent
a3d8202
commit 95d3d33
Showing
9 changed files
with
233 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
//go:build search | ||
// +build search | ||
|
||
package client | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"gopkg.in/yaml.v2" | ||
|
||
zerr "zotregistry.io/zot/errors" | ||
"zotregistry.io/zot/pkg/api/constants" | ||
"zotregistry.io/zot/pkg/cli/cmdflags" | ||
Check failure on line 17 in pkg/cli/client/server_info_cmd.go GitHub Actions / Run zot with extensions tests
|
||
) | ||
|
||
func NewServerInfoCommand() *cobra.Command { | ||
serverInfoCmd := &cobra.Command{ | ||
Use: "server-info", | ||
Short: "Information about the server configuration and build information", | ||
Long: `Information about the server configuration and build information`, | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
searchConfig, err := GetSearchConfigFromFlags(cmd, NewSearchService()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return GetServerInfo(searchConfig) | ||
}, | ||
} | ||
|
||
serverInfoCmd.Flags().String(cmdflags.OutputFormatFlag, "text", "Specify the output format [text|json|yaml]") | ||
|
||
return serverInfoCmd | ||
} | ||
|
||
func GetServerInfo(config searchConfig) error { | ||
username, password := getUsernameAndPassword(config.user) | ||
ctx := context.Background() | ||
|
||
mgmtEndpoint, err := combineServerAndEndpointURL(config.servURL, fmt.Sprintf("%s%s", | ||
constants.RoutePrefix, constants.ExtMgmt)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
serverInfo := ServerInfo{} | ||
|
||
_, err = makeGETRequest(ctx, mgmtEndpoint, username, password, config.verifyTLS, config.debug, | ||
&serverInfo, config.resultWriter) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
outputResult, err := serverInfo.ToStringFormat(config.outputFormat) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Fprintln(config.resultWriter, outputResult) | ||
|
||
return nil | ||
} | ||
|
||
type ServerInfo struct { | ||
DistSpecVersion string `json:"distSpecVersion" mapstructure:"distSpecVersion"` | ||
Commit string `json:"commit" mapstructure:"commit"` | ||
BinaryType string `json:"binaryType" mapstructure:"binaryType"` | ||
ReleaseTag string `json:"releaseTag" mapstructure:"releaseTag"` | ||
} | ||
|
||
func (si *ServerInfo) ToStringFormat(format string) (string, error) { | ||
switch format { | ||
case "text", "": | ||
return si.ToText() | ||
case "json": | ||
return si.ToJSON() | ||
case "yaml", "yml": | ||
return si.ToYAML() | ||
default: | ||
return "", zerr.ErrFormatNotSupported | ||
} | ||
} | ||
|
||
func (si *ServerInfo) ToText() (string, error) { | ||
flagsList := strings.Split(strings.Trim(si.BinaryType, "-"), "-") | ||
flags := strings.Join(flagsList, ", ") | ||
|
||
return fmt.Sprintf("Server Version: %s\n"+ | ||
"Dist Spec Version: %s\n"+ | ||
"Built with: %s", | ||
si.ReleaseTag, si.DistSpecVersion, flags, | ||
), | ||
nil | ||
} | ||
|
||
func (si *ServerInfo) ToJSON() (string, error) { | ||
blob, err := json.MarshalIndent(*si, "", " ") | ||
|
||
return string(blob), err | ||
} | ||
|
||
func (si *ServerInfo) ToYAML() (string, error) { | ||
body, err := yaml.Marshal(*si) | ||
|
||
return string(body), err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
//go:build search | ||
// +build search | ||
|
||
package client //nolint:testpackage | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"regexp" | ||
"strings" | ||
"testing" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
|
||
"zotregistry.io/zot/pkg/api" | ||
"zotregistry.io/zot/pkg/api/config" | ||
extconf "zotregistry.io/zot/pkg/extensions/config" | ||
test "zotregistry.io/zot/pkg/test/common" | ||
) | ||
|
||
func TestServerInfoCommand(t *testing.T) { | ||
Convey("ServerInfoCommand", t, func() { | ||
port := test.GetFreePort() | ||
baseURL := test.GetBaseURL(port) | ||
conf := config.New() | ||
conf.HTTP.Port = port | ||
conf.Storage.GC = false | ||
defaultVal := true | ||
conf.Extensions = &extconf.ExtensionConfig{ | ||
Search: &extconf.SearchConfig{BaseConfig: extconf.BaseConfig{Enable: &defaultVal}}, | ||
} | ||
|
||
ctlr := api.NewController(conf) | ||
ctlr.Config.Storage.RootDirectory = t.TempDir() | ||
cm := test.NewControllerManager(ctlr) | ||
|
||
cm.StartAndWait(conf.HTTP.Port) | ||
defer cm.StopServer() | ||
|
||
configPath := makeConfigFile(fmt.Sprintf(`{"configs":[{"_name":"server-info-test","url":"%s","showspinner":false}]}`, | ||
baseURL)) | ||
defer os.Remove(configPath) | ||
|
||
args := []string{"server-info", "--config", "server-info-test"} | ||
cmd := NewCliRootCmd() | ||
buff := bytes.NewBufferString("") | ||
cmd.SetOut(buff) | ||
cmd.SetErr(buff) | ||
cmd.SetArgs(args) | ||
err := cmd.Execute() | ||
So(err, ShouldBeNil) | ||
space := regexp.MustCompile(`\s+`) | ||
str := space.ReplaceAllString(buff.String(), " ") | ||
actual := strings.TrimSpace(str) | ||
So(actual, ShouldContainSubstring, config.ReleaseTag) | ||
So(actual, ShouldContainSubstring, config.BinaryType) | ||
|
||
// JSON | ||
args = []string{"server-info", "--config", "server-info-test", "--format", "json"} | ||
cmd = NewCliRootCmd() | ||
buff = bytes.NewBufferString("") | ||
cmd.SetOut(buff) | ||
cmd.SetErr(buff) | ||
cmd.SetArgs(args) | ||
err = cmd.Execute() | ||
So(err, ShouldBeNil) | ||
space = regexp.MustCompile(`\s+`) | ||
str = space.ReplaceAllString(buff.String(), " ") | ||
actual = strings.TrimSpace(str) | ||
So(actual, ShouldContainSubstring, config.ReleaseTag) | ||
So(actual, ShouldContainSubstring, config.BinaryType) | ||
|
||
// YAML | ||
args = []string{"server-info", "--config", "server-info-test", "--format", "yaml"} | ||
cmd = NewCliRootCmd() | ||
buff = bytes.NewBufferString("") | ||
cmd.SetOut(buff) | ||
cmd.SetErr(buff) | ||
cmd.SetArgs(args) | ||
err = cmd.Execute() | ||
So(err, ShouldBeNil) | ||
space = regexp.MustCompile(`\s+`) | ||
str = space.ReplaceAllString(buff.String(), " ") | ||
actual = strings.TrimSpace(str) | ||
So(actual, ShouldContainSubstring, config.ReleaseTag) | ||
So(actual, ShouldContainSubstring, config.BinaryType) | ||
|
||
// bad type | ||
args = []string{"server-info", "--config", "server-info-test", "--format", "badType"} | ||
cmd = NewCliRootCmd() | ||
buff = bytes.NewBufferString("") | ||
cmd.SetOut(buff) | ||
cmd.SetErr(buff) | ||
cmd.SetArgs(args) | ||
err = cmd.Execute() | ||
So(err, ShouldNotBeNil) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters