force valid arguments

This commit is contained in:
Tyler Murphy 2022-11-09 12:50:44 -05:00
parent 65345247fb
commit 49c1c8e424
8 changed files with 38 additions and 24 deletions

2
Cargo.lock generated
View file

@ -34,7 +34,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "crab"
version = "0.0.3"
version = "0.0.4"
dependencies = [
"exec",
"nix",

View file

@ -1,6 +1,6 @@
[package]
name = "crab"
version = "0.0.3"
version = "0.0.4"
edition = "2021"
[dependencies]

View file

@ -1,6 +1,6 @@
pkgbase = crab
pkgdesc = A rusty permission authentication system
pkgver = 0.0.3
pkgver = 0.0.4
pkgrel = 1
url = https://g.tylerm.dev/tylermurphy534/crab.git
arch = x86_64

View file

@ -5,7 +5,7 @@
# Maintainer: Tyler Murphy <tylermurphy534@gmail.com>
pkgname=crab
pkgver=0.0.3
pkgver=0.0.4
pkgrel=1
pkgdesc="A rusty permission authentication system"
arch=('x86_64' 'i686')

View file

@ -5,7 +5,7 @@ pub struct Flags {
pub arg_count: usize
}
pub fn parse(args: &[String]) -> Flags {
pub fn parse(args: &[String]) -> Option<Flags> {
let mut flags = Flags {
help: false,
version: false,
@ -17,15 +17,21 @@ pub fn parse(args: &[String]) -> Flags {
flags.arg_count += 1;
if arg.starts_with("--") {
let flag = &arg[2..];
check_flag(&flag, &mut flags);
if !set_flag(&flag, &mut flags) {
eprintln!("Invalid argument: {}", arg);
return None
}
} else {
let flag = &arg[1..];
for char in flag.chars() {
check_flag(&char.to_string(), &mut flags);
if !set_flag(&char.to_string(), &mut flags) {
eprintln!("Invalid argument: {}", arg);
return None
}
}
}
}
flags
Some(flags)
}
fn is_arg(arg: &str) -> bool {
@ -36,16 +42,18 @@ const HELP_FLAG: &str = "help h";
const VERSION_FLAG: &str = "version v";
const DONT_PERSIST: &str = "d";
fn check_flag(arg: &str, flags: &mut Flags) {
fn set_flag(arg: &str, flags: &mut Flags) -> bool {
if has_flag_set(&arg, HELP_FLAG) {
flags.help = true
}
if has_flag_set(&arg, VERSION_FLAG) {
flags.version = true
}
if has_flag_set(&arg, DONT_PERSIST) {
flags.dont_persist = true
flags.help = true;
return true
} else if has_flag_set(&arg, VERSION_FLAG) {
flags.version = true;
return true
} else if has_flag_set(&arg, DONT_PERSIST) {
flags.dont_persist = true;
return true
}
false
}
fn has_flag_set(arg: &str, check: &str) -> bool {

View file

@ -2,7 +2,6 @@ pub fn help() {
let help =
"Usage:
crab [-d] command [args]
Options:
-v --version Get the current version of the package
-h --help Generates the crab help message

View file

@ -5,7 +5,7 @@ use nix::unistd;
extern crate time;
// const ERROR_COMMAND: u8 = 1;
const ERROR_ARGS: u8 = 1;
const ERROR_CONFIG: u8 = 2;
const ERROR_NO_USER: u8 = 3;
const ERROR_NOT_AUTHORIZED: u8 = 4;
@ -18,7 +18,13 @@ mod help;
fn main() -> ExitCode {
let args: Vec<String> = env::args().collect();
let flags = flags::parse(&args[1..]);
let flags = match flags::parse(&args[1..]) {
Some(data) => data,
None => {
help::help();
return ExitCode::from(ERROR_ARGS);
}
};
if flags.version {
println!("crab version 0.0.3");
return ExitCode::SUCCESS;

View file

@ -83,12 +83,13 @@ fn get_terminal_config() -> Option<Value> {
}
fn write_terminal_config(id: &i32, data: &str) -> Result<(), Box<dyn std::error::Error>> {
std::fs::write(path(&id), data)?;
std::fs::write(path(&id), "")?;
unistd::chown(std::path::Path::new(&path(&id)), Some(unistd::Uid::from(0)), Some(unistd::Gid::from(0)))?;
let metadata = std::fs::metadata(path(&id))?;
let mut perms = metadata.permissions();
perms.set_mode(0o660);
fs::set_permissions(path(&id), perms)?;
std::fs::write(path(&id), data)?;
Ok(())
}