-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ion dump #2
Merged
Ion dump #2
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
787b018
Added ion-c submodule.
zslayton 9c61572
Build logic to create ion-c executable.
zslayton 93d8eea
Updated ion-c submodule.
zslayton 61b9e3c
Added `dump` command.
zslayton d2dbbf0
Removed old main.rs
zslayton 23355d8
README fix, comment fixes
zslayton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "ion-c"] | ||
path = ion-c | ||
url = https://github.com/amzn/ion-c.git |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[package] | ||
name = "ion-cli" | ||
version = "0.1.0" | ||
authors = ["The Ion Team <ion-team@amazon.com>"] | ||
edition = "2018" | ||
description = "Command line tool for working with the Ion data format." | ||
repository = "https://github.com/amzn/ion-cli" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
clap = "~2.27.0" | ||
libc = "0.2" | ||
|
||
[build-dependencies] | ||
cmake = "0.1.44" | ||
|
||
[[bin]] | ||
name = "ion" | ||
test = false | ||
bench = false | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
## My Project | ||
## `ion-cli` | ||
|
||
TODO: Fill this README out! | ||
_This package is considered experimental. It is under active/early development, | ||
and the API is subject to change._ | ||
|
||
Be sure to: | ||
## Developer notes | ||
|
||
* Change the title in this README | ||
* Edit your repository description on GitHub | ||
Run the following command to initialize all of the necessary git submodules. | ||
``` | ||
git submodule update --init --recursive | ||
``` | ||
|
||
## Security | ||
|
||
See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more information. | ||
|
||
## License | ||
|
||
This project is licensed under the Apache-2.0 License. | ||
|
||
This project is licensed under the Apache-2.0 License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use cmake::Config; | ||
use std::fs::create_dir_all; | ||
use std::path::Path; | ||
use std::process; | ||
|
||
fn main() { | ||
let build_dir = Path::new("./ion-c/build/release"); | ||
|
||
// Create the ion-c build directory if necessary | ||
if !build_dir.is_dir() { | ||
println!("Creating build directory {}", build_dir.display()); | ||
if let Err(error) = create_dir_all(build_dir) { | ||
eprintln!("Could not create build directory: {:?}", error); | ||
process::exit(1); | ||
} | ||
} | ||
|
||
// Configure and run CMake | ||
Config::new("ion-c") | ||
.define("CMAKE_BUILD_TYPE", "Release") | ||
.out_dir("./ion-c/build/release") | ||
.build(); | ||
|
||
// Output lines that start with "cargo:" are interpreted by Cargo. See the docs for details: | ||
// https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script | ||
|
||
// The `ion` executable statically links to the `ion-c` CLI. The following output tells Cargo | ||
// which libraries to link against and in which directories they can be found. | ||
|
||
// ion_events library | ||
println!("cargo:rustc-link-search=native=./ion-c/build/release/build/tools/events"); | ||
println!("cargo:rustc-link-lib=static=ion_events_static"); | ||
|
||
// ion_c library | ||
println!("cargo:rustc-link-search=native=./ion-c/build/release/build/ionc"); | ||
println!("cargo:rustc-link-lib=static=ionc_static"); | ||
|
||
// decNumber library | ||
println!("cargo:rustc-link-search=native=./ion-c/build/release/build/decNumber"); | ||
println!("cargo:rustc-link-lib=static=decNumberStatic"); | ||
|
||
// C++ library | ||
println!("cargo:rustc-link-search=native=/usr/lib"); | ||
println!("cargo:rustc-link-lib=c++"); | ||
|
||
// ion-c CLI library | ||
println!("cargo:rustc-link-search=native=./ion-c/build/release/build/tools/cli/"); | ||
println!("cargo:rustc-link-lib=static=ion_cli_main"); | ||
|
||
// Only rebuild ion-c if that submodule directory is updated | ||
println!("cargo:rereun-if-changed={}", build_dir.display()); | ||
// ...or if this build script is changed. | ||
println!("cargo:rereun-if-changed=build.rs"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use clap::{App, Arg, ArgMatches}; | ||
|
||
use libc::c_char; | ||
use libc::c_int; | ||
use std::ffi::CString; | ||
use std::ptr; | ||
|
||
// ion_c_cli_main is a C function that lives in the ion-c CLI, to which ion-cli is | ||
// statically linked. | ||
extern "C" { | ||
fn ion_c_cli_main(argc: c_int, argv: *const *const c_char); | ||
} | ||
|
||
fn run_ion_c_cli(args: &[&str]) { | ||
// Convert the length-prefixed Rust str arguments to null-terminated C strings | ||
let argv_as_c_str = args | ||
.iter() | ||
.map(|arg| CString::new(*arg).unwrap()) | ||
.collect::<Vec<CString>>(); | ||
|
||
// Convert the C strings to char * pointers. Note: it's important that we collect() | ||
// the values below into a separate vector from the values above; it guarantees that | ||
// the memory being pointed to will still be valid by the time the ion_c_cli accesses it. | ||
let mut argv_as_char_star = argv_as_c_str | ||
.iter() | ||
.map(|arg| arg.as_ptr()) | ||
.collect::<Vec<*const c_char>>(); | ||
|
||
// The number of arguments as a C int | ||
let argc = argv_as_char_star.len() as c_int; | ||
|
||
// Programs sometimes rely on argv being null-terminated, so we'll push a null onto the array. | ||
argv_as_char_star.push(ptr::null()); | ||
|
||
let argv = argv_as_char_star.as_ptr(); | ||
|
||
unsafe { | ||
ion_c_cli_main(argc, argv); | ||
} | ||
} | ||
|
||
pub fn app() -> App<'static, 'static> { | ||
App::new("dump") | ||
.about("Prints Ion in the requested format") | ||
.arg( | ||
Arg::with_name("format") | ||
.long("format") | ||
.short("f") | ||
.takes_value(true) | ||
.default_value("pretty") | ||
.possible_values(&["binary", "text", "pretty"]) | ||
.help("Output format"), | ||
) | ||
.arg( | ||
Arg::with_name("output") | ||
.long("output") | ||
.short("o") | ||
.takes_value(true) | ||
.help("Output file [default: STDOUT]"), | ||
) | ||
.arg( | ||
// All argv entries after the program name (argv[0]) | ||
// and any `clap`-managed options are considered input files. | ||
Arg::with_name("input") | ||
.index(1) | ||
.multiple(true) | ||
.help("Input file [default: STDIN]"), | ||
) | ||
} | ||
|
||
pub fn run(command_name: &str, matches: &ArgMatches<'static>) { | ||
let mut args: Vec<&str> = vec![command_name, "process"]; | ||
|
||
// -f pretty|text|binary | ||
if let Some(format) = matches.value_of("format") { | ||
args.push("-f"); | ||
args.push(format); | ||
} | ||
|
||
// -o filename | ||
if let Some(output_file) = matches.value_of("output") { | ||
args.push("-o"); | ||
args.push(output_file); | ||
} | ||
|
||
// ...files | ||
if let Some(input_file_iter) = matches.values_of("input") { | ||
for input_file in input_file_iter { | ||
args.push(input_file); | ||
} | ||
} else { | ||
args.push("-"); // Signifies STDIN | ||
} | ||
|
||
run_ion_c_cli(&args); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use clap::{App, ArgMatches}; | ||
|
||
pub mod dump; | ||
|
||
// Creates a Vec of CLI configurations for all of the available built-in commands | ||
pub fn built_in_commands() -> Vec<App<'static, 'static>> { | ||
vec![dump::app()] | ||
} | ||
|
||
// Maps the given command name to the entry point for that command if it exists | ||
pub fn runner_for_built_in_command(command_name: &str) -> Option<fn(&str, &ArgMatches<'static>)> { | ||
let runner = match command_name { | ||
"dump" => dump::run, | ||
_ => return None, | ||
}; | ||
Some(runner) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
mod commands; | ||
|
||
use crate::commands::{built_in_commands, runner_for_built_in_command}; | ||
use clap::{crate_authors, crate_version, App, AppSettings}; | ||
|
||
const PROGRAM_NAME: &str = "ion"; | ||
|
||
fn main() { | ||
let mut app = App::new(PROGRAM_NAME) | ||
.version(crate_version!()) | ||
.author(crate_authors!()) | ||
.setting(AppSettings::SubcommandRequiredElseHelp) | ||
.setting(AppSettings::TrailingVarArg); | ||
|
||
for command in built_in_commands() { | ||
app = app.subcommand(command); | ||
} | ||
|
||
let args = app.get_matches(); | ||
let (command_name, command_args) = args.subcommand(); | ||
|
||
if let Some(runner) = runner_for_built_in_command(command_name) { | ||
// If a runner is registered for the given command name, command_args is guaranteed to | ||
// be defined. | ||
runner(command_name, command_args.unwrap()); | ||
} else { | ||
let message = format!( | ||
"The requested command ('{}') is not supported and clap did not generate an error message.", | ||
command_name | ||
); | ||
unreachable!(message); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The project's directory structure and the contents of this file were inspired by
cargo
, which follows a similar pattern.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you/cargo making some distinction between "built-in" commands and some other type of commands? If not, perhaps the term is simply "subcommands" (here and elsewhere).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. "Built-in" commands are part of the same
ion
executable (and so show up in the output ofion help
) while "external" commands are (potentially 3rd party) executables installed separately on the user's$PATH
. See #1 for some extra detail.