-
Notifications
You must be signed in to change notification settings - Fork 11
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 #260 from systemli/Add-Command-to-create-User
π§βπ» Add Command to create User
- Loading branch information
Showing
13 changed files
with
82 additions
and
104 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
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
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,61 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/systemli/ticker/internal/storage" | ||
|
||
pwd "github.com/sethvargo/go-password/password" | ||
) | ||
|
||
var ( | ||
email string | ||
password string | ||
isSuperAdmin bool | ||
|
||
userCmd = &cobra.Command{ | ||
Use: "user", | ||
Short: "Manage users", | ||
Long: "Commands for managing users.", | ||
Args: cobra.ExactArgs(1), | ||
} | ||
|
||
userCreateCmd = &cobra.Command{ | ||
Use: "create", | ||
Short: "Create a new user", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
var err error | ||
if email == "" { | ||
log.Fatal("email is required") | ||
} | ||
if password == "" { | ||
password, err = pwd.Generate(24, 3, 3, false, false) | ||
if err != nil { | ||
log.WithError(err).Fatal("could not generate password") | ||
} | ||
} | ||
|
||
user, err := storage.NewUser(email, password) | ||
if err != nil { | ||
log.WithError(err).Fatal("could not create user") | ||
} | ||
user.IsSuperAdmin = isSuperAdmin | ||
|
||
if err := store.SaveUser(&user); err != nil { | ||
log.WithError(err).Fatal("could not save user") | ||
} | ||
|
||
fmt.Printf("Created user %d\n", user.ID) | ||
fmt.Printf("Password: %s\n", password) | ||
}, | ||
} | ||
) | ||
|
||
func init() { | ||
userCmd.AddCommand(userCreateCmd) | ||
|
||
userCreateCmd.Flags().StringVar(&email, "email", "", "email address of the user") | ||
userCreateCmd.Flags().StringVar(&password, "password", "", "password of the user") | ||
userCreateCmd.Flags().BoolVar(&isSuperAdmin, "super-admin", false, "make the user a super admin") | ||
} |
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,8 +4,6 @@ listen: "localhost:8080" | |
log_level: "error" | ||
# log_format sets log format for logrus (default: json) | ||
log_format: "json" | ||
# initiator is the email for the first admin user (see password in logs) | ||
initiator: "[email protected]" | ||
# configuration for the database | ||
database: | ||
type: "sqlite" # postgres, mysql, sqlite | ||
|
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 |
---|---|---|
|
@@ -7,8 +7,6 @@ listen: "localhost:8080" | |
log_level: "error" | ||
# log_format sets log format for logrus (default: json) | ||
log_format: "json" | ||
# initiator is the email for the first admin user (see password in logs) | ||
initiator: "[email protected]" | ||
# configuration for the database | ||
database: | ||
type: "sqlite" # postgres, mysql, sqlite | ||
|
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 |
---|---|---|
|
@@ -15,7 +15,6 @@ type Config struct { | |
Listen string `mapstructure:"listen"` | ||
LogLevel string `mapstructure:"log_level"` | ||
LogFormat string `mapstructure:"log_format"` | ||
Initiator string `mapstructure:"initiator"` | ||
Secret string `mapstructure:"secret"` | ||
Database Database `mapstructure:"database"` | ||
TelegramBotToken string `mapstructure:"telegram_bot_token"` | ||
|
@@ -39,7 +38,6 @@ func NewConfig() Config { | |
Listen: ":8080", | ||
LogLevel: "debug", | ||
LogFormat: "json", | ||
Initiator: "[email protected]", | ||
Secret: secret, | ||
Database: Database{Type: "sqlite", DSN: "ticker.db"}, | ||
MetricsListen: ":8181", | ||
|
@@ -63,7 +61,6 @@ func LoadConfig(path string) Config { | |
viper.SetDefault("listen", c.Listen) | ||
viper.SetDefault("log_level", c.LogLevel) | ||
viper.SetDefault("log_format", c.LogFormat) | ||
viper.SetDefault("initiator", c.Initiator) | ||
viper.SetDefault("secret", c.Secret) | ||
viper.SetDefault("database", c.Database) | ||
viper.SetDefault("metrics_listen", c.MetricsListen) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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