use dialoguer::Confirm;
use serde::Serialize;
use std::fs::{self, File};
use std::io::Write;
use std::path::Path;
use crate::style::LogMessage;
#[derive(clap::Args, Debug, Serialize)]
pub struct ReadmeCommands {
#[clap(short, long, value_parser, default_value = ".")]
pub path: String,
#[clap(short, long, value_parser, default_value = "Title")]
pub title: String,
#[clap(short, long, value_parser, default_value = "Description")]
pub description: String,
#[clap(short, long, value_parser)]
pub force: bool,
#[clap(short, long, value_parser)]
pub backup: bool,
}
impl ReadmeCommands {
pub fn parse(&self) {
let binding = Path::new(&self.path).join("README.md");
let path = binding.as_path();
if path.exists() {
if !self.force {
LogMessage::error("the readme already exist, use the --force flag to overwrite it");
return;
}
if self.force && !self.backup {
let override_readme = Confirm::new()
.with_prompt(
"The current readme would not be backed up, do you wish to continue?",
)
.interact()
.unwrap();
if !override_readme {
return;
} else {
fs::remove_file(path).unwrap();
ReadmeCommands::create_new(path, &self.title, &self.description);
}
}
if self.backup {
let backup_path = Path::new(&self.path).join("README.md.bak");
fs::copy(path, backup_path).expect("failed to create backup");
ReadmeCommands::create_new(path, &self.title, &self.description);
}
} else {
ReadmeCommands::create_new(path, &self.title, &self.description);
}
}
fn create_new(path: &Path, title: &str, description: &str) {
let mut file = File::create(path).unwrap();
file.write_all(ReadmeCommands::get_template(title, description).as_bytes())
.unwrap();
}
fn get_template(title: &str, description: &str) -> String {
format!(
r#" # {title}
- [Description](#description)
- [Getting Started](#getting-started)
- [Dependencies](#dependencies)
- [Installing](#installing)
- [Executing program](#executing-program)
- [Documentation](#documentation)
- [Help](#help)
- [Authors](#authors)
- [Version History](#version-history)
- [License](#license)
- [Acknowledgments](#acknowledgments)
## Description
{description}
## Getting Started
### Dependencies
- Describe any prerequisites, libraries, OS version, etc., needed before installing program.
- ex. Windows 10
### Installing
- How/where to download your program
- Any modifications needed to be made to files/folders
### Executing program
- How to run the program
- Step-by-step bullets
```
code blocks for commands
```
## Documentation
Describe any special instructions that are necessary to install a software package on your computer (if applicable).
## Help
Any advise for common problems or issues.
```
command to run if program contains helper info
```
## Authors
Contributors names and contact info
ex. Dominique Pizzie
ex. [@DomPizzie](https://twitter.com/dompizzie)
## Version History
- 0.2
- Various bug fixes and optimizations
- See [commit change]() or See [release history]()
- 0.1
- Initial Release
## License
This project is licensed under the [NAME HERE] License - see the LICENSE.md file for details
## Acknowledgments
Inspiration, code snippets, etc.
"#
)
}
}