generated from GDGVIT/template
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from Dhruv9449/v2
refactor: cli and web apps
- Loading branch information
Showing
10 changed files
with
259 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,7 @@ | |
.db | ||
data | ||
credentials | ||
test/ | ||
test/ | ||
backups/ | ||
prod/ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package commands | ||
|
||
import "github.com/urfave/cli/v2" | ||
|
||
// AddCommands adds the commands to the app | ||
func AddCommands(cliApp *cli.App) { | ||
// Add the commands to the app | ||
cliApp.Commands = append(cliApp.Commands, userCommands...) | ||
cliApp.Commands = append(cliApp.Commands, TimetableCommands...) | ||
} |
2 changes: 1 addition & 1 deletion
2
...d-api/cmd/management/timetableCommands.go → ...end-api/cli/commands/timetableCommands.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package management | ||
package commands | ||
|
||
import ( | ||
"fmt" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package commands | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/GDGVIT/vitty-backend/vitty-backend-api/internal/database" | ||
"github.com/GDGVIT/vitty-backend/vitty-backend-api/internal/models" | ||
"github.com/GDGVIT/vitty-backend/vitty-backend-api/internal/utils" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
var userCommands = []*cli.Command{ | ||
{ | ||
Name: "createsuperuser", | ||
Aliases: []string{"csu"}, | ||
Usage: "Create a superuser", | ||
Action: createSuperuser, | ||
}, | ||
{ | ||
Name: "createadminuser", | ||
Aliases: []string{"cau"}, | ||
Usage: "Create an admin user", | ||
Action: createAdminuser, | ||
}, | ||
{ | ||
Name: "getusers", | ||
Aliases: []string{"gu"}, | ||
Usage: "Get users", | ||
Action: getUsers, | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "filter", | ||
Aliases: []string{"f"}, | ||
Usage: "Filter users by", | ||
Required: false, | ||
}, | ||
&cli.StringFlag{ | ||
Name: "value", | ||
Aliases: []string{"v"}, | ||
Usage: "Value of filter", | ||
Required: false, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "deleteuser", | ||
Aliases: []string{"du"}, | ||
Usage: "Delete user", | ||
Action: deleteUser, | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "key", | ||
Aliases: []string{"k"}, | ||
Usage: "Key of user to delete", | ||
Required: false, | ||
}, | ||
&cli.StringFlag{ | ||
Name: "value", | ||
Aliases: []string{"v"}, | ||
Usage: "Value of key", | ||
Required: false, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
func createSuperuser(c *cli.Context) error { | ||
fmt.Println("Enter username: ") | ||
var username string | ||
fmt.Scanln(&username) | ||
|
||
if !utils.CheckUserExists(username) { | ||
return errors.New("user does not exist") | ||
} | ||
user := utils.GetUserByUsername(username) | ||
user.Role = "superuser" | ||
err := database.DB.Save(&user).Error | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println("Superuser created successfully") | ||
return nil | ||
} | ||
|
||
func createAdminuser(c *cli.Context) error { | ||
fmt.Println("Enter username: ") | ||
var username string | ||
fmt.Scanln(&username) | ||
|
||
if !utils.CheckUserExists(username) { | ||
return errors.New("user does not exist") | ||
} | ||
user := utils.GetUserByUsername(username) | ||
user.Role = "admin" | ||
err := database.DB.Save(&user).Error | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println("Admin user created successfully") | ||
return nil | ||
} | ||
|
||
func getUsers(c *cli.Context) error { | ||
// Take arguements as name of filter and value of filter | ||
// If no arguements are given, return all users | ||
// If arguements are given, return users that match the filter | ||
|
||
filter := c.String("filter") | ||
value := c.String("value") | ||
|
||
var users []models.User | ||
filter_query := fmt.Sprintf("%s = ?", filter) | ||
|
||
if filter == "" || value == "" { | ||
database.DB.Find(&users) | ||
} else { | ||
database.DB.Where(filter_query, value).Find(&users) | ||
} | ||
|
||
// Display Users in a table | ||
fmt.Println("ID\tUsername\t\tRole\tRegNo\tEmail\tPicture") | ||
for _, user := range users { | ||
fmt.Printf("\t%s\t\t%s\t\t%s\t\t%s\t\t%s\n", user.Username, user.Role, user.RegNo, user.Email, user.Picture) | ||
} | ||
return nil | ||
} | ||
|
||
func deleteUser(c *cli.Context) error { | ||
key := c.String("key") | ||
value := c.String("value") | ||
|
||
if key == "" || value == "" { | ||
fmt.Println("Enter key: ") | ||
fmt.Scanln(&key) | ||
fmt.Println("Enter value: ") | ||
fmt.Scanln(&value) | ||
} | ||
|
||
var users models.User | ||
filter_query := fmt.Sprintf("%s = ?", key) | ||
err := database.DB.Where(filter_query, value).Delete(&users).Error | ||
if err != nil { | ||
fmt.Printf("Error: %s\n", err) | ||
return err | ||
} | ||
fmt.Println("User deleted successfully") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package vittyCli | ||
|
||
import ( | ||
"github.com/GDGVIT/vitty-backend/vitty-backend-api/cli/commands" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func NewCliApp() *cli.App { | ||
// New cli app | ||
cliApp := cli.NewApp() | ||
|
||
// Set the name, usage and version of the app | ||
cliApp.Name = "Vitty" | ||
cliApp.Usage = "Vitty Backend API" | ||
cliApp.Version = "0.0.1" | ||
cliApp.Authors = []*cli.Author{ | ||
{ | ||
Name: "Dhruv Shah", | ||
Email: "[email protected]", | ||
}} | ||
cliApp.EnableBashCompletion = true | ||
cliApp.Description = "CLI for Vitty Backend API" | ||
|
||
// Set the commands | ||
commands.AddCommands(cliApp) | ||
|
||
return cliApp | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.