Skip to content

Commit

Permalink
Merge pull request #10678 from php-coder/use_errors_new
Browse files Browse the repository at this point in the history
Merged by openshift-bot
  • Loading branch information
OpenShift Bot authored Sep 21, 2016
2 parents f5d9ec2 + 8a14bf9 commit aa08313
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 38 deletions.
35 changes: 18 additions & 17 deletions pkg/generate/app/cmd/newapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"encoding/json"
"errors"
"fmt"
"io"
"reflect"
Expand All @@ -17,7 +18,7 @@ import (
kclient "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/kubectl/resource"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util/errors"
kutilerrors "k8s.io/kubernetes/pkg/util/errors"

authapi "github.com/openshift/origin/pkg/authorization/api"
buildapi "github.com/openshift/origin/pkg/build/api"
Expand All @@ -42,7 +43,7 @@ const (

// ErrNoDockerfileDetected is the error returned when the requested build strategy is Docker
// and no Dockerfile is detected in the repository.
var ErrNoDockerfileDetected = fmt.Errorf("No Dockerfile was found in the repository and the requested build strategy is 'docker'")
var ErrNoDockerfileDetected = errors.New("No Dockerfile was found in the repository and the requested build strategy is 'docker'")

// GenerationInputs control how new-app creates output
// TODO: split these into finer grained structs
Expand Down Expand Up @@ -131,7 +132,7 @@ func (e ErrRequiresExplicitAccess) Error() string {
}

// ErrNoInputs is returned when no inputs are specified
var ErrNoInputs = fmt.Errorf("no inputs provided")
var ErrNoInputs = errors.New("no inputs provided")

// AppResult contains the results of an application
type AppResult struct {
Expand Down Expand Up @@ -260,7 +261,7 @@ func (c *AppConfig) validateBuilders(components app.ComponentReferences) error {
continue
}
}
return errors.NewAggregate(errs)
return kutilerrors.NewAggregate(errs)
}

func validateEnforcedName(name string) error {
Expand Down Expand Up @@ -428,7 +429,7 @@ func (c *AppConfig) installComponents(components app.ComponentReferences, env ap
var ok bool
name, ok = imageRef.SuggestName()
if !ok {
return nil, "", fmt.Errorf("can't suggest a valid name, please specify a name with --name")
return nil, "", errors.New("can't suggest a valid name, please specify a name with --name")
}
}
imageRef.ObjectName = name
Expand Down Expand Up @@ -495,10 +496,10 @@ func (c *AppConfig) RunQuery() (*QueryResult, error) {

if c.AsList {
if c.AsSearch {
return nil, fmt.Errorf("--list and --search can't be used together")
return nil, errors.New("--list and --search can't be used together")
}
if c.HasArguments() {
return nil, fmt.Errorf("--list can't be used with arguments")
return nil, errors.New("--list can't be used with arguments")
}
c.Components = append(c.Components, "*")
}
Expand All @@ -509,24 +510,24 @@ func (c *AppConfig) RunQuery() (*QueryResult, error) {
}
components, repositories, errs := b.Result()
if len(errs) > 0 {
return nil, errors.NewAggregate(errs)
return nil, kutilerrors.NewAggregate(errs)
}

if len(components) == 0 && !c.AsList {
return nil, ErrNoInputs
}

if len(repositories) > 0 {
errs = append(errs, fmt.Errorf("--search can't be used with source code"))
errs = append(errs, errors.New("--search can't be used with source code"))
}
if len(environment) > 0 {
errs = append(errs, fmt.Errorf("--search can't be used with --env"))
errs = append(errs, errors.New("--search can't be used with --env"))
}
if len(parameters) > 0 {
errs = append(errs, fmt.Errorf("--search can't be used with --param"))
errs = append(errs, errors.New("--search can't be used with --param"))
}
if len(errs) > 0 {
return nil, errors.NewAggregate(errs)
return nil, kutilerrors.NewAggregate(errs)
}

if err := components.Search(); err != nil {
Expand Down Expand Up @@ -574,7 +575,7 @@ func (c *AppConfig) validate() (cmdutil.Environment, cmdutil.Environment, error)
}
errs = append(errs, paramsErrs...)

return env, params, errors.NewAggregate(errs)
return env, params, kutilerrors.NewAggregate(errs)
}

// Run executes the provided config to generate objects.
Expand Down Expand Up @@ -621,10 +622,10 @@ func (c *AppConfig) Run() (*AppResult, error) {
}

if len(components.ImageComponentRefs().Group()) > 1 && len(c.Name) > 0 {
return nil, fmt.Errorf("only one component or source repository can be used when specifying a name")
return nil, errors.New("only one component or source repository can be used when specifying a name")
}
if len(components.UseSource()) > 1 && len(c.To) > 0 {
return nil, fmt.Errorf("only one component with source can be used when specifying an output image reference")
return nil, errors.New("only one component with source can be used when specifying an output image reference")
}

env := app.Environment(environment)
Expand All @@ -647,7 +648,7 @@ func (c *AppConfig) Run() (*AppResult, error) {
pipelines, err := c.buildPipelines(components.ImageComponentRefs(), env)
if err != nil {
if err == app.ErrNameRequired {
return nil, fmt.Errorf("can't suggest a valid name, please specify a name with --name")
return nil, errors.New("can't suggest a valid name, please specify a name with --name")
}
return nil, err
}
Expand Down Expand Up @@ -730,7 +731,7 @@ func (c *AppConfig) Run() (*AppResult, error) {
func (c *AppConfig) followRefToDockerImage(ref *kapi.ObjectReference, isContext *imageapi.ImageStream, objects app.Objects) (*kapi.ObjectReference, error) {

if ref == nil {
return nil, fmt.Errorf("Unable to follow nil")
return nil, errors.New("Unable to follow nil")
}

if ref.Kind == "DockerImage" {
Expand Down
33 changes: 17 additions & 16 deletions pkg/generate/app/cmd/resolve.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package cmd

import (
"errors"
"fmt"
"strings"

"github.com/golang/glog"
"k8s.io/kubernetes/pkg/util/errors"
kutilerrors "k8s.io/kubernetes/pkg/util/errors"

"github.com/openshift/origin/pkg/generate/app"
dockerfileutil "github.com/openshift/origin/pkg/util/docker/dockerfile"
Expand Down Expand Up @@ -97,7 +98,7 @@ func Resolve(r *Resolvers, c *ComponentInputs, g *GenerationInputs) (*ResolvedCo
}
components, repositories, errs := b.Result()
if len(errs) > 0 {
return nil, errors.NewAggregate(errs)
return nil, kutilerrors.NewAggregate(errs)
}

// TODO: the second half of this method is potentially splittable - each chunk below amends or qualifies
Expand All @@ -110,11 +111,11 @@ func Resolve(r *Resolvers, c *ComponentInputs, g *GenerationInputs) (*ResolvedCo
}

if len(g.Strategy) != 0 && len(repositories) == 0 && !g.BinaryBuild {
return nil, fmt.Errorf("when --strategy is specified you must provide at least one source code location")
return nil, errors.New("when --strategy is specified you must provide at least one source code location")
}

if g.BinaryBuild && (len(repositories) > 0 || components.HasSource()) {
return nil, fmt.Errorf("specifying binary builds and source repositories at the same time is not allowed")
return nil, errors.New("specifying binary builds and source repositories at the same time is not allowed")
}

// Add source components if source-image points to another location
Expand Down Expand Up @@ -186,21 +187,21 @@ func AddSourceRepositoriesToRefBuilder(b *app.ReferenceBuilder, repos []string,
}
if len(g.Dockerfile) > 0 {
if len(g.Strategy) != 0 && g.Strategy != "docker" {
return nil, fmt.Errorf("when directly referencing a Dockerfile, the strategy must must be 'docker'")
return nil, errors.New("when directly referencing a Dockerfile, the strategy must must be 'docker'")
}
if err := AddDockerfileToSourceRepositories(b, g.Dockerfile); err != nil {
return nil, err
}
}
_, result, errs := b.Result()
return result, errors.NewAggregate(errs)
return result, kutilerrors.NewAggregate(errs)
}

// AddDockerfile adds a Dockerfile passed in the command line to the reference
// builder.
func AddDockerfileToSourceRepositories(b *app.ReferenceBuilder, dockerfile string) error {
_, repos, errs := b.Result()
if err := errors.NewAggregate(errs); err != nil {
if err := kutilerrors.NewAggregate(errs); err != nil {
return err
}
switch len(repos) {
Expand All @@ -220,7 +221,7 @@ func AddDockerfileToSourceRepositories(b *app.ReferenceBuilder, dockerfile strin
}
default:
// Invalid.
return fmt.Errorf("--dockerfile cannot be used with multiple source repositories")
return errors.New("--dockerfile cannot be used with multiple source repositories")
}
return nil
}
Expand All @@ -239,7 +240,7 @@ func DetectSource(repositories []*app.SourceRepository, d app.Detector, g *Gener
continue
}
}
return errors.NewAggregate(errs)
return kutilerrors.NewAggregate(errs)
}

// AddComponentInputsToRefBuilder set up the components to be used by the reference builder.
Expand Down Expand Up @@ -353,7 +354,7 @@ func AddImageSourceRepository(sourceRepos app.SourceRepositories, r app.Resolver
sourceRepos[0].SetSourceImage(compRef)
sourceRepos[0].SetSourceImagePath(sourcePath, destPath)
default:
return nil, nil, fmt.Errorf("--image-source cannot be used with multiple source repositories")
return nil, nil, errors.New("--image-source cannot be used with multiple source repositories")
}

return compRef, sourceRepos, nil
Expand All @@ -367,7 +368,7 @@ func detectPartialMatches(components app.ComponentReferences) error {
errs = append(errs, fmt.Errorf("component %q had only a partial match of %q - if this is the value you want to use, specify it explicitly", input.From, input.ResolvedMatch.Name))
}
}
return errors.NewAggregate(errs)
return kutilerrors.NewAggregate(errs)
}

// InferBuildTypes infers build status and mismatches between source and docker builders
Expand Down Expand Up @@ -398,13 +399,13 @@ func InferBuildTypes(components app.ComponentReferences, g *GenerationInputs) (a
switch {
case input.ExpectToBuild && input.ResolvedMatch.IsTemplate():
// TODO: harder - break the template pieces and check if source code can be attached (look for a build config, build image, etc)
errs = append(errs, fmt.Errorf("template with source code explicitly attached is not supported - you must either specify the template and source code separately or attach an image to the source code using the '[image]~[code]' form"))
errs = append(errs, errors.New("template with source code explicitly attached is not supported - you must either specify the template and source code separately or attach an image to the source code using the '[image]~[code]' form"))
continue
}
}
if len(components) == 0 && g.BinaryBuild {
if len(g.Name) == 0 {
return nil, fmt.Errorf("you must provide a --name when you don't specify a source repository or base image")
return nil, errors.New("you must provide a --name when you don't specify a source repository or base image")
}
ref := &app.ComponentInput{
From: "--binary",
Expand All @@ -416,7 +417,7 @@ func InferBuildTypes(components app.ComponentReferences, g *GenerationInputs) (a
components = append(components, ref)
}

return components, errors.NewAggregate(errs)
return components, kutilerrors.NewAggregate(errs)
}

// EnsureHasSource ensure every builder component has source code associated with it. It takes a list of component references
Expand Down Expand Up @@ -470,7 +471,7 @@ func EnsureHasSource(components app.ComponentReferences, repositories app.Source
input.ExpectToBuild = true
}
case g.ExpectToBuild:
return fmt.Errorf("you must specify at least one source repository URL, provide a Dockerfile, or indicate you wish to use binary builds")
return errors.New("you must specify at least one source repository URL, provide a Dockerfile, or indicate you wish to use binary builds")
default:
for _, component := range components {
component.Input().ExpectToBuild = false
Expand Down Expand Up @@ -529,5 +530,5 @@ func AddMissingComponentsToRefBuilder(
result = append(result, refs...)
}
}
return result, errors.NewAggregate(errs)
return result, kutilerrors.NewAggregate(errs)
}
7 changes: 4 additions & 3 deletions pkg/generate/app/sourcelookup.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"errors"
"fmt"
"io/ioutil"
"net/url"
Expand Down Expand Up @@ -38,7 +39,7 @@ func NewDockerfileFromFile(path string) (Dockerfile, error) {

func NewDockerfile(contents string) (Dockerfile, error) {
if len(contents) == 0 {
return nil, fmt.Errorf("Dockerfile is empty")
return nil, errors.New("Dockerfile is empty")
}
node, err := parser.Parse(strings.NewReader(contents))
if err != nil {
Expand Down Expand Up @@ -428,7 +429,7 @@ type SourceRepositoryEnumerator struct {

// ErrNoLanguageDetected is the error returned when no language can be detected by all
// source code detectors.
var ErrNoLanguageDetected = fmt.Errorf("No language matched the source repository")
var ErrNoLanguageDetected = errors.New("No language matched the source repository")

// Detect extracts source code information about the provided source repository
func (e SourceRepositoryEnumerator) Detect(dir string, dockerStrategy bool) (*SourceRepositoryInfo, error) {
Expand Down Expand Up @@ -506,7 +507,7 @@ func StrategyAndSourceForRepository(repo *SourceRepository, image *ImageRef) (*B
return strategy, source, nil
}

// CloneAndCheckoutSources clones the remote repository using either regulare
// CloneAndCheckoutSources clones the remote repository using either regular
// git clone operation or shallow git clone, based on the "ref" provided (you
// cannot shallow clone using the 'ref').
// This function will return the full path to the buildable sources, including
Expand Down
5 changes: 3 additions & 2 deletions pkg/generate/git/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package git
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -56,7 +57,7 @@ const (

// ErrGitNotAvailable will be returned if the git call fails because a git binary
// could not be found
var ErrGitNotAvailable = fmt.Errorf("git binary not available")
var ErrGitNotAvailable = errors.New("git binary not available")

// SourceInfo stores information about the source code
type SourceInfo struct {
Expand Down Expand Up @@ -270,7 +271,7 @@ func (r *repository) Archive(location, ref, format string, w io.Writer) error {
// Checkout switches to the given ref for the git repository
func (r *repository) Checkout(location string, ref string) error {
if r.shallow {
return fmt.Errorf("cannot checkout ref on shallow clone")
return errors.New("cannot checkout ref on shallow clone")
}
_, _, err := r.git(location, "checkout", ref)
return err
Expand Down

0 comments on commit aa08313

Please sign in to comment.