Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Add --debug flag to upload commands
Browse files Browse the repository at this point in the history
This is useful if you want to figure out something that might not be
working. We decided to use stderr so that you can turn debugging on
without breaking your happo-ci scripts.
  • Loading branch information
trotzig committed Dec 7, 2016
1 parent 0c0dd76 commit 73e607b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 24 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,19 @@ the argument to `happo upload_diffs`. E.g.
happo upload "https://test.example"
```

To debug uploading, you can use the `--debug` flag. Additional information will
then be printed to `stderr`.

### `happo upload-test`

Uploads a small text file to an AWS S3 Account. This is useful if you want to
test your S3 configuration. Uses the same configuration as [`happo
upload`](#happo-upload-triggeredbyurl) does.
upload`](#happo-upload-triggeredbyurl) does. As with `happo upload`, you can
apply a `--debug` flag here for a more verbose output.

```sh
happo upload-test --debug
```

## Running in a CI environment

Expand Down
32 changes: 22 additions & 10 deletions src/server/S3Uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ const {
} = process.env;

module.exports = class S3Uploader {
constructor() {
process.stdout.write(`Initializing S3 configuration for ${S3_ACCESS_KEY_ID}\n`);
constructor({ debug } = {}) {
this.debug = debug;
this.debugLog(`Initializing S3 configuration for ${S3_ACCESS_KEY_ID}\n`);

AWS.config = new AWS.Config({
accessKeyId: S3_ACCESS_KEY_ID,
secretAccessKey: S3_SECRET_ACCESS_KEY,
region: S3_REGION || 'us-west-2',
logger: process.stdout,
logger: debug && process.stderr,
});

this.s3 = new AWS.S3();
Expand All @@ -28,7 +30,17 @@ module.exports = class S3Uploader {
crypto.randomBytes(16).toString('hex'),
].filter(Boolean).join('/');

process.stdout.write(`Setting S3 directory to ${this.directory}\n`);
this.debugLog(`Setting S3 directory to ${this.directory}\n`);
}

debugLog(message) {
if (!this.debug) {
return;
}

// We print to stderr to avoid messing with the end result (the link to the
// uploaded HTML file)
process.stderr.write(message);
}

/**
Expand All @@ -38,17 +50,17 @@ module.exports = class S3Uploader {
*/
prepare() {
return new Promise((resolve, reject) => {
process.stdout.write(`Checking for bucket ${Bucket}\n`);
this.debugLog(`Checking for bucket ${Bucket}\n`);

this.s3.headBucket({ Bucket }, (headErr) => {
if (headErr) {
process.stdout.write(`Bucket not found, creating new bucket ${Bucket}\n`);
this.debugLog(`Bucket not found, creating new bucket ${Bucket}\n`);
this.s3.createBucket({ Bucket }, (createErr) => {
if (createErr) {
process.stderr.write(`Bucket creation failed ${Bucket}\n`);
this.debugLog(`Bucket creation failed ${Bucket}\n`);
reject(createErr);
} else {
process.stdout.write(`Bucket creation successful ${Bucket}\n`);
this.debugLog(`Bucket creation successful ${Bucket}\n`);
resolve();
}
});
Expand Down Expand Up @@ -82,10 +94,10 @@ module.exports = class S3Uploader {
Key: `${this.directory}/${fileName}`,
};

process.stdout.write('Attempting upload\n');
this.debugLog('Attempting upload\n');
this.s3.upload(uploadParams, (err, { Location }) => {
if (err) {
process.stderr.write('Upload failed\n');
this.debugLog('Upload failed\n');
reject(err);
} else {
resolve(Location);
Expand Down
23 changes: 12 additions & 11 deletions src/server/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,19 @@ commander.command('review-demo').action(() => {
});
});

commander.command('upload [<triggeredByUrl>]').action((triggeredByUrl) => {
uploadLastResult(triggeredByUrl)
.then((url) => {
if (url) {
console.log(url);
}
})
.catch(logAndExit);
});
commander.command('upload [<triggeredByUrl>]').option('--debug').action(
(triggeredByUrl, { debug }) => {
uploadLastResult(triggeredByUrl, { debug })
.then((url) => {
if (url) {
console.log(url);
}
})
.catch(logAndExit);
});

commander.command('upload-test').action(() => {
const uploader = new S3Uploader();
commander.command('upload-test').option('--debug').action(({ debug }) => {
const uploader = new S3Uploader({ debug });
uploader.prepare()
.then(() => {
uploader.upload({
Expand Down
6 changes: 4 additions & 2 deletions src/server/uploadLastResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ function uploadHTMLFile({ uploader, diffImages, newImages, triggeredByUrl }) {
}

/**
* @param {String} triggeredByUrl
* @param {Boolean} options.debug
* @return {Promise} that resolves with a URL to the html document uploaded to
* s3.
*/
module.exports = function uploadLastResult(triggeredByUrl) {
module.exports = function uploadLastResult(triggeredByUrl, { debug }) {
return new Promise((resolve, reject) => {
const { diffImages, newImages } = getLastResultSummary();

Expand All @@ -73,7 +75,7 @@ module.exports = function uploadLastResult(triggeredByUrl) {
return;
}

const uploader = new S3Uploader();
const uploader = new S3Uploader({ debug });
uploader.prepare().then(() => {
const uploadPromises = [];
diffImages.forEach((image) => {
Expand Down

0 comments on commit 73e607b

Please sign in to comment.