Skip to content

Commit

Permalink
implemented create admin
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirari04 committed Dec 1, 2023
1 parent ec21e13 commit 91169d2
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 42 deletions.
10 changes: 5 additions & 5 deletions console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ func main() {
// loop over arguments and exec all matching functions
for _, v := range argsWithoutProg {
switch v {
case "seed:adminuser":
log.Println("running seed:adminuser")
if err := console_helpers.SeedAdminUser(); err != nil {
case "create:adminuser":
log.Println("running create:adminuser")
if err := console_helpers.CreateAdminUser(); err != nil {
log.Println(err)
} else {
log.Println("success seed:adminuser")
log.Println("success create:adminuser")
}
case "migrate":
log.Println("running migrate")
Expand All @@ -59,7 +59,7 @@ func main() {
func functions() {
log.Println("")
log.Println("Available commands:")
log.Println("seed:adminuser")
log.Println("create:adminuser")
log.Println("fresh:database")
log.Println("migrate")
}
46 changes: 46 additions & 0 deletions console/helpers/CreateAdminUser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package console_helpers

import (
"bufio"
"ch/kirari04/videocms/helpers"
"ch/kirari04/videocms/inits"
"ch/kirari04/videocms/models"
"fmt"
"os"
"syscall"

"golang.org/x/term"
)

func CreateAdminUser() error {
reader := bufio.NewReader(os.Stdin)

fmt.Print("Enter Username: ")
username, err := reader.ReadString('\n')
if err != nil {
return err
}

fmt.Print("Enter Password: ")
bytePassword, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
return err
}

password := string(bytePassword)

hash, _ := helpers.HashPassword(password)
if res := inits.DB.Create(&models.User{
Username: username,
Hash: hash,
Admin: true,
Settings: models.UserSettings{
WebhooksEnabled: true,
WebhooksMax: 10,
},
}); res.Error != nil {
return fmt.Errorf("error while creating admin user: %s", res.Error.Error())
}

return nil
}
4 changes: 0 additions & 4 deletions console/helpers/FreshDatabase.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,5 @@ func FreshDatabase() error {
inits.Database()
inits.Models()

if err := SeedAdminUser(); err != nil {
return err
}

return nil
}
33 changes: 0 additions & 33 deletions console/helpers/SeedAdminUser.go

This file was deleted.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/gofiber/template/html/v2 v2.0.5
github.com/golang-jwt/jwt/v5 v5.0.0
github.com/patrickmn/go-cache v2.1.0+incompatible
golang.org/x/term v0.13.0
gopkg.in/vansante/go-ffprobe.v2 v2.1.1
gorm.io/driver/sqlite v1.5.4
gorm.io/gorm v1.25.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.3.0/go.mod h1:q750SLmJuPmVoN1blW3UFBPREJfb1KmY3vwxfr+nFDA=
golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
Expand Down

0 comments on commit 91169d2

Please sign in to comment.