remove xerrors package

This commit is contained in:
jgsqware 2016-05-02 20:23:43 +02:00
parent 3083a891e0
commit 2e016b2900
16 changed files with 52 additions and 66 deletions

View File

@ -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)
}
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}
},

View File

@ -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)
}
}

View File

@ -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)
}

View File

@ -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

View File

@ -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)
}
},

View File

@ -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"

View File

@ -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 {

View File

@ -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{

View File

@ -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)
}
}
}

View File

@ -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

View File

@ -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))
}

View File

@ -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")
)