From 2e016b2900190a94715073cfae7348e2e037e55d Mon Sep 17 00:00:00 2001 From: jgsqware Date: Mon, 2 May 2016 20:23:43 +0200 Subject: [PATCH] remove xerrors package --- cmd/clairctl/cmd/analyse.go | 15 +++++++-------- cmd/clairctl/cmd/health.go | 5 ++--- cmd/clairctl/cmd/login.go | 9 ++++----- cmd/clairctl/cmd/logout.go | 7 +++---- cmd/clairctl/cmd/pull.go | 7 +++---- cmd/clairctl/cmd/push.go | 15 +++++++-------- cmd/clairctl/cmd/report.go | 9 ++++----- cmd/clairctl/cmd/root.go | 5 ++++- cmd/clairctl/cmd/version.go | 3 +-- cmd/clairctl/config/config.go | 8 +++++--- cmd/clairctl/docker/auth.go | 6 ++++-- cmd/clairctl/docker/image.go | 5 +++-- cmd/clairctl/docker/image_test.go | 5 ++--- cmd/clairctl/docker/login.go | 3 +-- cmd/clairctl/docker/pull.go | 5 ++--- cmd/clairctl/xerrors/xerrors.go | 11 ----------- 16 files changed, 52 insertions(+), 66 deletions(-) delete mode 100644 cmd/clairctl/xerrors/xerrors.go diff --git a/cmd/clairctl/cmd/analyse.go b/cmd/clairctl/cmd/analyse.go index ce00a49e..f85fc108 100644 --- a/cmd/clairctl/cmd/analyse.go +++ b/cmd/clairctl/cmd/analyse.go @@ -6,11 +6,10 @@ import ( "text/template" "github.com/Sirupsen/logrus" - "github.com/spf13/cobra" - "github.com/spf13/viper" "github.com/coreos/clair/cmd/clairctl/clair" "github.com/coreos/clair/cmd/clairctl/docker" - "github.com/coreos/clair/cmd/clairctl/xerrors" + "github.com/spf13/cobra" + "github.com/spf13/viper" ) const analyseTplt = ` @@ -36,7 +35,7 @@ var analyseCmd = &cobra.Command{ err := template.Must(template.New("analysis").Parse(analyseTplt)).Execute(os.Stdout, ia) if err != nil { - fmt.Println(xerrors.InternalError) + fmt.Println(errInternalError) logrus.Fatalf("rendering analysis: %v", err) } }, @@ -50,10 +49,10 @@ func analyse(imageName string) clair.ImageAnalysis { image, err = docker.Pull(imageName) if err != nil { - if err == xerrors.NotFound { + if err == docker.ErrLoginNotFound { fmt.Println(err) } else { - fmt.Println(xerrors.InternalError) + fmt.Println(errInternalError) } logrus.Fatalf("pulling image %q: %v", imageName, err) } @@ -61,12 +60,12 @@ func analyse(imageName string) clair.ImageAnalysis { } else { image, err = docker.Parse(imageName) if err != nil { - fmt.Println(xerrors.InternalError) + fmt.Println(errInternalError) logrus.Fatalf("parsing local image %q: %v", imageName, err) } docker.FromHistory(&image) if err != nil { - fmt.Println(xerrors.InternalError) + fmt.Println(errInternalError) logrus.Fatalf("getting local image %q from history: %v", imageName, err) } } diff --git a/cmd/clairctl/cmd/health.go b/cmd/clairctl/cmd/health.go index 50c4c334..443e65f3 100644 --- a/cmd/clairctl/cmd/health.go +++ b/cmd/clairctl/cmd/health.go @@ -6,9 +6,8 @@ import ( "text/template" "github.com/Sirupsen/logrus" - "github.com/spf13/cobra" "github.com/coreos/clair/cmd/clairctl/clair" - "github.com/coreos/clair/cmd/clairctl/xerrors" + "github.com/spf13/cobra" ) const healthTplt = ` @@ -27,7 +26,7 @@ var healthCmd = &cobra.Command{ ok := clair.IsHealthy() err := template.Must(template.New("health").Parse(healthTplt)).Execute(os.Stdout, ok) if err != nil { - fmt.Println(xerrors.InternalError) + fmt.Println(errInternalError) logrus.Fatalf("rendering the health: %v", err) } diff --git a/cmd/clairctl/cmd/login.go b/cmd/clairctl/cmd/login.go index 79093b6d..83db89a6 100644 --- a/cmd/clairctl/cmd/login.go +++ b/cmd/clairctl/cmd/login.go @@ -13,7 +13,6 @@ import ( "github.com/spf13/cobra" "github.com/coreos/clair/cmd/clairctl/config" "github.com/coreos/clair/cmd/clairctl/docker" - "github.com/coreos/clair/cmd/clairctl/xerrors" "github.com/coreos/clair/cmd/clairctl/xstrings" ) @@ -38,7 +37,7 @@ var loginCmd = &cobra.Command{ var users userMapping if err := readConfigFile(&users, config.HyperclairConfig()); err != nil { - fmt.Println(xerrors.InternalError) + fmt.Println(errInternalError) logrus.Fatalf("reading hyperclair file: %v", err) } @@ -50,21 +49,21 @@ var loginCmd = &cobra.Command{ var usr user if err := askForUser(&usr); err != nil { - fmt.Println(xerrors.InternalError) + fmt.Println(errInternalError) logrus.Fatalf("encrypting password: %v", err) } users[reg] = usr if err := writeConfigFile(users, config.HyperclairConfig()); err != nil { - fmt.Println(xerrors.InternalError) + fmt.Println(errInternalError) logrus.Fatalf("indenting login: %v", err) } logged, err := docker.Login(reg) if err != nil { - fmt.Println(xerrors.InternalError) + fmt.Println(errInternalError) logrus.Fatalf("log in: %v", err) } diff --git a/cmd/clairctl/cmd/logout.go b/cmd/clairctl/cmd/logout.go index e81805ff..aee968df 100644 --- a/cmd/clairctl/cmd/logout.go +++ b/cmd/clairctl/cmd/logout.go @@ -5,10 +5,9 @@ import ( "os" "github.com/Sirupsen/logrus" - "github.com/spf13/cobra" "github.com/coreos/clair/cmd/clairctl/config" "github.com/coreos/clair/cmd/clairctl/docker" - "github.com/coreos/clair/cmd/clairctl/xerrors" + "github.com/spf13/cobra" ) var logoutCmd = &cobra.Command{ @@ -31,14 +30,14 @@ var logoutCmd = &cobra.Command{ var users userMapping if err := readConfigFile(&users, config.HyperclairConfig()); err != nil { - fmt.Println(xerrors.InternalError) + fmt.Println(errInternalError) logrus.Fatalf("reading hyperclair file: %v", err) } if _, present := users[reg]; present { delete(users, reg) if err := writeConfigFile(users, config.HyperclairConfig()); err != nil { - fmt.Println(xerrors.InternalError) + fmt.Println(errInternalError) logrus.Fatalf("indenting login: %v", err) } diff --git a/cmd/clairctl/cmd/pull.go b/cmd/clairctl/cmd/pull.go index 163dffae..21a9464f 100644 --- a/cmd/clairctl/cmd/pull.go +++ b/cmd/clairctl/cmd/pull.go @@ -20,9 +20,8 @@ import ( "text/template" "github.com/Sirupsen/logrus" - "github.com/spf13/cobra" "github.com/coreos/clair/cmd/clairctl/docker" - "github.com/coreos/clair/cmd/clairctl/xerrors" + "github.com/spf13/cobra" ) const pullTplt = ` @@ -46,13 +45,13 @@ var pullCmd = &cobra.Command{ im := args[0] image, err := docker.Pull(im) if err != nil { - fmt.Println(xerrors.ServiceUnavailable) + fmt.Println(errInternalError) logrus.Fatalf("pulling image %v: %v", args[0], err) } err = template.Must(template.New("pull").Parse(pullTplt)).Execute(os.Stdout, image) if err != nil { - fmt.Println(xerrors.InternalError) + fmt.Println(errInternalError) logrus.Fatalf("rendering image: %v", err) } }, diff --git a/cmd/clairctl/cmd/push.go b/cmd/clairctl/cmd/push.go index 253bab08..a87dc19a 100644 --- a/cmd/clairctl/cmd/push.go +++ b/cmd/clairctl/cmd/push.go @@ -8,7 +8,6 @@ import ( "github.com/coreos/clair/cmd/clairctl/config" "github.com/coreos/clair/cmd/clairctl/docker" "github.com/coreos/clair/cmd/clairctl/server" - "github.com/coreos/clair/cmd/clairctl/xerrors" "github.com/spf13/cobra" ) @@ -32,10 +31,10 @@ var pushCmd = &cobra.Command{ var err error image, err = docker.Pull(imageName) if err != nil { - if err == xerrors.NotFound { + if err == docker.ErrLoginNotFound { fmt.Println(err) } else { - fmt.Println(xerrors.InternalError) + fmt.Println(errInternalError) } logrus.Fatalf("pulling image %q: %v", imageName, err) } @@ -43,13 +42,13 @@ var pushCmd = &cobra.Command{ var err error image, err = docker.Parse(imageName) if err != nil { - fmt.Println(xerrors.InternalError) + fmt.Println(errInternalError) logrus.Fatalf("parsing local image %q: %v", imageName, err) } err = docker.Prepare(&image) logrus.Debugf("prepared image layers: %d", len(image.FsLayers)) if err != nil { - fmt.Println(xerrors.InternalError) + fmt.Println(errInternalError) logrus.Fatalf("preparing local image %q from history: %v", imageName, err) } } @@ -57,7 +56,7 @@ var pushCmd = &cobra.Command{ logrus.Info("Pushing Image") if err := docker.Push(image); err != nil { if err != nil { - fmt.Println(xerrors.InternalError) + fmt.Println(errInternalError) logrus.Fatalf("pushing image %q: %v", imageName, err) } } @@ -76,12 +75,12 @@ func init() { func startLocalServer() { sURL, err := config.LocalServerIP() if err != nil { - fmt.Println(xerrors.InternalError) + fmt.Println(errInternalError) logrus.Fatalf("retrieving internal server IP: %v", err) } err = server.Serve(sURL) if err != nil { - fmt.Println(xerrors.InternalError) + fmt.Println(errInternalError) logrus.Fatalf("starting local server: %v", err) } } diff --git a/cmd/clairctl/cmd/report.go b/cmd/clairctl/cmd/report.go index e3bae8b9..18e9e3f1 100644 --- a/cmd/clairctl/cmd/report.go +++ b/cmd/clairctl/cmd/report.go @@ -10,7 +10,6 @@ import ( "github.com/spf13/viper" "github.com/coreos/clair/cmd/clairctl/clair" "github.com/coreos/clair/cmd/clairctl/docker" - "github.com/coreos/clair/cmd/clairctl/xerrors" "github.com/coreos/clair/cmd/clairctl/xstrings" ) @@ -30,12 +29,12 @@ var reportCmd = &cobra.Command{ case "html": html, err := clair.ReportAsHTML(analyses) if err != nil { - fmt.Println(xerrors.InternalError) + fmt.Println(errInternalError) logrus.Fatalf("generating HTML report: %v", err) } err = saveReport(imageName, string(html)) if err != nil { - fmt.Println(xerrors.InternalError) + fmt.Println(errInternalError) logrus.Fatalf("saving HTML report: %v", err) } @@ -43,12 +42,12 @@ var reportCmd = &cobra.Command{ json, err := xstrings.ToIndentJSON(analyses) if err != nil { - fmt.Println(xerrors.InternalError) + fmt.Println(errInternalError) logrus.Fatalf("indenting JSON: %v", err) } err = saveReport(imageName, string(json)) if err != nil { - fmt.Println(xerrors.InternalError) + fmt.Println(errInternalError) logrus.Fatalf("saving JSON report: %v", err) } diff --git a/cmd/clairctl/cmd/root.go b/cmd/clairctl/cmd/root.go index 32a52cd6..e4706cd5 100644 --- a/cmd/clairctl/cmd/root.go +++ b/cmd/clairctl/cmd/root.go @@ -15,13 +15,16 @@ package cmd import ( + "errors" "fmt" "os" - "github.com/spf13/cobra" "github.com/coreos/clair/cmd/clairctl/config" + "github.com/spf13/cobra" ) +var errInternalError = errors.New("client quit unexpectedly") + var cfgFile string var logLevel string diff --git a/cmd/clairctl/cmd/version.go b/cmd/clairctl/cmd/version.go index 58ff9e80..2d9e8e97 100644 --- a/cmd/clairctl/cmd/version.go +++ b/cmd/clairctl/cmd/version.go @@ -7,7 +7,6 @@ import ( "github.com/Sirupsen/logrus" "github.com/spf13/cobra" - "github.com/coreos/clair/cmd/clairctl/xerrors" ) const versionTplt = ` @@ -26,7 +25,7 @@ var versionCmd = &cobra.Command{ err := templ.Execute(os.Stdout, version) if err != nil { - fmt.Println(xerrors.InternalError) + fmt.Println(errInternalError) logrus.Fatalf("rendering the version: %v", err) } }, diff --git a/cmd/clairctl/config/config.go b/cmd/clairctl/config/config.go index 311652e6..60a10794 100644 --- a/cmd/clairctl/config/config.go +++ b/cmd/clairctl/config/config.go @@ -2,6 +2,7 @@ package config import ( "bytes" + "errors" "fmt" "io" "os" @@ -12,11 +13,12 @@ import ( "gopkg.in/yaml.v2" "github.com/Sirupsen/logrus" - "github.com/spf13/viper" "github.com/coreos/clair/cmd/clairctl/clair" - "github.com/coreos/clair/cmd/clairctl/xerrors" + "github.com/spf13/viper" ) +var ErrLoginNotFound = errors.New("user is not log in") + type r struct { Path, Format string } @@ -136,7 +138,7 @@ func Print() { func HyperclairHome() string { usr, err := user.Current() if err != nil { - fmt.Println(xerrors.InternalError) + fmt.Println(errInternalError) logrus.Fatalf("retrieving user: %v", err) } p := usr.HomeDir + "/.hyperclair" diff --git a/cmd/clairctl/docker/auth.go b/cmd/clairctl/docker/auth.go index 32e19dcd..f26f46f5 100644 --- a/cmd/clairctl/docker/auth.go +++ b/cmd/clairctl/docker/auth.go @@ -2,15 +2,17 @@ package docker import ( "encoding/json" + "errors" "fmt" "io/ioutil" "net/http" "strings" "github.com/coreos/clair/cmd/clairctl/docker/httpclient" - "github.com/coreos/clair/cmd/clairctl/xerrors" ) +var ErrUnauthorized = errors.New("unauthorized access") + type Authentication struct { Username, Password string } @@ -63,7 +65,7 @@ func AuthenticateResponse(dockerResponse *http.Response, request *http.Request) } if response.StatusCode == http.StatusUnauthorized { - return xerrors.Unauthorized + return ErrUnauthorized } if response.StatusCode != http.StatusOK { diff --git a/cmd/clairctl/docker/image.go b/cmd/clairctl/docker/image.go index 3e16e5d4..66a1d40d 100644 --- a/cmd/clairctl/docker/image.go +++ b/cmd/clairctl/docker/image.go @@ -7,9 +7,10 @@ import ( "strings" "github.com/spf13/viper" - "github.com/coreos/clair/cmd/clairctl/xerrors" ) +errDisallowed = errors.New("analysing official images is not allowed") + //Image represent Image Manifest from Docker image, including the registry URL type Image struct { Name string @@ -79,7 +80,7 @@ func Parse(image string) (Image, error) { } if strings.Contains(registry, "docker.io") && repository == "" { - return Image{}, xerrors.ErrDisallowed + return Image{}, errDisallowed } return Image{ diff --git a/cmd/clairctl/docker/image_test.go b/cmd/clairctl/docker/image_test.go index 371b6104..e5055965 100644 --- a/cmd/clairctl/docker/image_test.go +++ b/cmd/clairctl/docker/image_test.go @@ -3,7 +3,6 @@ package docker import ( "testing" - "github.com/coreos/clair/cmd/clairctl/xerrors" ) var imageNameTests = []struct { @@ -46,8 +45,8 @@ func TestParse(t *testing.T) { func TestParseDisallowed(t *testing.T) { for _, imageName := range invalidImageNameTests { _, err := Parse(imageName.in) - if err != xerrors.ErrDisallowed { - t.Errorf("Parse(\"%s\") should failed with err \"%v\": %v", imageName.in, xerrors.ErrDisallowed, err) + if err != errDisallowed { + t.Errorf("Parse(\"%s\") should failed with err \"%v\": %v", imageName.in, errDisallowed, err) } } } diff --git a/cmd/clairctl/docker/login.go b/cmd/clairctl/docker/login.go index 52e1b7c2..fcfe5f65 100644 --- a/cmd/clairctl/docker/login.go +++ b/cmd/clairctl/docker/login.go @@ -7,7 +7,6 @@ import ( "github.com/Sirupsen/logrus" "github.com/coreos/clair/cmd/clairctl/docker/httpclient" - "github.com/coreos/clair/cmd/clairctl/xerrors" ) //Pull Image from Registry or Hub depending on image name @@ -34,7 +33,7 @@ func Login(registry string) (bool, error) { err := AuthenticateResponse(response, request) if err != nil { - if err == xerrors.Unauthorized { + if err == ErrUnauthorized { authorized = false } return false, err diff --git a/cmd/clairctl/docker/pull.go b/cmd/clairctl/docker/pull.go index fabdfeb6..6da94a2e 100644 --- a/cmd/clairctl/docker/pull.go +++ b/cmd/clairctl/docker/pull.go @@ -8,7 +8,6 @@ import ( "github.com/Sirupsen/logrus" "github.com/coreos/clair/cmd/clairctl/docker/httpclient" - "github.com/coreos/clair/cmd/clairctl/xerrors" ) //Pull Image from Registry or Hub depending on image name @@ -48,9 +47,9 @@ func Pull(imageName string) (Image, error) { if response.StatusCode != 200 { switch response.StatusCode { case http.StatusUnauthorized: - return Image{}, xerrors.Unauthorized + return Image{}, ErrUnauthorized case http.StatusNotFound: - return Image{}, xerrors.NotFound + return Image{}, docker.ErrLoginNotFound default: return Image{}, fmt.Errorf("%d - %s", response.StatusCode, string(body)) } diff --git a/cmd/clairctl/xerrors/xerrors.go b/cmd/clairctl/xerrors/xerrors.go deleted file mode 100644 index 33c99328..00000000 --- a/cmd/clairctl/xerrors/xerrors.go +++ /dev/null @@ -1,11 +0,0 @@ -package xerrors - -import "errors" - -var ( - ServiceUnavailable = errors.New("service is unavailable") - Unauthorized = errors.New("unauthorized access") - NotFound = errors.New("image not found") - InternalError = errors.New("client quit unexpectedly") - ErrDisallowed = errors.New("analysing official images is not allowed") -)