Skip to content

Conversation

@x1022as
Copy link

@x1022as x1022as commented Sep 1, 2016

add usage message to validate:
'cat document.json | validate schema.json'
validate would block in such case:
'validate schema.json'
add stdin status check to avoid this.

Signed-off-by: Deng Guangxing [email protected]
before:

$ ./validate config-schema.json 


^C$

after:

$ ./validate config-schema.json 
Error: document.json not specified

Usage:
     ./validate <schema.json> <document.json>
or
    cat <document.json> | ./validate <schema.json>

@wking
Copy link
Contributor

wking commented Sep 1, 2016

On Thu, Sep 01, 2016 at 05:17:23AM -0700, x1022as wrote:

before:

$ ./validate config-schema.json 


^C$

after:

$ ./validate config-schema.json 
Error: document.json not specified

Lots of commands (e.g. cat) block if they expect input on stdin and
stdin is still open. I don't see a problem with the current behavior.
Maybe the user intends to type the input JSON by hand or paste it in?
Or maybe they're doing some terminal magic to feed in the JSON.
And ^C is easy to do if you forget ;).

@x1022as
Copy link
Author

x1022as commented Sep 2, 2016

@wking thanks for reviewing.
this pr means to attach validate stdin to a pipe, the usage message takes 'cat' as an example.
I don't know about terminal magic, but I've tried like this before, and it didn't work out for me

$validate schema.json
echo '$JSON_STRING' > /proc/$VALIDATE_PID/fd/0

I'm not sure if this can make sense :)

@wking
Copy link
Contributor

wking commented Sep 2, 2016

On Thu, Sep 01, 2016 at 05:10:31PM -0700, x1022as wrote:

this pr means to attach validate stdin to a pipe, the usage message
takes 'cat' as an example.

It doesn't do anything about attaching it to a pipe, it just errors
out if it's attached to a terminal. Being attached to a terminal does
not mean ioutil.ReadAll(os.Stdin) will block forever. For example
(with a build of the master ‘validate’).

  • an immediate ctrl+d raises a panic (we should probably fix this):

    $ ./validate state-schema.json
    panic: EOF

    goroutine 1 [running]:
    panic(0x70a860, 0xc820101590)
    /usr/lib/go/src/runtime/panic.go:481 +0x3e6
    main.main()
    /home/wking/src/opencontainers/runtime-spec/schema/validate.go:46 +0x5fc

  • typing ‘{}’ + \n + ctrl+d:

    $ ./validate state-schema.json
    {}
    The document is not valid. see errors :

    • ociVersion: ociVersion is required
    • id: id is required
    • status: status is required
    • pid: pid is required
    • bundlePath: bundlePath is required

I haven't dug in enough to figure out how to programmatically write to
a terminal stdin and then close that stdin, but I'm sure
pseudoterminals give you a way to do that.

My point is that I don't think “special handling when stdin is a
terminal” is a good idea. You're trying to guard from:

  1. User types ‘./validate state-schema.json’.
  2. User sits for a long time, waiting for ‘validate’ to do something.
    Meanwhile, ‘validate’ waits watching stdin, waiting for the user
    (or someone with write-access to its stdin) to do something.

I agree that is a situation that will happen, but don't think it will
happen frequently enough to be worth guarding against. Especially
since some of the cases that guard considers errors may be valid use
cases. cat, grep, etc. all agree and treat TTYs like any other stdin.

@x1022as
Copy link
Author

x1022as commented Sep 2, 2016

@wking that make sense, and I think a better usage would help a lot;)

@wking
Copy link
Contributor

wking commented Sep 2, 2016

On Thu, Sep 01, 2016 at 11:13:39PM -0700, x1022as wrote:

@wking that make sense, and I think a better usage would help a lot;)

Agreed. I'd be fine with using an actual command-line-argument
library here, so you could do ‘validate --help’ and such.

@x1022as x1022as changed the title add usage and fix stdin block problem of validate improve validate usage message Sep 5, 2016
@x1022as
Copy link
Author

x1022as commented Sep 5, 2016

@wking sorry for late response, validate seems to be very simple for now, I am not sure if we need a cli library here. cobra(as docker and runc use it) may be a little bit complex for this.
what do others think about this?

@cyphar
Copy link
Member

cyphar commented Sep 5, 2016

Why don't we just follow every other CLI and make it so that if you specify - as a parameter, the program takes that to mean "just read from stdin". No need for checking what the mode of stdin is or any other cleverness.

@wking
Copy link
Contributor

wking commented Sep 7, 2016

On Mon, Sep 05, 2016 at 05:39:40AM -0700, x1022as wrote:

cobra(as docker and runc use it) may be a little bit complex for
this.

I haven't seen cobra used before, but urfave/cli is pretty simple 1.

@wking
Copy link
Contributor

wking commented Sep 7, 2016

On Mon, Sep 05, 2016 at 06:19:32AM -0700, Aleksa Sarai wrote:

Why don't we just follow every other CLI and make it so that if you
specify - as a parameter, the program takes that to mean "just
read from stdin".

I think the pattern is “default to reading from stdin, unless a file
is specified whose name is not ‘-’”. For example 1:

file
A pathname of an input file. If no file operands are specified,
the standard input shall be used. If a file is '-', the cat
utility shall read from the standard input at that point in the
sequence. The cat utility shall not close and reopen standard
input when it is referenced in this way, but shall accept multiple
occurrences of '-' as a file operand.

In that case, I don't see the point of bothering with the special ‘-’.

Or are you suggesting we only support ‘-’ for stdin and error out if
no file is specified? I can't think of another command that uses that
pattern off the top of my head, but requiring an explicit argument
makes it less likely that the user accidentally invokes ‘validate’ to
read from stdin.

No need for checking what the mode of stdin is or any other
cleverness.

Agreed.

@x1022as x1022as force-pushed the validate branch 2 times, most recently from db70d1e to 9a0a926 Compare September 8, 2016 15:35
@x1022as
Copy link
Author

x1022as commented Sep 8, 2016

validate now works as following:

$ ./validate -s config-schema.json config.json 
The document is valid
$ cat config.json | ./validate -s config-schema.json 
The document is valid
$ ./validate -s config-schema.json 
Input your DOCUMENT CONTENT here, ended with ctrl+d or your self-defined EOF keys

ping

cli.StringFlag{
Name: "schema,s",
Value: "",
Usage: "specify the schema-json file",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no validation without a schema, so I'd rather keep this as a positional parameter (and not make it an option).

@tianon
Copy link
Member

tianon commented Nov 4, 2016

@vbatts you're the primary author of this script 😄 What're your thoughts on introducing urfave/cli here?

result, err := gojsonschema.Validate(schemaLoader, documentLoader)
if err != nil {
panic(err.Error())
return fmt.Errorf("Error: json validate error: %s", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: "Error: json..." -> "json..."

@vbatts
Copy link
Member

vbatts commented Jan 11, 2017

I'm not terribly opposed to this. I know urfave/cli does fancy-things-easily, but I suppose I was going to minimally functional and preferring std-lib before having remote dependencies. If you want to see that 2 arguments are required, then check flag.NArg().

@vbatts
Copy link
Member

vbatts commented Jan 11, 2017

side-note: i reckon we need to vendor github.com/xeipuuv/gojsonschema source code to this repo for perpetuity

@crosbymichael
Copy link
Member

ping @vbatts to make a decision :)

I'd vote for not having any dependencies in this repo. Keep it minimal.

@vbatts
Copy link
Member

vbatts commented Feb 7, 2017

dang @crosbymichael . I though you loved urfave/cli.

@x1022as can we do this feature without pulling in the urfave/cli repo please.

@crosbymichael
Copy link
Member

@vbatts I do love it but sometimes simple is better, I think for the needsd we have, its good to leave out in this repo.

@x1022as
Copy link
Author

x1022as commented Feb 8, 2017

@vbatts of course, will do this later :)

@x1022as
Copy link
Author

x1022as commented Feb 8, 2017

updated

this commit contains:
* add explicit usage message to validate
* schemaPath was overrided by filepath.Abs(), schemaLoader would not get
* the abs path.
* check local scheme and document file path with os.Stat()

Signed-off-by: Deng Guangxing <[email protected]>
@x1022as
Copy link
Author

x1022as commented Feb 8, 2017

This pr contains:

  • add explicit usage message to validate
  • schemaPath was overrided by filepath.Abs(), schemaLoader would not get the abs path.
  • check local scheme and document file path with os.Stat()

@vbatts
Copy link
Member

vbatts commented Feb 8, 2017

LGTM

Approved with PullApprove

1 similar comment
@mrunalp
Copy link
Contributor

mrunalp commented Feb 8, 2017

LGTM

Approved with PullApprove

@mrunalp mrunalp merged commit 7c1a16b into opencontainers:master Feb 8, 2017
@vbatts vbatts mentioned this pull request Mar 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants