1
0
Fork 0

move go files to another directory

refactor/models
Matthew 2019-12-20 12:25:35 -05:00
parent ace7594c12
commit efc17189c0
No known key found for this signature in database
GPG Key ID: 766BE43AE75F7559
2 changed files with 17 additions and 11 deletions

View File

@ -6,7 +6,7 @@ import (
) )
// DirSize This function returns the total size of a directory in bytes. // DirSize This function returns the total size of a directory in bytes.
func DirSize(path string) float64 { func DirSize(path* string) float64 {
var dirSize int64 = 0 var dirSize int64 = 0
readSize := func(path string, file os.FileInfo, err error) error { readSize := func(path string, file os.FileInfo, err error) error {
@ -17,7 +17,8 @@ func DirSize(path string) float64 {
return nil return nil
} }
filepath.Walk(path, readSize) err := filepath.Walk(*path, readSize)
HandleError(err, 0)
size := float64(dirSize) size := float64(dirSize)

View File

@ -9,7 +9,8 @@ import (
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
"io/ioutil" "io/ioutil"
"time" "time"
"strings"
) )
// Collection the MongoDB Account collection // Collection the MongoDB Account collection
@ -55,6 +56,8 @@ func main() {
fmt.Printf("Connected to MongoDB [GO]\n") fmt.Printf("Connected to MongoDB [GO]\n")
HandleError(err, 1) HandleError(err, 1)
Collection = client.Database("cloudservices").Collection("accounts")
RedisClient = redis.NewClient(&redis.Options{ RedisClient = redis.NewClient(&redis.Options{
Addr: "localhost:6379", Addr: "localhost:6379",
Password: "", Password: "",
@ -73,7 +76,7 @@ func main() {
} }
func handler() { func handler() {
cur, err := Collection.Find(context.TODO(), bson.M{}) cur, err := Collection.Find(context.TODO(), bson.D{})
HandleError(err, 0) HandleError(err, 0)
for cur.Next(context.TODO()) { for cur.Next(context.TODO()) {
@ -84,11 +87,13 @@ func handler() {
} }
func checkAccountSizeAndUpdate(username string, id string) { func checkAccountSizeAndUpdate(username string, id string) {
var bytes float64 = 0 var size float64 = 0
sizeHome := DirSize("/home/" + username) var userHomeDirectory string = strings.Replace(strings.Join([]string{"/home/", string(username)}, ""), "\"", "", -1)
bytes += sizeHome fmt.Println(userHomeDirectory)
sizeMail := DirSize("/var/mail/" + username) sizeHome := DirSize(&userHomeDirectory)
bytes += sizeMail size += sizeHome
RedisClient.Set("storage"+"-"+id, bytes, 0) sizeMail := DirSize(&userHomeDirectory)
fmt.Printf("Set Call | Username: %v, ID: %v | Bytes: %f\n", username, id, bytes) size += sizeMail
RedisClient.Set("storage"+"-"+string(id), size, 0)
fmt.Printf("Set Call | Username: %v, ID: %v | Bytes: %f\n", string(username), string(id), size)
} }