Skip to content

Commit

Permalink
Generate random names
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Levick <ryan.levick@fermyon.com>
  • Loading branch information
rylev committed Sep 29, 2023
1 parent 75bd051 commit 77ddf24
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 4 deletions.
83 changes: 83 additions & 0 deletions src/adjectives.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
adventurous
affectionate
ambitious
amiable
blissful
brave
brilliant
caring
cheerful
confident
courageous
creative
delightful
eager
elegant
energetic
enthusiastic
excited
exuberant
friendly
generous
gentle
grateful
happy
harmonious
hopeful
inspirational
intelligent
jovial
joyful
kind
lively
loving
magical
mindful
optimistic
passionate
peaceful
positive
radiant
resilient
sincere
spirited
strong
thankful
thriving
tranquil
uplifting
vibrant
vivacious
warm
wise
wonderful
zealous
admirable
blissful
bountiful
charming
cheerful
compassionate
confident
dazzling
effervescent
empathetic
engaging
enlightened
fascinating
gracious
gregarious
inspiring
jubilant
kindhearted
motivated
nurturing
passionate
radiant
remarkable
spirited
stellar
thrilling
vibrant
wholesome
zealous
23 changes: 19 additions & 4 deletions src/commands/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use crate::{
get_app_id_cloud,
variables::{get_variables, set_variables},
},
random_name::RandomNameGenerator,
spin,
};

Expand Down Expand Up @@ -648,7 +649,14 @@ Would you like to link an existing database or create a new database?"#
label,
databases.into_iter().map(|d| d.name).collect::<Vec<_>>(),
),
1 => prompt_link_to_new_database(name, label),
1 => prompt_link_to_new_database(
name,
label,
databases
.iter()
.map(|d| d.name.as_str())
.collect::<HashSet<_>>(),
),
_ => bail!("Choose unavailable option"),
}
}
Expand All @@ -672,9 +680,16 @@ fn prompt_for_existing_database(
Ok(DatabaseSelection::Existing(database_names.remove(index)))
}

fn prompt_link_to_new_database(name: &str, label: &str) -> Result<DatabaseSelection> {
// TODO: use random name generator
let default_name = format!("{name}-{label}");
fn prompt_link_to_new_database(
name: &str,
label: &str,
existing_names: HashSet<&str>,
) -> Result<DatabaseSelection> {
let generator = RandomNameGenerator::new();
let default_name = generator
.generate_unique(existing_names, 20)
.context("could not generate unique database name")?;

let prompt = format!(
r#"What would you like to name your database?
Note: This name is used when managing your database at the account level. The app "{name}" will refer to this database by the label "{label}".
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod commands;
mod opts;
mod random_name;
mod spin;

use anyhow::{Error, Result};
Expand Down
100 changes: 100 additions & 0 deletions src/nouns.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
cat
dog
bird
lion
tiger
elephant
monkey
snake
bear
rabbit
fish
horse
cow
sheep
pig
frog
kangaroo
giraffe
zebra
dolphin
turtle
whale
gorilla
penguin
hippo
crocodile
jaguar
octopus
koala
rhino
squirrel
otter
lobster
parrot
peacock
panther
cheetah
panda
buffalo
goat
ostrich
camel
raccoon
hedgehog
chimpanzee
elephant
snail
deer
fox
badger
apple
orange
strawberry
grape
watermelon
mango
pear
peach
pineapple
kiwi
coconut
blueberry
lemon
lime
cherry
plum
apricot
raspberry
blackberry
pomegranate
fig
avocado
melon
guava
lychee
mangosteen
durian
persimmon
quince
pawpaw
jackfruit
rambutan
mulberry
gooseberry
cranberry
date
nectarine
cantaloupe
honeydew
tangerine
grapefruit
rhubarb
tomato
boson
quark
moon
sun
earth
planet
squash
49 changes: 49 additions & 0 deletions src/random_name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use std::collections::HashSet;

use rand::seq::SliceRandom;

const ADJECTIVES: &str = include_str!("adjectives.txt");
const NOUNS: &str = include_str!("nouns.txt");

pub struct RandomNameGenerator {
adjectives: Vec<&'static str>,
nouns: Vec<&'static str>,
}

impl RandomNameGenerator {
pub fn new() -> Self {
let adjectives = ADJECTIVES.split("\n").collect();
let nouns = NOUNS.split("\n").collect();
Self { adjectives, nouns }
}

pub fn generate(&self) -> String {
let mut rng = rand::thread_rng();
let adjective = self.adjectives.choose(&mut rng).unwrap();
let noun = self.nouns.choose(&mut rng).unwrap();
format!("{adjective}-{noun}")
}

pub fn generate_unique(&self, existing: HashSet<&str>, max_attempts: usize) -> Option<String> {
let mut i = 0;
while i < max_attempts {
let name = self.generate();
if !existing.contains(name.as_str()) {
return Some(name);
}
i += 1;
}
None
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn can_produce_name() {
let generator = RandomNameGenerator::new();
assert!(generator.generate_unique(Default::default(), 10).is_some());
}
}

0 comments on commit 77ddf24

Please sign in to comment.