Skip to content

Commit

Permalink
file.Create adds error return (goravel#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl authored Jun 3, 2023
1 parent 95a4b62 commit 6be5025
Show file tree
Hide file tree
Showing 20 changed files with 73 additions and 39 deletions.
5 changes: 4 additions & 1 deletion auth/console/policy_make_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ func (receiver *PolicyMakeCommand) Handle(ctx console.Context) error {
return errors.New("Not enough arguments (missing: name) ")
}

file.Create(receiver.getPath(name), receiver.populateStub(receiver.getStub(), name))
if err := file.Create(receiver.getPath(name), receiver.populateStub(receiver.getStub(), name)); err != nil {
return err
}

color.Greenln("Policy created successfully")

return nil
Expand Down
3 changes: 2 additions & 1 deletion config/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"

"github.com/goravel/framework/support/file"
Expand All @@ -14,7 +15,7 @@ type ApplicationTestSuite struct {
}

func TestApplicationTestSuite(t *testing.T) {
file.Create(".env", "APP_KEY=12345678901234567890123456789012")
assert.Nil(t, file.Create(".env", "APP_KEY=12345678901234567890123456789012"))

suite.Run(t, &ApplicationTestSuite{
config: NewApplication(".env"),
Expand Down
4 changes: 1 addition & 3 deletions console/console/make_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ func (receiver *MakeCommand) Handle(ctx console.Context) error {
return errors.New("Not enough arguments (missing: name) ")
}

file.Create(receiver.getPath(name), receiver.populateStub(receiver.getStub(), name))

return nil
return file.Create(receiver.getPath(name), receiver.populateStub(receiver.getStub(), name))
}

func (receiver *MakeCommand) getStub() string {
Expand Down
8 changes: 4 additions & 4 deletions database/console/migrate_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestMigrateCommand(t *testing.T) {
}

func createMysqlMigrations() {
file.Create("database/migrations/20230311160527_create_agents_table.up.sql",
_ = file.Create("database/migrations/20230311160527_create_agents_table.up.sql",
`CREATE TABLE agents (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
Expand All @@ -120,7 +120,7 @@ INSERT INTO agents (name, created_at, updated_at) VALUES ('goravel', '2023-03-11
}

func createPostgresqlMigrations() {
file.Create("database/migrations/20230311160527_create_agents_table.up.sql",
_ = file.Create("database/migrations/20230311160527_create_agents_table.up.sql",
`CREATE TABLE agents (
id SERIAL PRIMARY KEY NOT NULL,
name varchar(255) NOT NULL,
Expand All @@ -132,7 +132,7 @@ INSERT INTO agents (name, created_at, updated_at) VALUES ('goravel', '2023-03-11
}

func createSqlserverMigrations() {
file.Create("database/migrations/20230311160527_create_agents_table.up.sql",
_ = file.Create("database/migrations/20230311160527_create_agents_table.up.sql",
`CREATE TABLE agents (
id bigint NOT NULL IDENTITY(1,1),
name varchar(255) NOT NULL,
Expand All @@ -145,7 +145,7 @@ INSERT INTO agents (name, created_at, updated_at) VALUES ('goravel', '2023-03-11
}

func createSqliteMigrations() {
file.Create("database/migrations/20230311160527_create_agents_table.up.sql",
_ = file.Create("database/migrations/20230311160527_create_agents_table.up.sql",
`CREATE TABLE agents (
id integer PRIMARY KEY AUTOINCREMENT NOT NULL,
name varchar(255) NOT NULL,
Expand Down
12 changes: 9 additions & 3 deletions database/console/migrate_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,23 @@ func NewMigrateCreator(config config.Config) *MigrateCreator {
}

//Create a new migration
func (receiver MigrateCreator) Create(name string, table string, create bool) {
func (receiver MigrateCreator) Create(name string, table string, create bool) error {
// First we will get the stub file for the migration, which serves as a type
// of template for the migration. Once we have those we will populate the
// various place-holders, save the file, and run the post create event.
upStub, downStub := receiver.getStub(table, create)

//Create the up.sql file.
file.Create(receiver.getPath(name, "up"), receiver.populateStub(upStub, table))
if err := file.Create(receiver.getPath(name, "up"), receiver.populateStub(upStub, table)); err != nil {
return err
}

//Create the down.sql file.
file.Create(receiver.getPath(name, "down"), receiver.populateStub(downStub, table))
if err := file.Create(receiver.getPath(name, "down"), receiver.populateStub(downStub, table)); err != nil {
return err
}

return nil
}

//getStub Get the migration stub file.
Expand Down
2 changes: 1 addition & 1 deletion database/console/migrate_creator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestCreate(t *testing.T) {
supporttime.SetTestNow(now)

migrateCreator := NewMigrateCreator(mockConfig)
migrateCreator.Create("create_users_table", "users", true)
assert.Nil(t, migrateCreator.Create("create_users_table", "users", true))
assert.True(t, file.Exists(fmt.Sprintf("database/migrations/%s_%s.%s.sql", now.Format("20060102150405"), "create_users_table", "up")))
assert.True(t, file.Exists(fmt.Sprintf("database/migrations/%s_%s.%s.sql", now.Format("20060102150405"), "create_users_table", "down")))
assert.True(t, file.Remove("database"))
Expand Down
4 changes: 3 additions & 1 deletion database/console/migrate_make_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ func (receiver *MigrateMakeCommand) Handle(ctx console.Context) error {

//Write the migration file to disk.
migrateCreator := NewMigrateCreator(receiver.config)
migrateCreator.Create(name, table, create)
if err := migrateCreator.Create(name, table, create); err != nil {
return err
}

color.Green.Printf("Created Migration: %s\n", name)

Expand Down
5 changes: 4 additions & 1 deletion database/console/model_make_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ func (receiver *ModelMakeCommand) Handle(ctx console.Context) error {
return nil
}

file.Create(receiver.getPath(name), receiver.populateStub(receiver.getStub(), name))
if err := file.Create(receiver.getPath(name), receiver.populateStub(receiver.getStub(), name)); err != nil {
return err
}

color.Greenln("Model created successfully")

return nil
Expand Down
5 changes: 4 additions & 1 deletion database/console/observer_make_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ func (receiver *ObserverMakeCommand) Handle(ctx console.Context) error {
return nil
}

file.Create(receiver.getPath(name), receiver.populateStub(receiver.getStub(), name))
if err := file.Create(receiver.getPath(name), receiver.populateStub(receiver.getStub(), name)); err != nil {
return err
}

color.Greenln("Observer created successfully")

return nil
Expand Down
5 changes: 4 additions & 1 deletion event/console/event_make_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ func (receiver *EventMakeCommand) Handle(ctx console.Context) error {
return errors.New("Not enough arguments (missing: name) ")
}

file.Create(receiver.getPath(name), receiver.populateStub(receiver.getStub(), name))
if err := file.Create(receiver.getPath(name), receiver.populateStub(receiver.getStub(), name)); err != nil {
return err
}

color.Greenln("Event created successfully")

return nil
Expand Down
5 changes: 4 additions & 1 deletion event/console/listener_make_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ func (receiver *ListenerMakeCommand) Handle(ctx console.Context) error {
return errors.New("Not enough arguments (missing: name) ")
}

file.Create(receiver.getPath(name), receiver.populateStub(receiver.getStub(), name))
if err := file.Create(receiver.getPath(name), receiver.populateStub(receiver.getStub(), name)); err != nil {
return err
}

color.Greenln("Listener created successfully")

return nil
Expand Down
2 changes: 1 addition & 1 deletion filesystem/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestStorage(t *testing.T) {
return
}

file.Create("test.txt", "Goravel")
assert.Nil(t, file.Create("test.txt", "Goravel"))
mockConfig := initConfig()
minioPool, minioResource, err := initMinioDocker(mockConfig)
assert.Nil(t, err)
Expand Down
5 changes: 4 additions & 1 deletion http/console/controller_make_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ func (receiver *ControllerMakeCommand) Handle(ctx console.Context) error {
return errors.New("Not enough arguments (missing: name) ")
}

file.Create(receiver.getPath(name), receiver.populateStub(receiver.getStub(), name))
if err := file.Create(receiver.getPath(name), receiver.populateStub(receiver.getStub(), name)); err != nil {
return err
}

color.Greenln("Controller created successfully")

return nil
Expand Down
5 changes: 4 additions & 1 deletion http/console/middleware_make_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ func (receiver *MiddlewareMakeCommand) Handle(ctx console.Context) error {
return errors.New("Not enough arguments (missing: name) ")
}

file.Create(receiver.getPath(name), receiver.populateStub(receiver.getStub(), name))
if err := file.Create(receiver.getPath(name), receiver.populateStub(receiver.getStub(), name)); err != nil {
return err
}

color.Greenln("Middleware created successfully")

return nil
Expand Down
5 changes: 4 additions & 1 deletion http/console/request_make_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ func (receiver *RequestMakeCommand) Handle(ctx console.Context) error {
return errors.New("Not enough arguments (missing: name) ")
}

file.Create(receiver.getPath(name), receiver.populateStub(receiver.getStub(), name))
if err := file.Create(receiver.getPath(name), receiver.populateStub(receiver.getStub(), name)); err != nil {
return err
}

color.Greenln("Request created successfully")

return nil
Expand Down
5 changes: 4 additions & 1 deletion queue/console/job_make_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ func (receiver *JobMakeCommand) Handle(ctx console.Context) error {
return errors.New("Not enough arguments (missing: name) ")
}

file.Create(receiver.getPath(name), receiver.populateStub(receiver.getStub(), name))
if err := file.Create(receiver.getPath(name), receiver.populateStub(receiver.getStub(), name)); err != nil {
return err
}

color.Greenln("Job created successfully")

return nil
Expand Down
4 changes: 1 addition & 3 deletions queue/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ func (receiver *Test) Signature() string {

//Handle Execute the job.
func (receiver *Test) Handle(args ...any) error {
file.Create("test.txt", args[0].(string))

return nil
return file.Create("test.txt", args[0].(string))
}

func TestDispatchSync(t *testing.T) {
Expand Down
19 changes: 9 additions & 10 deletions support/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,24 @@ func Contain(file string, search string) bool {
return false
}

func Create(file string, content string) {
err := os.MkdirAll(path.Dir(file), os.ModePerm)
if err != nil {
panic(err.Error())
func Create(file string, content string) error {
if err := os.MkdirAll(path.Dir(file), os.ModePerm); err != nil {
return err
}

f, err := os.Create(file)
if err != nil {
return err
}
defer func() {
f.Close()
}()

if err != nil {
panic(err.Error())
if _, err = f.WriteString(content); err != nil {
return err
}

_, err = f.WriteString(content)
if err != nil {
panic(err.Error())
}
return nil
}

func Exists(file string) bool {
Expand Down
4 changes: 2 additions & 2 deletions support/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestContain(t *testing.T) {
func TestCreate(t *testing.T) {
pwd, _ := os.Getwd()
path := pwd + "/goravel/goravel.txt"
Create(path, `goravel`)
assert.Nil(t, Create(path, `goravel`))
assert.Equal(t, 1, file.GetLineNum(path))
assert.True(t, Exists(path))
assert.True(t, Remove(path))
Expand Down Expand Up @@ -52,7 +52,7 @@ func TestMimeType(t *testing.T) {
func TestRemove(t *testing.T) {
pwd, _ := os.Getwd()
path := pwd + "/goravel/goravel.txt"
Create(path, `goravel`)
assert.Nil(t, Create(path, `goravel`))

assert.True(t, Remove(path))
}
Expand Down
5 changes: 4 additions & 1 deletion validation/console/rule_make_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ func (receiver *RuleMakeCommand) Handle(ctx console.Context) error {
return errors.New("Not enough arguments (missing: name) ")
}

file.Create(receiver.getPath(name), receiver.populateStub(receiver.getStub(), name))
if err := file.Create(receiver.getPath(name), receiver.populateStub(receiver.getStub(), name)); err != nil {
return err
}

color.Greenln("Rule created successfully")

return nil
Expand Down

0 comments on commit 6be5025

Please sign in to comment.