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" "text/template"
"github.com/Sirupsen/logrus" "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/clair"
"github.com/coreos/clair/cmd/clairctl/docker" "github.com/coreos/clair/cmd/clairctl/docker"
"github.com/coreos/clair/cmd/clairctl/xerrors" "github.com/spf13/cobra"
"github.com/spf13/viper"
) )
const analyseTplt = ` const analyseTplt = `
@ -36,7 +35,7 @@ var analyseCmd = &cobra.Command{
err := template.Must(template.New("analysis").Parse(analyseTplt)).Execute(os.Stdout, ia) err := template.Must(template.New("analysis").Parse(analyseTplt)).Execute(os.Stdout, ia)
if err != nil { if err != nil {
fmt.Println(xerrors.InternalError) fmt.Println(errInternalError)
logrus.Fatalf("rendering analysis: %v", err) logrus.Fatalf("rendering analysis: %v", err)
} }
}, },
@ -50,10 +49,10 @@ func analyse(imageName string) clair.ImageAnalysis {
image, err = docker.Pull(imageName) image, err = docker.Pull(imageName)
if err != nil { if err != nil {
if err == xerrors.NotFound { if err == docker.ErrLoginNotFound {
fmt.Println(err) fmt.Println(err)
} else { } else {
fmt.Println(xerrors.InternalError) fmt.Println(errInternalError)
} }
logrus.Fatalf("pulling image %q: %v", imageName, err) logrus.Fatalf("pulling image %q: %v", imageName, err)
} }
@ -61,12 +60,12 @@ func analyse(imageName string) clair.ImageAnalysis {
} else { } else {
image, err = docker.Parse(imageName) image, err = docker.Parse(imageName)
if err != nil { if err != nil {
fmt.Println(xerrors.InternalError) fmt.Println(errInternalError)
logrus.Fatalf("parsing local image %q: %v", imageName, err) logrus.Fatalf("parsing local image %q: %v", imageName, err)
} }
docker.FromHistory(&image) docker.FromHistory(&image)
if err != nil { if err != nil {
fmt.Println(xerrors.InternalError) fmt.Println(errInternalError)
logrus.Fatalf("getting local image %q from history: %v", imageName, err) logrus.Fatalf("getting local image %q from history: %v", imageName, err)
} }
} }

View File

@ -6,9 +6,8 @@ import (
"text/template" "text/template"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/coreos/clair/cmd/clairctl/clair" "github.com/coreos/clair/cmd/clairctl/clair"
"github.com/coreos/clair/cmd/clairctl/xerrors" "github.com/spf13/cobra"
) )
const healthTplt = ` const healthTplt = `
@ -27,7 +26,7 @@ var healthCmd = &cobra.Command{
ok := clair.IsHealthy() ok := clair.IsHealthy()
err := template.Must(template.New("health").Parse(healthTplt)).Execute(os.Stdout, ok) err := template.Must(template.New("health").Parse(healthTplt)).Execute(os.Stdout, ok)
if err != nil { if err != nil {
fmt.Println(xerrors.InternalError) fmt.Println(errInternalError)
logrus.Fatalf("rendering the health: %v", err) logrus.Fatalf("rendering the health: %v", err)
} }

View File

@ -13,7 +13,6 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/coreos/clair/cmd/clairctl/config" "github.com/coreos/clair/cmd/clairctl/config"
"github.com/coreos/clair/cmd/clairctl/docker" "github.com/coreos/clair/cmd/clairctl/docker"
"github.com/coreos/clair/cmd/clairctl/xerrors"
"github.com/coreos/clair/cmd/clairctl/xstrings" "github.com/coreos/clair/cmd/clairctl/xstrings"
) )
@ -38,7 +37,7 @@ var loginCmd = &cobra.Command{
var users userMapping var users userMapping
if err := readConfigFile(&users, config.HyperclairConfig()); err != nil { if err := readConfigFile(&users, config.HyperclairConfig()); err != nil {
fmt.Println(xerrors.InternalError) fmt.Println(errInternalError)
logrus.Fatalf("reading hyperclair file: %v", err) logrus.Fatalf("reading hyperclair file: %v", err)
} }
@ -50,21 +49,21 @@ var loginCmd = &cobra.Command{
var usr user var usr user
if err := askForUser(&usr); err != nil { if err := askForUser(&usr); err != nil {
fmt.Println(xerrors.InternalError) fmt.Println(errInternalError)
logrus.Fatalf("encrypting password: %v", err) logrus.Fatalf("encrypting password: %v", err)
} }
users[reg] = usr users[reg] = usr
if err := writeConfigFile(users, config.HyperclairConfig()); err != nil { if err := writeConfigFile(users, config.HyperclairConfig()); err != nil {
fmt.Println(xerrors.InternalError) fmt.Println(errInternalError)
logrus.Fatalf("indenting login: %v", err) logrus.Fatalf("indenting login: %v", err)
} }
logged, err := docker.Login(reg) logged, err := docker.Login(reg)
if err != nil { if err != nil {
fmt.Println(xerrors.InternalError) fmt.Println(errInternalError)
logrus.Fatalf("log in: %v", err) logrus.Fatalf("log in: %v", err)
} }

View File

@ -5,10 +5,9 @@ import (
"os" "os"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/coreos/clair/cmd/clairctl/config" "github.com/coreos/clair/cmd/clairctl/config"
"github.com/coreos/clair/cmd/clairctl/docker" "github.com/coreos/clair/cmd/clairctl/docker"
"github.com/coreos/clair/cmd/clairctl/xerrors" "github.com/spf13/cobra"
) )
var logoutCmd = &cobra.Command{ var logoutCmd = &cobra.Command{
@ -31,14 +30,14 @@ var logoutCmd = &cobra.Command{
var users userMapping var users userMapping
if err := readConfigFile(&users, config.HyperclairConfig()); err != nil { if err := readConfigFile(&users, config.HyperclairConfig()); err != nil {
fmt.Println(xerrors.InternalError) fmt.Println(errInternalError)
logrus.Fatalf("reading hyperclair file: %v", err) logrus.Fatalf("reading hyperclair file: %v", err)
} }
if _, present := users[reg]; present { if _, present := users[reg]; present {
delete(users, reg) delete(users, reg)
if err := writeConfigFile(users, config.HyperclairConfig()); err != nil { if err := writeConfigFile(users, config.HyperclairConfig()); err != nil {
fmt.Println(xerrors.InternalError) fmt.Println(errInternalError)
logrus.Fatalf("indenting login: %v", err) logrus.Fatalf("indenting login: %v", err)
} }

View File

@ -20,9 +20,8 @@ import (
"text/template" "text/template"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/coreos/clair/cmd/clairctl/docker" "github.com/coreos/clair/cmd/clairctl/docker"
"github.com/coreos/clair/cmd/clairctl/xerrors" "github.com/spf13/cobra"
) )
const pullTplt = ` const pullTplt = `
@ -46,13 +45,13 @@ var pullCmd = &cobra.Command{
im := args[0] im := args[0]
image, err := docker.Pull(im) image, err := docker.Pull(im)
if err != nil { if err != nil {
fmt.Println(xerrors.ServiceUnavailable) fmt.Println(errInternalError)
logrus.Fatalf("pulling image %v: %v", args[0], err) logrus.Fatalf("pulling image %v: %v", args[0], err)
} }
err = template.Must(template.New("pull").Parse(pullTplt)).Execute(os.Stdout, image) err = template.Must(template.New("pull").Parse(pullTplt)).Execute(os.Stdout, image)
if err != nil { if err != nil {
fmt.Println(xerrors.InternalError) fmt.Println(errInternalError)
logrus.Fatalf("rendering image: %v", err) 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/config"
"github.com/coreos/clair/cmd/clairctl/docker" "github.com/coreos/clair/cmd/clairctl/docker"
"github.com/coreos/clair/cmd/clairctl/server" "github.com/coreos/clair/cmd/clairctl/server"
"github.com/coreos/clair/cmd/clairctl/xerrors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -32,10 +31,10 @@ var pushCmd = &cobra.Command{
var err error var err error
image, err = docker.Pull(imageName) image, err = docker.Pull(imageName)
if err != nil { if err != nil {
if err == xerrors.NotFound { if err == docker.ErrLoginNotFound {
fmt.Println(err) fmt.Println(err)
} else { } else {
fmt.Println(xerrors.InternalError) fmt.Println(errInternalError)
} }
logrus.Fatalf("pulling image %q: %v", imageName, err) logrus.Fatalf("pulling image %q: %v", imageName, err)
} }
@ -43,13 +42,13 @@ var pushCmd = &cobra.Command{
var err error var err error
image, err = docker.Parse(imageName) image, err = docker.Parse(imageName)
if err != nil { if err != nil {
fmt.Println(xerrors.InternalError) fmt.Println(errInternalError)
logrus.Fatalf("parsing local image %q: %v", imageName, err) logrus.Fatalf("parsing local image %q: %v", imageName, err)
} }
err = docker.Prepare(&image) err = docker.Prepare(&image)
logrus.Debugf("prepared image layers: %d", len(image.FsLayers)) logrus.Debugf("prepared image layers: %d", len(image.FsLayers))
if err != nil { if err != nil {
fmt.Println(xerrors.InternalError) fmt.Println(errInternalError)
logrus.Fatalf("preparing local image %q from history: %v", imageName, err) logrus.Fatalf("preparing local image %q from history: %v", imageName, err)
} }
} }
@ -57,7 +56,7 @@ var pushCmd = &cobra.Command{
logrus.Info("Pushing Image") logrus.Info("Pushing Image")
if err := docker.Push(image); err != nil { if err := docker.Push(image); err != nil {
if err != nil { if err != nil {
fmt.Println(xerrors.InternalError) fmt.Println(errInternalError)
logrus.Fatalf("pushing image %q: %v", imageName, err) logrus.Fatalf("pushing image %q: %v", imageName, err)
} }
} }
@ -76,12 +75,12 @@ func init() {
func startLocalServer() { func startLocalServer() {
sURL, err := config.LocalServerIP() sURL, err := config.LocalServerIP()
if err != nil { if err != nil {
fmt.Println(xerrors.InternalError) fmt.Println(errInternalError)
logrus.Fatalf("retrieving internal server IP: %v", err) logrus.Fatalf("retrieving internal server IP: %v", err)
} }
err = server.Serve(sURL) err = server.Serve(sURL)
if err != nil { if err != nil {
fmt.Println(xerrors.InternalError) fmt.Println(errInternalError)
logrus.Fatalf("starting local server: %v", err) logrus.Fatalf("starting local server: %v", err)
} }
} }

View File

@ -10,7 +10,6 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
"github.com/coreos/clair/cmd/clairctl/clair" "github.com/coreos/clair/cmd/clairctl/clair"
"github.com/coreos/clair/cmd/clairctl/docker" "github.com/coreos/clair/cmd/clairctl/docker"
"github.com/coreos/clair/cmd/clairctl/xerrors"
"github.com/coreos/clair/cmd/clairctl/xstrings" "github.com/coreos/clair/cmd/clairctl/xstrings"
) )
@ -30,12 +29,12 @@ var reportCmd = &cobra.Command{
case "html": case "html":
html, err := clair.ReportAsHTML(analyses) html, err := clair.ReportAsHTML(analyses)
if err != nil { if err != nil {
fmt.Println(xerrors.InternalError) fmt.Println(errInternalError)
logrus.Fatalf("generating HTML report: %v", err) logrus.Fatalf("generating HTML report: %v", err)
} }
err = saveReport(imageName, string(html)) err = saveReport(imageName, string(html))
if err != nil { if err != nil {
fmt.Println(xerrors.InternalError) fmt.Println(errInternalError)
logrus.Fatalf("saving HTML report: %v", err) logrus.Fatalf("saving HTML report: %v", err)
} }
@ -43,12 +42,12 @@ var reportCmd = &cobra.Command{
json, err := xstrings.ToIndentJSON(analyses) json, err := xstrings.ToIndentJSON(analyses)
if err != nil { if err != nil {
fmt.Println(xerrors.InternalError) fmt.Println(errInternalError)
logrus.Fatalf("indenting JSON: %v", err) logrus.Fatalf("indenting JSON: %v", err)
} }
err = saveReport(imageName, string(json)) err = saveReport(imageName, string(json))
if err != nil { if err != nil {
fmt.Println(xerrors.InternalError) fmt.Println(errInternalError)
logrus.Fatalf("saving JSON report: %v", err) logrus.Fatalf("saving JSON report: %v", err)
} }

View File

@ -15,13 +15,16 @@
package cmd package cmd
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"github.com/spf13/cobra"
"github.com/coreos/clair/cmd/clairctl/config" "github.com/coreos/clair/cmd/clairctl/config"
"github.com/spf13/cobra"
) )
var errInternalError = errors.New("client quit unexpectedly")
var cfgFile string var cfgFile string
var logLevel string var logLevel string

View File

@ -7,7 +7,6 @@ import (
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/coreos/clair/cmd/clairctl/xerrors"
) )
const versionTplt = ` const versionTplt = `
@ -26,7 +25,7 @@ var versionCmd = &cobra.Command{
err := templ.Execute(os.Stdout, version) err := templ.Execute(os.Stdout, version)
if err != nil { if err != nil {
fmt.Println(xerrors.InternalError) fmt.Println(errInternalError)
logrus.Fatalf("rendering the version: %v", err) logrus.Fatalf("rendering the version: %v", err)
} }
}, },

View File

@ -2,6 +2,7 @@ package config
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"io" "io"
"os" "os"
@ -12,11 +13,12 @@ import (
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/spf13/viper"
"github.com/coreos/clair/cmd/clairctl/clair" "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 { type r struct {
Path, Format string Path, Format string
} }
@ -136,7 +138,7 @@ func Print() {
func HyperclairHome() string { func HyperclairHome() string {
usr, err := user.Current() usr, err := user.Current()
if err != nil { if err != nil {
fmt.Println(xerrors.InternalError) fmt.Println(errInternalError)
logrus.Fatalf("retrieving user: %v", err) logrus.Fatalf("retrieving user: %v", err)
} }
p := usr.HomeDir + "/.hyperclair" p := usr.HomeDir + "/.hyperclair"

View File

@ -2,15 +2,17 @@ package docker
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"strings" "strings"
"github.com/coreos/clair/cmd/clairctl/docker/httpclient" "github.com/coreos/clair/cmd/clairctl/docker/httpclient"
"github.com/coreos/clair/cmd/clairctl/xerrors"
) )
var ErrUnauthorized = errors.New("unauthorized access")
type Authentication struct { type Authentication struct {
Username, Password string Username, Password string
} }
@ -63,7 +65,7 @@ func AuthenticateResponse(dockerResponse *http.Response, request *http.Request)
} }
if response.StatusCode == http.StatusUnauthorized { if response.StatusCode == http.StatusUnauthorized {
return xerrors.Unauthorized return ErrUnauthorized
} }
if response.StatusCode != http.StatusOK { if response.StatusCode != http.StatusOK {

View File

@ -7,9 +7,10 @@ import (
"strings" "strings"
"github.com/spf13/viper" "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 //Image represent Image Manifest from Docker image, including the registry URL
type Image struct { type Image struct {
Name string Name string
@ -79,7 +80,7 @@ func Parse(image string) (Image, error) {
} }
if strings.Contains(registry, "docker.io") && repository == "" { if strings.Contains(registry, "docker.io") && repository == "" {
return Image{}, xerrors.ErrDisallowed return Image{}, errDisallowed
} }
return Image{ return Image{

View File

@ -3,7 +3,6 @@ package docker
import ( import (
"testing" "testing"
"github.com/coreos/clair/cmd/clairctl/xerrors"
) )
var imageNameTests = []struct { var imageNameTests = []struct {
@ -46,8 +45,8 @@ func TestParse(t *testing.T) {
func TestParseDisallowed(t *testing.T) { func TestParseDisallowed(t *testing.T) {
for _, imageName := range invalidImageNameTests { for _, imageName := range invalidImageNameTests {
_, err := Parse(imageName.in) _, err := Parse(imageName.in)
if err != xerrors.ErrDisallowed { if err != errDisallowed {
t.Errorf("Parse(\"%s\") should failed with err \"%v\": %v", imageName.in, xerrors.ErrDisallowed, err) 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/Sirupsen/logrus"
"github.com/coreos/clair/cmd/clairctl/docker/httpclient" "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 //Pull Image from Registry or Hub depending on image name
@ -34,7 +33,7 @@ func Login(registry string) (bool, error) {
err := AuthenticateResponse(response, request) err := AuthenticateResponse(response, request)
if err != nil { if err != nil {
if err == xerrors.Unauthorized { if err == ErrUnauthorized {
authorized = false authorized = false
} }
return false, err return false, err

View File

@ -8,7 +8,6 @@ import (
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/coreos/clair/cmd/clairctl/docker/httpclient" "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 //Pull Image from Registry or Hub depending on image name
@ -48,9 +47,9 @@ func Pull(imageName string) (Image, error) {
if response.StatusCode != 200 { if response.StatusCode != 200 {
switch response.StatusCode { switch response.StatusCode {
case http.StatusUnauthorized: case http.StatusUnauthorized:
return Image{}, xerrors.Unauthorized return Image{}, ErrUnauthorized
case http.StatusNotFound: case http.StatusNotFound:
return Image{}, xerrors.NotFound return Image{}, docker.ErrLoginNotFound
default: default:
return Image{}, fmt.Errorf("%d - %s", response.StatusCode, string(body)) 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")
)