Skip to content

Allow to read config from stdin#895

Merged
sporksmith merged 6 commits intoshadow:devfrom
florian-vuillemot:read_config_from_stdin
Aug 4, 2020
Merged

Allow to read config from stdin#895
sporksmith merged 6 commits intoshadow:devfrom
florian-vuillemot:read_config_from_stdin

Conversation

@florian-vuillemot
Copy link
Copy Markdown
Contributor

No description provided.

@florian-vuillemot
Copy link
Copy Markdown
Contributor Author

@sporksmith @robgjansen do you have have an idea to improve the utility_getFileContent ? Waiting "</shadow>" string is not beautiful. Moreover I'm really surprised that I have to re code this function, do you know another way ?

@sporksmith
Copy link
Copy Markdown
Contributor

@sporksmith @robgjansen do you have have an idea to improve the utility_getFileContent ? Waiting "</shadow>" string is not beautiful. Moreover I'm really surprised that I have to re code this function, do you know another way ?

Typically for this kind of usage you want to read until end-of-file, rather than looking for a specific "end" content pattern.

You might be able to use scanf with the m or a modifier to tell it to allocate memory for you. Unfortunately the s specifier stops at white-space, but you might be able to use an "all-character" character set. e.g. something like. scanf("%a[^]", &buf)

@sporksmith
Copy link
Copy Markdown
Contributor

Alternatively we might be able to just pass "/dev/stdin" as the filename to the existing helper function, but I'm not sure how portable that is.

@florian-vuillemot
Copy link
Copy Markdown
Contributor Author

In case we read until the EOF we have to consider that if a user enters the schema by copy/past in stdin, he also has to send a CTRL+D. It's cleaner in the code but I don't find it really "user friendly" even if I will be surprised if someone does a copy/past of a config file.

@sporksmith
Copy link
Copy Markdown
Contributor

sporksmith commented Jul 20, 2020

Reading until EOF is the convention used by most command-line utilities that read from stdin. I agree it can be a little confusing for someone new to the command-line, but I don't think it's worth breaking convention.

Typically the user would be using shell redirection or a pipe to send the contents in anyway, rather than copy-pasting. e.g. convert yml2xml config.yml | shadow -

@florian-vuillemot
Copy link
Copy Markdown
Contributor Author

It seems that scanf("%a[^]", &buf) is not allowed by the compiler. I didn't find any easy and clean way to do the job with scanf.. So I let fgets read until EOF.

I will add tests now except if you have any other idea for cleaning. :)

file = utility_getFileContents(fileName->str);
// Read config from file or stdin
if (0 == g_strcmp0("-", fileName->str)) {
file = utility_getFileContents("/dev/stdin");
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Maybe could I remove this condition by relocating /dev/stdin string in the master->options hydration ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't follow what you're suggesting, but this looks fine as is.

@florian-vuillemot
Copy link
Copy Markdown
Contributor Author

I would like to add some tests. I thought in the config directory by adding 2 sub directories in it (convertor, read_from_stdin). What do you think?

@codecov
Copy link
Copy Markdown

codecov bot commented Jul 21, 2020

Codecov Report

Merging #895 into dev will decrease coverage by 0.05%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##              dev     #895      +/-   ##
==========================================
- Coverage   50.41%   50.36%   -0.06%     
==========================================
  Files         124      125       +1     
  Lines       16925    16929       +4     
  Branches     4326     4327       +1     
==========================================
- Hits         8533     8526       -7     
- Misses       5663     5677      +14     
+ Partials     2729     2726       -3     
Flag Coverage Δ
#tests 50.36% <100.00%> (-0.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/test/config/convert/test_config.c 100.00% <ø> (ø)
src/main/core/master.c 46.90% <100.00%> (+0.71%) ⬆️
src/test/config/read_from_stdin/test_config.c 100.00% <100.00%> (ø)
src/main/shmem/shmem_cleanup.c 51.02% <0.00%> (-10.21%) ⬇️
src/main/shmem/shmem_file.c 46.87% <0.00%> (-6.25%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 17b9e45...c43e9a1. Read the comment docs.

@sporksmith
Copy link
Copy Markdown
Contributor

I would like to add some tests. I thought in the config directory by adding 2 sub directories in it (convertor, read_from_stdin). What do you think?

Is the idea to move the tests currently in the config directory to a new subdirectory convert, and add test(s) for this new functionality in read_from_stdin? Sounds good to me.

file = utility_getFileContents(fileName->str);
// Read config from file or stdin
if (0 == g_strcmp0("-", fileName->str)) {
file = utility_getFileContents("/dev/stdin");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't follow what you're suggesting, but this looks fine as is.

@sporksmith
Copy link
Copy Markdown
Contributor

@robgjansen LMK whether you want to review; otherwise I can go ahead and (squash) and merge

@sporksmith sporksmith merged commit 5d3a1b4 into shadow:dev Aug 4, 2020
@florian-vuillemot florian-vuillemot deleted the read_config_from_stdin branch August 11, 2020 14:26
@robgjansen robgjansen mentioned this pull request Aug 27, 2020
5 tasks
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.

3 participants