<commit_before>// Copyright (c) 2017-2017 Chef Software Inc. and/or applicable contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

extern crate ansi_term;
#[macro_use]
extern crate clap;
extern crate env_logger;
extern crate habitat_common as common;
#[macro_use]
extern crate habitat_core as hcore;
extern crate habitat_launcher_client as launcher_client;
#[macro_use]
extern crate habitat_sup as sup;
extern crate habitat_sup_protocol as protocol;
extern crate libc;
#[macro_use]
extern crate log;
extern crate protobuf;
extern crate time;
extern crate tokio_core;
extern crate url;

use std::env;
use std::io::{self, Write};
use std::net::{SocketAddr, ToSocketAddrs};
use std::path::Path;
use std::process;
use std::result;
use std::str::{self, FromStr};

use clap::{App, ArgMatches};
use common::command::package::install::InstallSource;
use common::ui::{Coloring, NONINTERACTIVE_ENVVAR, UI};
use hcore::channel;
#[cfg(windows)]
use hcore::crypto::dpapi::encrypt;
use hcore::crypto::{self, default_cache_key_path, SymKey};
use hcore::env as henv;
use hcore::url::{bldr_url_from_env, default_bldr_url};
use launcher_client::{LauncherCli, ERR_NO_RETRY_EXCODE, OK_NO_RETRY_EXCODE};
use protocol::{ctl::ServiceBindList,
               types::{ApplicationEnvironment, BindingMode, ServiceBind, ServiceGroup, Topology,
                       UpdateStrategy}};
use url::Url;

use sup::command;
use sup::config::{GossipListenAddr, GOSSIP_DEFAULT_PORT};
use sup::error::{Error, Result, SupError};
use sup::feat;
use sup::http_gateway;
use sup::manager::{Manager, ManagerConfig};
use sup::util;
use sup::VERSION;

/// Our output key
static LOGKEY: &'static str = "MN";

static RING_ENVVAR: &'static str = "HAB_RING";
static RING_KEY_ENVVAR: &'static str = "HAB_RING_KEY";

fn main() {
    if let Err(err) = start() {
        println!("{}", err);
        match err {
            SupError {
                err: Error::ProcessLocked(_),
                ..
            } => process::exit(ERR_NO_RETRY_EXCODE),
            SupError {
                err: Error::Departed,
                ..
            } => {
                process::exit(ERR_NO_RETRY_EXCODE);
            }
            _ => process::exit(1),
        }
    }
}

fn boot() -> Option<LauncherCli> {
    env_logger::init();
    enable_features_from_env();
    if !crypto::init() {
        println!("Crypto initialization failed!");
        process::exit(1);
    }
    match launcher_client::env_pipe() {
        Some(pipe) => match LauncherCli::connect(pipe) {
            Ok(launcher) => Some(launcher),
            Err(err) => {
                println!("{}", err);
                process::exit(1);
            }
        },
        None => None,
    }
}

fn start() -> Result<()> {
    let launcher = boot();
    let app_matches = match cli().get_matches_safe() {
        Ok(matches) => matches,
        Err(err) => {
            let out = io::stdout();
            writeln!(&mut out.lock(), "{}", err.message).expect("Error writing Error to stdout");
            process::exit(ERR_NO_RETRY_EXCODE);
        }
    };
    match app_matches.subcommand() {
        ("bash", Some(_)) => sub_bash(),
        ("run", Some(m)) => {
            let launcher = launcher.ok_or(sup_error!(Error::NoLauncher))?;
            sub_run(m, launcher)
        }
        ("sh", Some(_)) => sub_sh(),
        ("term", Some(m)) => sub_term(m),
        _ => unreachable!(),
    }
}<commit_msg>[hab-sup] Exit 0 when given --help or --version<commit_after>// Copyright (c) 2017-2017 Chef Software Inc. and/or applicable contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

extern crate ansi_term;
#[macro_use]
extern crate clap;
extern crate env_logger;
extern crate habitat_common as common;
#[macro_use]
extern crate habitat_core as hcore;
extern crate habitat_launcher_client as launcher_client;
#[macro_use]
extern crate habitat_sup as sup;
extern crate habitat_sup_protocol as protocol;
extern crate libc;
#[macro_use]
extern crate log;
extern crate protobuf;
extern crate time;
extern crate tokio_core;
extern crate url;

use std::env;
use std::io::{self, Write};
use std::net::{SocketAddr, ToSocketAddrs};
use std::path::Path;
use std::process;
use std::result;
use std::str::{self, FromStr};

use clap::{App, ArgMatches};
use common::command::package::install::InstallSource;
use common::ui::{Coloring, NONINTERACTIVE_ENVVAR, UI};
use hcore::channel;
#[cfg(windows)]
use hcore::crypto::dpapi::encrypt;
use hcore::crypto::{self, default_cache_key_path, SymKey};
use hcore::env as henv;
use hcore::url::{bldr_url_from_env, default_bldr_url};
use launcher_client::{LauncherCli, ERR_NO_RETRY_EXCODE, OK_NO_RETRY_EXCODE};
use protocol::{ctl::ServiceBindList,
               types::{ApplicationEnvironment, BindingMode, ServiceBind, ServiceGroup, Topology,
                       UpdateStrategy}};
use url::Url;

use sup::command;
use sup::config::{GossipListenAddr, GOSSIP_DEFAULT_PORT};
use sup::error::{Error, Result, SupError};
use sup::feat;
use sup::http_gateway;
use sup::manager::{Manager, ManagerConfig};
use sup::util;
use sup::VERSION;

/// Our output key
static LOGKEY: &'static str = "MN";

static RING_ENVVAR: &'static str = "HAB_RING";
static RING_KEY_ENVVAR: &'static str = "HAB_RING_KEY";

fn main() {
    if let Err(err) = start() {
        println!("{}", err);
        match err {
            SupError {
                err: Error::ProcessLocked(_),
                ..
            } => process::exit(ERR_NO_RETRY_EXCODE),
            SupError {
                err: Error::Departed,
                ..
            } => {
                process::exit(ERR_NO_RETRY_EXCODE);
            }
            _ => process::exit(1),
        }
    }
}

fn boot() -> Option<LauncherCli> {
    env_logger::init();
    enable_features_from_env();
    if !crypto::init() {
        println!("Crypto initialization failed!");
        process::exit(1);
    }
    match launcher_client::env_pipe() {
        Some(pipe) => match LauncherCli::connect(pipe) {
            Ok(launcher) => Some(launcher),
            Err(err) => {
                println!("{}", err);
                process::exit(1);
            }
        },
        None => None,
    }
}

fn start() -> Result<()> {
    let launcher = boot();
    let app_matches = match cli().get_matches_safe() {
        Ok(matches) => matches,
        Err(err) => {
            let out = io::stdout();
            writeln!(&mut out.lock(), "{}", err.message).expect("Error writing Error to stdout");
            match launcher {
                Some(_) => process::exit(ERR_NO_RETRY_EXCODE),
                // If we weren't started by a launcher, exit 0 for
                // help and version
                None => match err.kind {
                    clap::ErrorKind::HelpDisplayed => process::exit(0),
                    clap::ErrorKind::VersionDisplayed => process::exit(0),
                    _ => process::exit(ERR_NO_RETRY_EXCODE),
                },
            }
        }
    };
    match app_matches.subcommand() {
        ("bash", Some(_)) => sub_bash(),
        ("run", Some(m)) => {
            let launcher = launcher.ok_or(sup_error!(Error::NoLauncher))?;
            sub_run(m, launcher)
        }
        ("sh", Some(_)) => sub_sh(),
        ("term", Some(m)) => sub_term(m),
        _ => unreachable!(),
    }
}
{'fraction_non_alphanumeric': 0.1390118435219524, 'fraction_numerical': 0.004067472185668142, 'mean_word_length': 2.8883720930232557, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3070424', 'n_tokens_mistral': 2605, 'n_tokens_neox': 2534, 'n_words': 679}