Skip to content

Commit

Permalink
stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
laduke committed Oct 10, 2023
1 parent 57141fb commit 7e393f2
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 6 deletions.
9 changes: 5 additions & 4 deletions README.md
Expand Up @@ -16,11 +16,12 @@ example.com/my-config.json
```

# todo
- [ ] github action tests
- [ ] linter,formatter
- [ ] refarcter
- [ ] local file config
- [x] github action tests
- [x] linter,formatter
- [x] refarcter
- [x] local file config
- [ ] logging
- [ ] accept stdin
- [ ] package for easy install
- [ ] more readme
- [ ] systemd timer service
Expand Down
1 change: 1 addition & 0 deletions bin.js
Expand Up @@ -4,6 +4,7 @@ import { config as loadsConfig } from './src/loads-config.js'
run()

async function run () {

const { tokenPath, urls, files } = loadsConfig()

const config = { tokenPath, urls, files }
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Expand Up @@ -5,6 +5,7 @@ import { load as loadsAuthToken } from './loads-authtoken.js'
import { logger } from './logger.js'



export default function () {
async function run (config = {}) {
logger.debug('running')
Expand Down
15 changes: 15 additions & 0 deletions src/loads-remote-networks.js
@@ -1,3 +1,5 @@
import readline from 'node:readline'

import { getNetworks } from './http-client.js'
import { read} from './read-file.js'

Expand All @@ -15,6 +17,19 @@ export async function loadFiles (paths = []) {
return flatten(result)
}

export async function loadSTDIN () {
const rl = readline.createInterface({
input: process.stdin,
});

let lines = ''
for await (const line of rl) {
lines = lines.concat(line)
}

return JSON.parse(lines)
}

function flatten (xs = []) {
return xs.flatMap((el) => {
return el.networks.map((nw) => {
Expand Down
13 changes: 11 additions & 2 deletions src/loads-resources.js
@@ -1,19 +1,28 @@
import { load as loadLocal } from './loads-local-networks.js'
import { load as loadRemote, loadFiles } from './loads-remote-networks.js'
import { load as loadRemote, loadFiles, loadSTDIN } from './loads-remote-networks.js'
import { validate } from './validates-local-networks.js'
import { validate as validateRemote } from './validates-remote-networks.js'




export async function load ({ urls = [], files = [], authToken, localPort }) {
const local = await loadLocal(authToken, localPort)
validate(local)


const remote1 = await loadRemote(urls)
validateRemote(remote1)

const remote2 = await loadFiles(files)
validateRemote(remote2)

const remote = remote1.concat(remote2)
let remote3 = []
if (!process.stdin.isTTY) {
remote3 = await loadSTDIN()
}

const remote = remote1.concat(remote2).concat(remote3)
.filter(id)

return { local, remote }
Expand Down
6 changes: 6 additions & 0 deletions src/zerotier-one.js
Expand Up @@ -18,11 +18,17 @@ export async function join (config, network) {
const { allowDefault, allowManaged, allowGlobal } = network
const service = new Service({ authToken, port })
return service.set(network.id, { allowDefault, allowManaged, allowGlobal })
.catch(e => {
throw new Error(`Error joining ${network.id} ${e.message}`)
})
}

export async function leave (config, id) {
const { localPort: port, authToken } = config
const service = new Service({ authToken, port })
const result = await service.leave(id)
.catch(e => {
throw new Error(`Error leaving ${network.id} ${e.message}`)
})
return result
}

0 comments on commit 7e393f2

Please sign in to comment.