From 20079f8922dd22caa900b21e3d98e7e1985bf1fa Mon Sep 17 00:00:00 2001 From: Fernando Giorgetti Date: Fri, 25 Aug 2023 15:07:10 -0300 Subject: [PATCH] Prevents cli from throwing unexpected error when podman endpoint is no longer available Fixes #1219. --- client/podman/rest.go | 6 +++++- cmd/skupper/skupper_podman.go | 9 +++++++-- cmd/skupper/skupper_podman_site.go | 4 +++- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/client/podman/rest.go b/client/podman/rest.go index b79a412ea..821450b9e 100644 --- a/client/podman/rest.go +++ b/client/podman/rest.go @@ -42,7 +42,7 @@ func NewPodmanClient(endpoint, basePath string) (*PodmanRestClient, error) { var err error if endpoint == "" { - defaultEndpoint := fmt.Sprintf("unix://%s/podman/podman.sock", config.GetRuntimeDir()) + defaultEndpoint := GetDefaultPodmanEndpoint() endpoint = utils.DefaultStr(os.Getenv(ENV_PODMAN_ENDPOINT), defaultEndpoint) } @@ -143,6 +143,10 @@ func NewPodmanClient(endpoint, basePath string) (*PodmanRestClient, error) { return cli, nil } +func GetDefaultPodmanEndpoint() string { + return fmt.Sprintf("unix://%s/podman/podman.sock", config.GetRuntimeDir()) +} + func (p *PodmanRestClient) IsSockEndpoint() bool { return strings.HasPrefix(p.endpoint, "/") || strings.HasPrefix(p.endpoint, "unix://") } diff --git a/cmd/skupper/skupper_podman.go b/cmd/skupper/skupper_podman.go index 3e799928f..74964a447 100644 --- a/cmd/skupper/skupper_podman.go +++ b/cmd/skupper/skupper_podman.go @@ -7,6 +7,7 @@ import ( "github.com/skupperproject/skupper/api/types" clientpodman "github.com/skupperproject/skupper/client/podman" "github.com/skupperproject/skupper/pkg/domain/podman" + "github.com/skupperproject/skupper/test/utils" "github.com/spf13/cobra" ) @@ -83,6 +84,7 @@ func (s *SkupperPodman) NewClient(cmd *cobra.Command, args []string) { // endpoint can be provided during init var endpoint string var isInitCmd bool + exitOnError := true switch cmd.Name() { case "init": // require site not present @@ -90,6 +92,8 @@ func (s *SkupperPodman) NewClient(cmd *cobra.Command, args []string) { endpoint = args[0] } isInitCmd = true + case "version": + exitOnError = false default: podmanCfg, err := podman.NewPodmanConfigFileHandler().GetConfig() if err != nil { @@ -101,8 +105,9 @@ func (s *SkupperPodman) NewClient(cmd *cobra.Command, args []string) { c, err := clientpodman.NewPodmanClient(endpoint, "") if err != nil { - if endpoint != "" { - fmt.Printf("Podman endpoint is not available: %s", endpoint) + if exitOnError { + fmt.Printf("Podman endpoint is not available: %s", + utils.StrDefault(endpoint, clientpodman.GetDefaultPodmanEndpoint())) fmt.Println() os.Exit(1) } diff --git a/cmd/skupper/skupper_podman_site.go b/cmd/skupper/skupper_podman_site.go index 5d8d491bb..41fc75cd2 100644 --- a/cmd/skupper/skupper_podman_site.go +++ b/cmd/skupper/skupper_podman_site.go @@ -294,7 +294,9 @@ func (s *SkupperPodmanSite) UpdateFlags(cmd *cobra.Command) { func (s *SkupperPodmanSite) Version(cmd *cobra.Command, args []string) error { site := s.podman.currentSite if site == nil { - return fmt.Errorf("Skupper is not enabled for user '%s'", podman.Username) + fmt.Printf("Skupper is not enabled for user '%s'", podman.Username) + fmt.Println() + return nil } for _, deploy := range site.GetDeployments() { for _, component := range deploy.GetComponents() {