/// Method to download and save the latest release into workflow's cache dir.
///
/// If the download and save operations are both successful, it returns name of file in which the
/// downloaded Alfred workflow bundle is saved.
///
/// The downloaded workflow will be saved in dedicated cache folder of the workflow, and it
/// will be always renamed to `latest_release_WORKFLOW-NAME.alfredworkflow`
///
/// To install the downloaded release, your workflow needs to somehow open the saved file.
///
/// Within shell, it can be installed by issuing something like:
/// ```bash
/// open -b com.runningwithcrayons.Alfred-3 latest_release_WORKFLOW-NAME.alfredworkflow
/// ```
///
/// Or you can add "Run script" object to your workflow and use environment variables set by
/// Alfred to automatically open the downloaded release:
/// ```bash
/// open -b com.runningwithcrayons.Alfred-3 "$alfred_workflow_cache/latest_release_$alfred_workflow_name.alfredworkflow"
/// ```
///
/// # Note
///
/// The method may take longer than other Alfred-based actions to complete. Workflow authors using this crate
/// should implement strategies to prevent unpleasant long blocks of user's typical work flow.
///
/// One option to initiate the download and upgrade process is to invoke your executable with a
/// different argument. The following snippet can be tied to a dedicated Alfred **Hotkey**
/// or **Script Filter** so that it is only executed when user explicitly asks for it:
///
/// # Example
///
/// ```rust,no_run
/// # extern crate alfred;
/// # extern crate alfred_rs;
/// # extern crate failure;
/// # use std::io;
/// use alfred_rs::Updater;
/// use alfred::{ItemBuilder, json};
///
/// # fn main() {
/// # let updater =
/// #    Updater::gh("spamwax/alfred-pinboard-rs").expect("cannot initiate Updater");
/// # let cmd_line_download_flag = true;
/// if cmd_line_download_flag && updater.update_ready().unwrap() {
///     match updater.download_latest() {
///         Ok(downloaded_fn) => {
///             json::write_items(io::stdout(), &[
///                 ItemBuilder::new("New version of workflow is available!")
///                              .subtitle("Click to upgrade!")
///                              .arg(downloaded_fn.to_str().unwrap())
///                              .variable("update_ready", "yes")
///                              .valid(true)
///                              .into_item()
///             ]);
///         },
///         Err(e) => {
///             // Show an error message to user or log it.
///         }
///     }
/// }
/// #    else {
/// #    }
/// # }
/// ```
///
/// For the above example to automatically work, you then need to connect the output of the script
/// to an **Open File** action so that Alfred can install/upgrade the new version.
///
/// As suggested in above example, you can add an Alfred variable to the item so that your workflow
/// can use it for further processing.
///
/// # Errors
/// Downloading latest workflow can fail if network error, file error or Alfred environment variable
/// errors happen, or if [`Releaser`] cannot produce a usable download url.
///
/// [`Releaser`]: trait.Releaser.html
Method to download and save the latest release into workflow's cache dir.
If the download and save operations are both successful, it returns name of file in which the
downloaded Alfred workflow bundle is saved.

The downloaded workflow will be saved in dedicated cache folder of the workflow, and it
will be always renamed to `latest_release_WORKFLOW-NAME.alfredworkflow`

To install the downloaded release, your workflow needs to somehow open the saved file.




Method to download and save the latest release into workflow ' s cache dir . If the download and save operations are both successful it returns name of file in which the downloaded Alfred workflow bundle is saved . The downloaded workflow will be saved in dedicated cache folder of the workflow and it will be always renamed to ` latest_release_WORKFLOW - NAME . alfredworkflow ` To install the downloaded release your workflow needs to somehow open the saved file .
pub fn download_latest(&self) -> Result<PathBuf, Error> {
        let url = self
            .state
            .download_url()
            .ok_or_else(|| err_msg("no release info avail yet"))?;
        let client = reqwest::Client::new();
        client
            .get(url)
            .send()?
            .error_for_status()
            .map_err(|e| e.into())
            .and_then(|mut resp| {
                // Get workflow's dedicated cache folder & build a filename
                let workflow_name = env::workflow_name()
                    .unwrap_or_else(|| "WhyUNoNameYourOwnWorkflow".to_string())
                    .chars()
                    .map(|c| if c.is_ascii_alphanumeric() { c } else { '_' })
                    .collect::<String>();
                let latest_release_downloaded_fn = env::workflow_cache()
                    .ok_or_else(|| {
                        err_msg(
                            "missing env variable for cache dir. forgot to set workflow bundle id?",
                        )
                    })
                    .and_then(|mut cache_dir| {
                        cache_dir
                            .push(["latest_release_", &workflow_name, ".alfredworkflow"].concat());
                        Ok(cache_dir)
                    })?;
                // Save the file
                File::create(&latest_release_downloaded_fn)
                    .map_err(|e| e.into())
                    .and_then(|fp| {
                        let mut buf_writer = BufWriter::with_capacity(0x10_0000, fp);
                        resp.copy_to(&mut buf_writer)?;
                        Ok(())
                    })
                    .or_else(|e: Error| {
                        let _ = remove_file(&latest_release_downloaded_fn);
                        Err(e)
                    })?;
                Ok(latest_release_downloaded_fn)
            })
    }
pub fn download_latest ( & self ) -> Result < PathBuf , Error > { let url = self . state . download_url ( ) . ok_or_else ( | | err_msg ( " " ) ) ? ; let client = reqwest :: Client :: new ( ) ; client . get ( url ) . send ( ) ? . error_for_status ( ) . map_err ( | e | e . into ( ) ) . and_then ( | mut resp | { // Get workflow's dedicated cache folder & build a filename let workflow_name = env :: workflow_name ( ) . unwrap_or_else ( | | " " . to_string ( ) ) . chars ( ) . map ( | c | if c . is_ascii_alphanumeric ( ) { c } else { '_' } ) . collect :: < String > ( ) ; let latest_release_downloaded_fn = env :: workflow_cache ( ) . ok_or_else ( | | { err_msg ( " " , ) } ) . and_then ( | mut cache_dir | { cache_dir . push ( [ " " , & workflow_name , " " ] . concat ( ) ) ; Ok ( cache_dir ) } ) ? ; // Save the file File :: create ( & latest_release_downloaded_fn ) . map_err ( | e | e . into ( ) ) . and_then ( | fp | { let mut buf_writer = BufWriter :: with_capacity ( 0x10_0000 , fp ) ; resp . copy_to ( & mut buf_writer ) ? ; Ok ( ( ) ) } ) . or_else ( | e : Error | { let _ = remove_file ( & latest_release_downloaded_fn ) ; Err ( e ) } ) ? ; Ok ( latest_release_downloaded_fn ) } ) }
Method to download and save the latest release into workflow's cache dir.
Method to download and save the latest release into workflow ' s cache dir .
&self
note
The method may take longer than other Alfred-based actions to complete. Workflow authors using this crate
should implement strategies to prevent unpleasant long blocks of user's typical work flow.

One option to initiate the download and upgrade process is to invoke your executable with a
different argument. The following snippet can be tied to a dedicated Alfred **Hotkey
or **Script Filter** so that it is only executed when user explicitly asks for it.
The method may take longer than other Alfred - based actions to complete . Workflow authors using this crate should implement strategies to prevent unpleasant long blocks of user ' s typical work flow . One option to initiate the download and upgrade process is to invoke your executable with a different argument . The following snippet can be tied to a dedicated Alfred ** Hotkey or ** Script Filter ** so that it is only executed when user explicitly asks for it .
example
None
extern crate alfred;
None
extern crate alfred_rs;
None
extern crate failure;
None
use std::io;
use alfred_rs::Updater;
use alfred::{ItemBuilder, json}.
use alfred_rs :: Updater ; use alfred :: { ItemBuilder json } .
fn main() {
None
let updater 
None
updater::gh("spamwax/alfred-pinboard-rs").expect("cannot initiate updater");
None
let cmd_line_download_flag = true;
if cmd_line_download_flag && updater.update_ready().unwrap() {
match updater.download_latest() {
Ok(downloaded_fn) => {
json::write_items(io::stdout(), &[
ItemBuilder::new("New version of workflow is available!")
.subtitle("Click to upgrade!")
.arg(downloaded_fn.to_str().unwrap())
.variable("update_ready", "yes")
.valid(true)
.into_item()
]);
},
Err(e) => {
Show an error message to user or log it.
if cmd_line_download_flag && updater . update_ready () . unwrap () { match updater . download_latest () { Ok ( downloaded_fn ) = > { json :: write_items ( io :: stdout () & [ ItemBuilder :: new ( " New version of workflow is available! " ) . subtitle ( " Click to upgrade! " ) . arg ( downloaded_fn . to_str () . unwrap () ) . variable ( " update_ready " " yes " ) . valid ( true ) . into_item () ] ) ; } Err ( e ) = > { Show an error message to user or log it .
else {
None
}
None
}


For the above example to automatically work, you then need to connect the output of the script
to an **Open File** action so that Alfred can install/upgrade the new version.

As suggested in above example, you can add an Alfred variable to the item so that your workflow
can use it for further processing.
For the above example to automatically work you then need to connect the output of the script to an ** Open File ** action so that Alfred can install / upgrade the new version . As suggested in above example you can add an Alfred variable to the item so that your workflow can use it for further processing .
errors
Downloading latest workflow can fail if network error, file error or Alfred environment variable
errors happen, or if [`Releaser`] cannot produce a usable download url.

[`Releaser`]: trait.Releaser.html
Downloading latest workflow can fail if network error file error or Alfred environment variable errors happen or if [ ` Releaser ` ] cannot produce a usable download url . [ ` Releaser ` ] : trait . Releaser . html