Skip to content

Commit

Permalink
moved away from pkg/errors (#484)
Browse files Browse the repository at this point in the history
  • Loading branch information
neel-astro committed Jan 27, 2022
1 parent 70607c4 commit 0621377
Show file tree
Hide file tree
Showing 34 changed files with 217 additions and 179 deletions.
15 changes: 7 additions & 8 deletions airflow/airflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/astronomer/astro-cli/pkg/fileutil"

semver "github.com/Masterminds/semver/v3"
"github.com/pkg/errors"
)

const (
Expand All @@ -36,12 +35,12 @@ func initDirs(root string, dirs []string) error {
// Move on if already exists
_, err := fileutil.Exists(fullpath)
if err != nil {
return errors.Wrapf(err, "failed to check existence of '%s'", fullpath)
return fmt.Errorf("failed to check existence of '%s': %w", fullpath, err)
}

// Create directory
if err := os.MkdirAll(fullpath, defaultDirPerm); err != nil {
return errors.Wrapf(err, "failed to create dir '%s'", dir)
return fmt.Errorf("failed to create dir '%s': %w", dir, err)
}
}

Expand All @@ -57,7 +56,7 @@ func initFiles(root string, files map[string]string) error {
// Move on if already exists
fileExist, err := fileutil.Exists(fullpath)
if err != nil {
return errors.Wrapf(err, "failed to check existence of '%s'", fullpath)
return fmt.Errorf("failed to check existence of '%s': %w", fullpath, err)
}

if fileExist {
Expand All @@ -66,7 +65,7 @@ func initFiles(root string, files map[string]string) error {

// Write files out
if err := fileutil.WriteStringToFile(fullpath, content); err != nil {
return errors.Wrapf(err, "failed to create file '%s'", fullpath)
return fmt.Errorf("failed to create file '%s': %w", fullpath, err)
}
}

Expand Down Expand Up @@ -97,12 +96,12 @@ func Init(path, airflowImageTag string) error {

// Initailize directories
if err := initDirs(path, dirs); err != nil {
return errors.Wrap(err, "failed to create project directories")
return fmt.Errorf("failed to create project directories: %w", err)
}

// Initialize files
if err := initFiles(path, files); err != nil {
return errors.Wrap(err, "failed to create project files")
return fmt.Errorf("failed to create project files: %w", err)
}

return nil
Expand All @@ -112,7 +111,7 @@ func ParseVersionFromDockerFile(airflowHome, dockerfile string) (uint64, error)
// parse dockerfile
cmd, err := docker.ParseFile(filepath.Join(airflowHome, dockerfile))
if err != nil {
return 0, errors.Wrapf(err, "failed to parse dockerfile: %s", filepath.Join(airflowHome, dockerfile))
return 0, fmt.Errorf("failed to parse dockerfile: %s: %w", filepath.Join(airflowHome, dockerfile), err)
}

_, airflowTag := docker.GetImageTagFromParsedFile(cmd)
Expand Down
16 changes: 11 additions & 5 deletions airflow/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package airflow
import (
"bytes"
"crypto/md5" //nolint:gosec
"errors"
"fmt"
"text/template"

Expand All @@ -11,8 +12,13 @@ import (
"github.com/astronomer/astro-cli/messages"
"github.com/astronomer/astro-cli/pkg/fileutil"
"github.com/docker/docker/api/types/versions"
)

"github.com/pkg/errors"
var (
errProjectAlreadyRunning = errors.New("cannot start, project already running")
errNoLogsProjectNotRunning = errors.New("cannot view logs, project not running")
errAirflowNotRunning = errors.New("airflow is not running, Start it with 'astro airflow start'")
errEmptyExecID = errors.New("exec ID is empty")
)

type Container string
Expand Down Expand Up @@ -115,7 +121,7 @@ func generateConfig(projectName, airflowHome, envFile string, imageLabels map[st

tmpl, err := template.New("yml").Parse(tmplFile)
if err != nil {
return "", errors.Wrap(err, "failed to generate config")
return "", fmt.Errorf("failed to generate config: %w", err)
}

envFile, err = getFmtEnvFile(envFile, containerEngine)
Expand Down Expand Up @@ -150,7 +156,7 @@ func generateConfig(projectName, airflowHome, envFile string, imageLabels map[st
buff := new(bytes.Buffer)
err = tmpl.Execute(buff, cfg)
if err != nil {
return "", errors.Wrap(err, "failed to generate config")
return "", fmt.Errorf("failed to generate config: %w", err)
}
return buff.String(), nil
}
Expand All @@ -163,7 +169,7 @@ func projectNameUnique() (string, error) {

pwd, err := fileutil.GetWorkingDir()
if err != nil {
return "", errors.Wrap(err, "error retrieving working directory")
return "", fmt.Errorf("error retrieving working directory: %w", err)
}

// #nosec
Expand All @@ -180,7 +186,7 @@ func getFmtEnvFile(envFile string, containerEngine Container) (string, error) {

envExists, err := fileutil.Exists(envFile)
if err != nil {
return "", errors.Wrapf(err, messages.EnvPath, envFile)
return "", fmt.Errorf("%s: %w", fmt.Sprintf(messages.EnvPath, envFile), err)
}

if !envExists {
Expand Down
36 changes: 16 additions & 20 deletions airflow/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

Expand All @@ -48,12 +47,12 @@ func DockerComposeInit(airflowHome, envFile string) (*DockerCompose, error) {
// Get project name from config
projectName, err := projectNameUnique()
if err != nil {
return nil, errors.Wrap(err, "error retrieving working directory")
return nil, fmt.Errorf("error retrieving working directory: %w", err)
}

dockerClient, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
return nil, errors.Wrap(err, "error initializing docker client")
return nil, fmt.Errorf("error initializing docker client: %w", err)
}
composeService := compose.NewComposeService(dockerClient, &configfile.ConfigFile{})
imageHandler := DockerImageInit(projectName)
Expand All @@ -71,15 +70,15 @@ func (d *DockerCompose) Start(dockerfile string) error {
// Get project containers
psInfo, err := d.composeService.Ps(context.TODO(), d.projectName, api.PsOptions{All: true})
if err != nil {
return errors.Wrap(err, messages.ErrContainerStatusCheck)
return fmt.Errorf("%s: %w", messages.ErrContainerStatusCheck, err)
}

if len(psInfo) > 0 {
// Ensure project is not already running
for idx := range psInfo {
info := psInfo[idx]
if checkServiceState(info.State, dockerStateUp) {
return errors.New("cannot start, project already running")
return errProjectAlreadyRunning
}
}
}
Expand All @@ -104,7 +103,7 @@ func (d *DockerCompose) Start(dockerfile string) error {
// Start up our project
err = d.composeService.Up(context.TODO(), project, api.UpOptions{})
if err != nil {
return errors.Wrap(err, messages.ErrContainerRecreate)
return fmt.Errorf("%s: %w", messages.ErrContainerRecreate, err)
}

parts := strings.Split(config.CFG.WebserverPort.GetString(), ":")
Expand All @@ -119,7 +118,7 @@ func (d *DockerCompose) Kill() error {
// Shut down our project
err := d.composeService.Down(context.TODO(), d.projectName, api.DownOptions{Volumes: true, RemoveOrphans: true})
if err != nil {
return errors.Wrap(err, messages.ErrContainerStop)
return fmt.Errorf("%s: %w", messages.ErrContainerStop, err)
}

return nil
Expand All @@ -128,11 +127,11 @@ func (d *DockerCompose) Kill() error {
func (d *DockerCompose) Logs(follow bool, containerNames ...string) error {
psInfo, err := d.composeService.Ps(context.TODO(), d.projectName, api.PsOptions{All: true})
if err != nil {
return errors.Wrap(err, messages.ErrContainerStatusCheck)
return fmt.Errorf("%s: %w", messages.ErrContainerStatusCheck, err)
}

if len(psInfo) == 0 {
return errors.New("cannot view logs, project not running")
return errNoLogsProjectNotRunning
}

logger := &ComposeLogger{logger: logrus.New()}
Expand All @@ -158,7 +157,7 @@ func (d *DockerCompose) Stop() error {
stopTimeout := time.Duration(projectStopTimeout)
err = d.composeService.Stop(context.TODO(), project, api.StopOptions{Timeout: &stopTimeout})
if err != nil {
return errors.Wrap(err, messages.ErrContainerPause)
return fmt.Errorf("%s: %w", messages.ErrContainerPause, err)
}

return nil
Expand All @@ -168,7 +167,7 @@ func (d *DockerCompose) PS() error {
// List project containers
psInfo, err := d.composeService.Ps(context.TODO(), d.projectName, api.PsOptions{All: true})
if err != nil {
return errors.Wrap(err, messages.ErrContainerStatusCheck)
return fmt.Errorf("%s: %w", messages.ErrContainerStatusCheck, err)
}

// Columns for table
Expand Down Expand Up @@ -217,12 +216,12 @@ func (d *DockerCompose) Run(args []string, user string) error {

response, err := cli.ContainerExecCreate(context.Background(), containerID, *execConfig)
if err != nil {
return errors.New("airflow is not running, Start it with 'astro airflow start'")
return errAirflowNotRunning
}

execID := response.ID
if execID == "" {
return errors.New("exec ID is empty")
return errEmptyExecID
}

execStartCheck := types.ExecStartCheck{
Expand All @@ -240,10 +239,7 @@ func (d *DockerCompose) ExecCommand(containerID, command string) string {
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr

out, err := cmd.Output()
if err != nil {
_ = errors.Wrapf(err, "error encountered")
}
out, _ := cmd.Output()

stringOut := string(out)
return stringOut
Expand All @@ -252,7 +248,7 @@ func (d *DockerCompose) ExecCommand(containerID, command string) string {
func (d *DockerCompose) GetContainerID(containerName string) (string, error) {
psInfo, err := d.composeService.Ps(context.TODO(), d.projectName, api.PsOptions{All: true})
if err != nil {
return "", errors.Wrap(err, messages.ErrContainerStatusCheck)
return "", fmt.Errorf("%s: %w", messages.ErrContainerStatusCheck, err)
}

for idx := range psInfo {
Expand All @@ -274,7 +270,7 @@ func createProject(projectName, airflowHome, envFile string, labels map[string]s
// Generate the docker-compose yaml
yaml, err := generateConfig(projectName, airflowHome, envFile, labels, DockerEngine)
if err != nil {
return nil, errors.Wrap(err, "failed to create project")
return nil, fmt.Errorf("failed to create project: %w", err)
}

if err != nil {
Expand All @@ -291,7 +287,7 @@ func createProject(projectName, airflowHome, envFile string, labels map[string]s
composeFile := "docker-compose.override.yml"
composeBytes, err := ioutil.ReadFile(composeFile)
if err != nil && !os.IsNotExist(err) {
return nil, errors.Wrapf(err, "Failed to open the compose file: %s", composeFile)
return nil, fmt.Errorf("failed to open the compose file: %s: %w", composeFile, err)
}
if err == nil {
overrideConfig := composeTypes.ConfigFile{Content: composeBytes, Filename: composeFile}
Expand Down
16 changes: 8 additions & 8 deletions airflow/docker_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"os"
Expand All @@ -17,7 +18,6 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

Expand All @@ -42,7 +42,7 @@ func (d *DockerImage) Build(path string) error {
// Build image
err = dockerExec(nil, nil, "build", "-t", imageName, ".")
if err != nil {
return errors.Wrapf(err, "command 'docker build -t %s failed", d.imageName)
return fmt.Errorf("command 'docker build -t %s failed: %w", d.imageName, err)
}

return nil
Expand All @@ -54,7 +54,7 @@ func (d *DockerImage) Push(cloudDomain, token, remoteImageTag string) error {

err := dockerExec(nil, nil, "tag", imageName(d.imageName, "latest"), remoteImage)
if err != nil {
return errors.Wrapf(err, "command 'docker tag %s %s' failed", d.imageName, remoteImage)
return fmt.Errorf("command 'docker tag %s %s' failed: %w", d.imageName, remoteImage, err)
}

// Push image to registry
Expand All @@ -69,7 +69,7 @@ func (d *DockerImage) Push(cloudDomain, token, remoteImageTag string) error {
log.Debugf("Exec Push docker creds %v \n", authConfig)
if err != nil {
log.Debugf("Error reading credentials: %v", err)
return errors.Errorf("Error reading credentials: %v", err)
return fmt.Errorf("error reading credentials: %w", err)
}

ctx := context.Background()
Expand Down Expand Up @@ -101,7 +101,7 @@ func (d *DockerImage) Push(cloudDomain, token, remoteImageTag string) error {
// Delete the image tags we just generated
err = dockerExec(nil, nil, "rmi", remoteImage)
if err != nil {
return errors.Wrapf(err, "command 'docker rmi %s' failed", remoteImage)
return fmt.Errorf("command 'docker rmi %s' failed: %w", remoteImage, err)
}
return nil
}
Expand All @@ -116,7 +116,7 @@ func (d *DockerImage) GetImageLabels() (map[string]string, error) {
return labels, err
}
if execErr := stderr.String(); execErr != "" {
return labels, errors.Wrap(errGetImageLabel, execErr)
return labels, fmt.Errorf("%s: %w", execErr, errGetImageLabel)
}
err = json.Unmarshal(stdout.Bytes(), &labels)
if err != nil {
Expand All @@ -129,7 +129,7 @@ func (d *DockerImage) GetImageLabels() (map[string]string, error) {
func dockerExec(stdout, stderr io.Writer, args ...string) error {
_, lookErr := exec.LookPath(Docker)
if lookErr != nil {
return errors.Wrap(lookErr, "failed to find the docker binary")
return fmt.Errorf("failed to find the docker binary: %w", lookErr)
}

cmd := exec.Command(Docker, args...)
Expand All @@ -147,7 +147,7 @@ func dockerExec(stdout, stderr io.Writer, args ...string) error {
}

if cmdErr := cmd.Run(); cmdErr != nil {
return errors.Wrapf(cmdErr, "failed to execute cmd")
return fmt.Errorf("failed to execute cmd: %w", cmdErr)
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions airflow/docker_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package airflow

import (
"context"
"fmt"
"os"

cliconfig "github.com/docker/cli/cli/config"
cliTypes "github.com/docker/cli/cli/config/types"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/docker/docker/registry"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -43,7 +43,7 @@ func (d *DockerRegistry) Login(username, token string) error {
log.Debugf("docker creds %v \n", authConfig)
_, err = cli.RegistryLogin(ctx, *authConfig)
if err != nil {
return errors.Errorf("registry login error: %v", err)
return fmt.Errorf("registry login error: %w", err)
}

cliAuthConfig := cliTypes.AuthConfig(*authConfig)
Expand All @@ -56,7 +56,7 @@ func (d *DockerRegistry) Login(username, token string) error {
creds := configFile.GetCredentialsStore(serverAddress)

if err := creds.Store(cliAuthConfig); err != nil {
return errors.Errorf("Error saving credentials: %v", err)
return fmt.Errorf("error saving credentials: %w", err)
}
return nil
}
2 changes: 1 addition & 1 deletion airflow/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package airflow
import (
"bufio"
"bytes"
"errors"
"fmt"
"os"
"strings"
Expand All @@ -15,7 +16,6 @@ import (

"github.com/docker/compose/v2/pkg/api"
"github.com/docker/docker/api/types"
"github.com/pkg/errors"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand Down

0 comments on commit 0621377

Please sign in to comment.