forked from suptejas/volt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.rs
More file actions
89 lines (75 loc) · 2.73 KB
/
config.rs
File metadata and controls
89 lines (75 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
Copyright 2021 Volt 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.
*/
use crate::core::utils::errors::VoltError;
use clap::Parser;
use ssri::Algorithm;
use std::{env, path::PathBuf};
#[derive(Debug, Clone, Parser)]
pub struct VoltConfig {
/// Path to current working directory
#[clap(short, long)]
cwd: Option<PathBuf>,
}
impl VoltConfig {
pub const _OS: &'static str = env::consts::OS;
pub const VOLT_HOME: &'static str = ".volt";
pub const _VOLT_LOCK: &'static str = "volt.lock";
pub fn home(&self) -> miette::Result<PathBuf> {
Ok(dirs::home_dir().ok_or(VoltError::GetHomeDirError)?)
}
/// Return the current directory (defaults to `.` if not provided)
pub fn cwd(&self) -> miette::Result<PathBuf> {
Ok(self.cwd.to_owned().unwrap_or({
env::current_dir().map_err(|e| VoltError::EnvironmentError {
env: "CURRENT_DIRECTORY".to_string(),
source: e,
})?
}))
}
/// Path to the volt lockfile (defaults to `./volt.lock`)
pub fn _lockfile(&self) -> miette::Result<PathBuf> {
Ok(self.cwd()?.join(Self::_VOLT_LOCK))
}
/// Path to the `node_modules` directory (defaults to `./node_modules`)
pub fn node_modules(&self) -> miette::Result<PathBuf> {
Ok(self.cwd()?.join("node_modules"))
}
/// Path to the config directory (defaults to `~/.volt`)
pub fn volt_home(&self) -> miette::Result<PathBuf> {
Ok(self.home()?.join(Self::VOLT_HOME))
}
/// Calculate the hash of a tarball
///
/// ## Examples
/// ```rs
/// calc_hash(bytes::Bytes::new(), ssri::Algorithm::Sha1)?;
/// ```
/// ## Returns
/// * Result<String>
pub fn calc_hash(data: &bytes::Bytes, algorithm: Algorithm) -> miette::Result<String> {
let integrity = if algorithm == Algorithm::Sha1 {
let hash = ssri::IntegrityOpts::new()
.algorithm(Algorithm::Sha1)
.chain(&data)
.result();
format!("sha1-{}", hash.to_hex().1)
} else {
ssri::IntegrityOpts::new()
.algorithm(Algorithm::Sha512)
.chain(&data)
.result()
.to_string()
};
Ok(integrity)
}
}