Skip to content

Commit

Permalink
Add update command / option
Browse files Browse the repository at this point in the history
  • Loading branch information
doesdev committed Mar 16, 2018
1 parent 30be910 commit 0cdc2e1
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 8 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ accepts `opts` as below, returns `ngo` function which executes Go commands
- **env** *(Object, environment vars to set for the Go command - optional)*
- **goRoot** *(String, Go root path (ex. `/usr/local/go`) - optional)*
- **goPath** *(String, Go workspace path (ex. `~/work`) - optional)*
- **update** *(Boolean, Update local install to latest - optional)*

### ngo(arguments, options)
returns promise which resolves to [`execa`](https://github.com/sindresorhus/execa) styled object
Expand All @@ -39,6 +40,10 @@ returns promise which resolves to [`execa`](https://github.com/sindresorhus/exec
```sh
$ ngo version
# go version go1.8.3 windows/amd64

# to update the `ngo` install of Go (won't update system version)
$ ngo-update
# go version go1.9.4 windows/amd64
```

#### programmatic usage
Expand Down
5 changes: 5 additions & 0 deletions cli-update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#! /usr/bin/env node
'use strict'

const ngo = require('./index.js')({update: true})
ngo().then((r) => console.log(r.stdout || r.stdout)).catch(console.error)
2 changes: 0 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#! /usr/bin/env node
'use strict'

// Setup
const ngo = require('./index.js')()
const args = process.argv.slice(2)

// Main
ngo(args).then((r) => console.log(r.stdout || r.stdout)).catch(console.error)
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ module.exports = (opts = {}) => {
env.GOARCH = goArch = goArch || env.GOARCH || arch
env.ngoBin = path.join(goRoot, 'bin', 'go')
try { env.hasBin = exists(path.join(goRoot, 'bin')) } catch (ex) {}
return (args, cmdOpts = {}) => runner(args, Object.assign({}, cmdOpts, {env}))
if (opts.update) delete env.hasBin
return (args, cmdOpts = {}) => {
if (opts.update) args = args || ['version']
return runner(args, Object.assign({}, cmdOpts, {env}))
}
}

function runner (args, opts) {
if (!args) return Promise.reject(new Error(`No GO command specified`))
if (!args) return Promise.reject(new Error(`No Go command specified`))
args = Array.isArray(args) ? args : [args]
let env = opts.env
if (env.hasBin) return execa(env.ngoBin, args, opts)
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
{
"name": "ngo",
"version": "2.1.1",
"version": "2.2.0",
"description": "Run Go commands, whether your Go env is in place or not",
"main": "index.js",
"files": [
"index.js",
"cli.js"
"cli.js",
"vendor/gopath"
],
"scripts": {
"test": "ava --verbose"
},
"bin": {
"ngo": "cli.js"
"ngo": "cli.js",
"ngo-update": "cli-update.js"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 0cdc2e1

Please sign in to comment.