Change profile creation to only target paths that don't exist yet
- Will fail if passed an existing path (file or directory)
This commit is contained in:
parent
1fa6e50b7e
commit
741c508017
@ -24,10 +24,32 @@ type Profile struct {
|
|||||||
DB *sql.DB
|
DB *sql.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Custom error
|
||||||
|
*/
|
||||||
|
type ErrTargetAlreadyExists struct {
|
||||||
|
target string
|
||||||
|
}
|
||||||
|
func (err ErrTargetAlreadyExists) Error() string {
|
||||||
|
return fmt.Sprintf("Target already exists: %s", err.target)
|
||||||
|
}
|
||||||
|
|
||||||
// Create a new profile in the given location.
|
|
||||||
// `path` is a directory
|
/**
|
||||||
|
* Create a new profile in the given location.
|
||||||
|
* Fails if target location already exists (i.e., is a file or directory).
|
||||||
|
*
|
||||||
|
* args:
|
||||||
|
* - target_dir: location to create the new profile directory
|
||||||
|
*
|
||||||
|
* returns:
|
||||||
|
* - the newly created Profile
|
||||||
|
*/
|
||||||
func NewProfile(target_dir string) (Profile, error) {
|
func NewProfile(target_dir string) (Profile, error) {
|
||||||
|
if file_exists(target_dir) {
|
||||||
|
return Profile{}, ErrTargetAlreadyExists{target_dir}
|
||||||
|
}
|
||||||
|
|
||||||
user_list_file := path.Join(target_dir, "users.txt")
|
user_list_file := path.Join(target_dir, "users.txt")
|
||||||
settings_file := path.Join(target_dir, "settings.yaml")
|
settings_file := path.Join(target_dir, "settings.yaml")
|
||||||
sqlite_file := path.Join(target_dir, "twitter.db")
|
sqlite_file := path.Join(target_dir, "twitter.db")
|
||||||
@ -35,23 +57,16 @@ func NewProfile(target_dir string) (Profile, error) {
|
|||||||
images_dir := path.Join(target_dir, "images")
|
images_dir := path.Join(target_dir, "images")
|
||||||
videos_dir := path.Join(target_dir, "videos")
|
videos_dir := path.Join(target_dir, "videos")
|
||||||
|
|
||||||
|
// Create the directory
|
||||||
for _, file := range []string{
|
fmt.Printf("Creating new profile: %s\n", target_dir)
|
||||||
user_list_file,
|
err := os.Mkdir(target_dir, os.FileMode(0755))
|
||||||
settings_file,
|
if err != nil {
|
||||||
sqlite_file,
|
return Profile{}, err
|
||||||
profile_images_dir,
|
|
||||||
images_dir,
|
|
||||||
videos_dir,
|
|
||||||
} {
|
|
||||||
if file_exists(file) {
|
|
||||||
return Profile{}, fmt.Errorf("File already exists: %s", file)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create `twitter.db`
|
// Create `twitter.db`
|
||||||
fmt.Printf("Creating %s\n", sqlite_file)
|
fmt.Printf("Creating............. %s\n", sqlite_file)
|
||||||
db, err := sql.Open("sqlite3", sqlite_file)
|
db, err := sql.Open("sqlite3", sqlite_file + "?_foreign_keys=on")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Profile{}, err
|
return Profile{}, err
|
||||||
}
|
}
|
||||||
@ -61,14 +76,14 @@ func NewProfile(target_dir string) (Profile, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create `users.txt`
|
// Create `users.txt`
|
||||||
fmt.Printf("Creating %s\n", user_list_file)
|
fmt.Printf("Creating............. %s\n", user_list_file)
|
||||||
err = os.WriteFile(user_list_file, []byte{}, os.FileMode(0644))
|
err = os.WriteFile(user_list_file, []byte{}, os.FileMode(0644))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Profile{}, err
|
return Profile{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create `settings.yaml`
|
// Create `settings.yaml`
|
||||||
fmt.Printf("Creating %s\n", settings_file)
|
fmt.Printf("Creating............. %s\n", settings_file)
|
||||||
settings := Settings{}
|
settings := Settings{}
|
||||||
data, err := yaml.Marshal(&settings)
|
data, err := yaml.Marshal(&settings)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -80,21 +95,21 @@ func NewProfile(target_dir string) (Profile, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create `profile_images`
|
// Create `profile_images`
|
||||||
fmt.Printf("Creating %s/\n", profile_images_dir)
|
fmt.Printf("Creating............. %s/\n", profile_images_dir)
|
||||||
err = os.Mkdir(profile_images_dir, os.FileMode(0755))
|
err = os.Mkdir(profile_images_dir, os.FileMode(0755))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Profile{}, err
|
return Profile{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create `images`
|
// Create `images`
|
||||||
fmt.Printf("Creating %s/\n", images_dir)
|
fmt.Printf("Creating............. %s/\n", images_dir)
|
||||||
err = os.Mkdir(images_dir, os.FileMode(0755))
|
err = os.Mkdir(images_dir, os.FileMode(0755))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Profile{}, err
|
return Profile{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create `videos`
|
// Create `videos`
|
||||||
fmt.Printf("Creating %s/\n", videos_dir)
|
fmt.Printf("Creating............. %s/\n", videos_dir)
|
||||||
err = os.Mkdir(videos_dir, os.FileMode(0755))
|
err = os.Mkdir(videos_dir, os.FileMode(0755))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Profile{}, err
|
return Profile{}, err
|
||||||
@ -104,6 +119,15 @@ func NewProfile(target_dir string) (Profile, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads the profile at the given location. Fails if the given directory is not a Profile.
|
||||||
|
*
|
||||||
|
* args:
|
||||||
|
* - profile_dir: location to check for the profile
|
||||||
|
*
|
||||||
|
* returns:
|
||||||
|
* - the loaded Profile
|
||||||
|
*/
|
||||||
func LoadProfile(profile_dir string) (Profile, error) {
|
func LoadProfile(profile_dir string) (Profile, error) {
|
||||||
user_list_file := path.Join(profile_dir, "users.txt")
|
user_list_file := path.Join(profile_dir, "users.txt")
|
||||||
settings_file := path.Join(profile_dir, "settings.yaml")
|
settings_file := path.Join(profile_dir, "settings.yaml")
|
||||||
|
@ -29,21 +29,41 @@ func isdir_map(is_dir bool) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func TestNewProfile(t *testing.T) {
|
/**
|
||||||
profile_path := "test_profiles/TestNewProfile"
|
* Should refuse to create a Profile if the target already exists (i.e., is a file or directory).
|
||||||
if !file_exists(profile_path) {
|
*/
|
||||||
err := os.Mkdir(profile_path, 0755)
|
func TestNewProfileInvalidPath(t *testing.T) {
|
||||||
|
gibberish_path := "test_profiles/fjlwrefuvaaw23efwm"
|
||||||
|
if file_exists(gibberish_path) {
|
||||||
|
err := os.RemoveAll(gibberish_path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
err := os.Mkdir(gibberish_path, 0755)
|
||||||
contents, err := os.ReadDir(profile_path)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
if len(contents) != 0 {
|
_, err = persistence.NewProfile(gibberish_path)
|
||||||
t.Fatalf("test_profile not empty at start of test!")
|
if err == nil {
|
||||||
|
t.Errorf("Should have failed to create a profile in an already existing directory!")
|
||||||
|
}
|
||||||
|
if _, is_right_type := err.(persistence.ErrTargetAlreadyExists); !is_right_type {
|
||||||
|
t.Errorf("Expected 'ErrTargetAlreadyExists' error, got %T instead", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Should correctly create a new Profile
|
||||||
|
*/
|
||||||
|
func TestNewProfile(t *testing.T) {
|
||||||
|
profile_path := "test_profiles/TestNewProfile"
|
||||||
|
if file_exists(profile_path) {
|
||||||
|
err := os.RemoveAll(profile_path)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
profile, err := persistence.NewProfile(profile_path)
|
profile, err := persistence.NewProfile(profile_path)
|
||||||
@ -59,7 +79,7 @@ func TestNewProfile(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check files were created
|
// Check files were created
|
||||||
contents, err = os.ReadDir(profile_path)
|
contents, err := os.ReadDir(profile_path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@ -86,24 +106,20 @@ func TestNewProfile(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Should correctly load the Profile
|
||||||
|
*/
|
||||||
func TestLoadProfile(t *testing.T) {
|
func TestLoadProfile(t *testing.T) {
|
||||||
profile_path := "test_profiles/TestLoadProfile"
|
profile_path := "test_profiles/TestLoadProfile"
|
||||||
if !file_exists(profile_path) {
|
if file_exists(profile_path) {
|
||||||
err := os.Mkdir(profile_path, 0755)
|
err := os.RemoveAll(profile_path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
contents, err := os.ReadDir(profile_path)
|
_, err := persistence.NewProfile(profile_path)
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
if len(contents) != 0 {
|
|
||||||
t.Fatalf("test_profile not empty at start of test!")
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = persistence.NewProfile(profile_path)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(err.Error())
|
t.Fatalf(err.Error())
|
||||||
}
|
}
|
||||||
@ -126,5 +142,4 @@ func TestLoadProfile(t *testing.T) {
|
|||||||
if len(profile.UsersList) != 2 {
|
if len(profile.UsersList) != 2 {
|
||||||
t.Errorf("Expected 2 users, got %v", profile.UsersList)
|
t.Errorf("Expected 2 users, got %v", profile.UsersList)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user