Fix for FieldCheck appearing even for the serve command
- commit
- 9572ee4d924acf2c196e8fe3c0268171a883d493
- author
- bnema · · 2023-10-27 07:38 UTC
- parent
- d6ab22b
- signature
- unsigned
4 files · +40 -28
M internal/cli/cmd/ping.go +8 -1
| @@ -2,6 +2,7 @@ package cmd | |||
| 2 | 2 | ||
| 3 | 3 | import ( | |
| 4 | 4 | "fmt" | |
| 5 | + | "os" | |
| 5 | 6 | ||
| 6 | 7 | "github.com/bnema/gordon/internal/cli" | |
| 7 | 8 | "github.com/bnema/gordon/internal/cli/handler" | |
| @@ -11,10 +12,16 @@ import ( | |||
| 11 | 12 | ) | |
| 12 | 13 | ||
| 13 | 14 | func NewPingCommand(a *cli.App) *cobra.Command { | |
| 14 | - | handler.FieldCheck(a) | |
| 15 | + | // handler.FieldCheck(a) | |
| 15 | 16 | return &cobra.Command{ | |
| 16 | 17 | Use: "ping", | |
| 17 | 18 | Short: "Send a ping request to the backend", | |
| 19 | + | PreRun: func(cmd *cobra.Command, args []string) { | |
| 20 | + | if err := handler.FieldCheck(a); err != nil { | |
| 21 | + | fmt.Println("Field check failed:", err) | |
| 22 | + | os.Exit(1) | |
| 23 | + | } | |
| 24 | + | }, | |
| 18 | 25 | Run: func(cmd *cobra.Command, args []string) { | |
| 19 | 26 | data := map[string]interface{}{"message": "ping"} | |
| 20 | 27 | payload, err := common.NewPingPayload(data) | |
| @@ -2,6 +2,7 @@ package cmd | |||||
| 2 | 2 | ||||
| 3 | import ( | 3 | import ( | ||
| 4 | "fmt" | 4 | "fmt" | ||
| 5 | + | "os" | |||
| 5 | 6 | ||||
| 6 | "github.com/bnema/gordon/internal/cli" | 7 | "github.com/bnema/gordon/internal/cli" | ||
| 7 | "github.com/bnema/gordon/internal/cli/handler" | 8 | "github.com/bnema/gordon/internal/cli/handler" | ||
| @@ -11,10 +12,16 @@ import ( | |||||
| 11 | ) | 12 | ) | ||
| 12 | 13 | ||||
| 13 | func NewPingCommand(a *cli.App) *cobra.Command { | 14 | func NewPingCommand(a *cli.App) *cobra.Command { | ||
| 14 | - | handler.FieldCheck(a) | 15 | + | // handler.FieldCheck(a) |
| 15 | return &cobra.Command{ | 16 | return &cobra.Command{ | ||
| 16 | Use: "ping", | 17 | Use: "ping", | ||
| 17 | Short: "Send a ping request to the backend", | 18 | Short: "Send a ping request to the backend", | ||
| 19 | + | PreRun: func(cmd *cobra.Command, args []string) { | |||
| 20 | + | if err := handler.FieldCheck(a); err != nil { | |||
| 21 | + | fmt.Println("Field check failed:", err) | |||
| 22 | + | os.Exit(1) | |||
| 23 | + | } | |||
| 24 | + | }, | |||
| 18 | Run: func(cmd *cobra.Command, args []string) { | 25 | Run: func(cmd *cobra.Command, args []string) { | ||
| 19 | data := map[string]interface{}{"message": "ping"} | 26 | data := map[string]interface{}{"message": "ping"} | ||
| 20 | payload, err := common.NewPingPayload(data) | 27 | payload, err := common.NewPingPayload(data) | ||
M internal/cli/cmd/push.go +11 -4
| @@ -2,6 +2,7 @@ package cmd | |||
| 2 | 2 | ||
| 3 | 3 | import ( | |
| 4 | 4 | "fmt" | |
| 5 | + | "os" | |
| 5 | 6 | ||
| 6 | 7 | "github.com/bnema/gordon/internal/cli" | |
| 7 | 8 | "github.com/bnema/gordon/internal/cli/handler" | |
| @@ -28,12 +29,18 @@ func NewPushCommand(a *cli.App) *cobra.Command { | |||
| 28 | 29 | var port string | |
| 29 | 30 | var targetDomain string | |
| 30 | 31 | ||
| 31 | - | handler.FieldCheck(a) | |
| 32 | + | // handler.FieldCheck(a) | |
| 32 | 33 | ||
| 33 | 34 | pushCmd := &cobra.Command{ | |
| 34 | 35 | Use: "push [image:tag]", | |
| 35 | 36 | Short: "Push an image to the server, if no tag is specified, latest is used", | |
| 36 | - | Args: cobra.ExactArgs(1), // Require exactly one positional argument for image:tag | |
| 37 | + | Args: cobra.ExactArgs(1), | |
| 38 | + | PreRun: func(cmd *cobra.Command, args []string) { | |
| 39 | + | if err := handler.FieldCheck(a); err != nil { | |
| 40 | + | fmt.Println("Field check failed:", err) | |
| 41 | + | os.Exit(1) | |
| 42 | + | } | |
| 43 | + | }, | |
| 37 | 44 | Run: func(cmd *cobra.Command, args []string) { | |
| 38 | 45 | imageName := args[0] | |
| 39 | 46 | fmt.Println("Exporting image:", imageName) | |
| @@ -45,9 +52,9 @@ func NewPushCommand(a *cli.App) *cobra.Command { | |||
| 45 | 52 | } | |
| 46 | 53 | fmt.Println("Image exported successfully") | |
| 47 | 54 | // Create a RequestPayload and populate it | |
| 48 | - | payload := common.RequestPayload{ | |
| 55 | + | payload := common.RequestPayload{ // <- TODO : Load RequestPayLoad from cli.App | |
| 49 | 56 | Type: "push", | |
| 50 | - | Payload: common.PushPayload{ | |
| 57 | + | Payload: common.PushPayload{ // <- Same | |
| 51 | 58 | Ports: port, | |
| 52 | 59 | TargetDomain: targetDomain, | |
| 53 | 60 | ImageName: imageName, | |
| @@ -2,6 +2,7 @@ package cmd | |||||
| 2 | 2 | ||||
| 3 | import ( | 3 | import ( | ||
| 4 | "fmt" | 4 | "fmt" | ||
| 5 | + | "os" | |||
| 5 | 6 | ||||
| 6 | "github.com/bnema/gordon/internal/cli" | 7 | "github.com/bnema/gordon/internal/cli" | ||
| 7 | "github.com/bnema/gordon/internal/cli/handler" | 8 | "github.com/bnema/gordon/internal/cli/handler" | ||
| @@ -28,12 +29,18 @@ func NewPushCommand(a *cli.App) *cobra.Command { | |||||
| 28 | var port string | 29 | var port string | ||
| 29 | var targetDomain string | 30 | var targetDomain string | ||
| 30 | 31 | ||||
| 31 | - | handler.FieldCheck(a) | 32 | + | // handler.FieldCheck(a) |
| 32 | 33 | ||||
| 33 | pushCmd := &cobra.Command{ | 34 | pushCmd := &cobra.Command{ | ||
| 34 | Use: "push [image:tag]", | 35 | Use: "push [image:tag]", | ||
| 35 | Short: "Push an image to the server, if no tag is specified, latest is used", | 36 | Short: "Push an image to the server, if no tag is specified, latest is used", | ||
| 36 | - | Args: cobra.ExactArgs(1), // Require exactly one positional argument for image:tag | 37 | + | Args: cobra.ExactArgs(1), |
| 38 | + | PreRun: func(cmd *cobra.Command, args []string) { | |||
| 39 | + | if err := handler.FieldCheck(a); err != nil { | |||
| 40 | + | fmt.Println("Field check failed:", err) | |||
| 41 | + | os.Exit(1) | |||
| 42 | + | } | |||
| 43 | + | }, | |||
| 37 | Run: func(cmd *cobra.Command, args []string) { | 44 | Run: func(cmd *cobra.Command, args []string) { | ||
| 38 | imageName := args[0] | 45 | imageName := args[0] | ||
| 39 | fmt.Println("Exporting image:", imageName) | 46 | fmt.Println("Exporting image:", imageName) | ||
| @@ -45,9 +52,9 @@ func NewPushCommand(a *cli.App) *cobra.Command { | |||||
| 45 | } | 52 | } | ||
| 46 | fmt.Println("Image exported successfully") | 53 | fmt.Println("Image exported successfully") | ||
| 47 | // Create a RequestPayload and populate it | 54 | // Create a RequestPayload and populate it | ||
| 48 | - | payload := common.RequestPayload{ | 55 | + | payload := common.RequestPayload{ // <- TODO : Load RequestPayLoad from cli.App |
| 49 | Type: "push", | 56 | Type: "push", | ||
| 50 | - | Payload: common.PushPayload{ | 57 | + | Payload: common.PushPayload{ // <- Same |
| 51 | Ports: port, | 58 | Ports: port, | ||
| 52 | TargetDomain: targetDomain, | 59 | TargetDomain: targetDomain, | ||
| 53 | ImageName: imageName, | 60 | ImageName: imageName, | ||
M internal/cli/handler/fieldcheck.go +21 -21
| @@ -12,38 +12,38 @@ import ( | |||
| 12 | 12 | // check in the config.yml if BackendURL and Token are set | |
| 13 | 13 | // if not, prompt the user to enter them | |
| 14 | 14 | ||
| 15 | - | func checkIfBackendURLisSet(a *cli.App) (string, error) { | |
| 16 | - | if a.Config.Http.BackendURL == "" { | |
| 17 | - | fmt.Println("BackendURL is not set in config.yml") | |
| 18 | - | a.Config.Http.BackendURL = common.ReadUserInput("Enter the backend URL (e.g. https://gordon.mydomain.com):") | |
| 19 | - | return a.Config.Http.BackendURL, nil | |
| 15 | + | // Helper function to check, prompt and set a value | |
| 16 | + | func checkAndSetField(value *string, promptMessage, errorMessage string) (bool, error) { | |
| 17 | + | wasEmpty := false | |
| 18 | + | if *value == "" { | |
| 19 | + | wasEmpty = true | |
| 20 | + | fmt.Println(errorMessage) | |
| 21 | + | *value = common.ReadUserInput(promptMessage) | |
| 20 | 22 | } | |
| 21 | - | return a.Config.Http.BackendURL, nil | |
| 22 | - | } | |
| 23 | - | ||
| 24 | - | func checkIfTokenIsSet(a *cli.App) (string, error) { | |
| 25 | - | if a.Config.General.Token == "" { | |
| 26 | - | fmt.Println("Token is not set in config.yml") | |
| 27 | - | a.Config.General.Token = common.ReadUserInput("Enter the token (check your backend config.yml):") | |
| 28 | - | return a.Config.General.Token, nil | |
| 29 | - | } | |
| 30 | - | return a.Config.General.Token, nil | |
| 23 | + | return wasEmpty, nil | |
| 31 | 24 | } | |
| 32 | 25 | ||
| 33 | 26 | func FieldCheck(a *cli.App) error { | |
| 34 | - | _, err := checkIfBackendURLisSet(a) | |
| 27 | + | wasBackendURLEmpty, err := checkAndSetField(&a.Config.Http.BackendURL, | |
| 28 | + | "Enter the backend URL (e.g. https://gordon.mydomain.com):", | |
| 29 | + | "BackendURL is not set in config.yml") | |
| 35 | 30 | if err != nil { | |
| 36 | 31 | return err | |
| 37 | 32 | } | |
| 38 | - | _, err = checkIfTokenIsSet(a) | |
| 33 | + | ||
| 34 | + | wasTokenEmpty, err := checkAndSetField(&a.Config.General.Token, | |
| 35 | + | "Enter the token (check your backend config.yml):", | |
| 36 | + | "Token is not set in config.yml") | |
| 39 | 37 | if err != nil { | |
| 40 | 38 | return err | |
| 41 | 39 | } | |
| 42 | 40 | ||
| 43 | - | // save config | |
| 44 | - | err = a.Config.SaveConfig() | |
| 45 | - | if err != nil { | |
| 46 | - | return err | |
| 41 | + | // Save config if one of the fields was empty | |
| 42 | + | if wasBackendURLEmpty || wasTokenEmpty { | |
| 43 | + | err = a.Config.SaveConfig() | |
| 44 | + | if err != nil { | |
| 45 | + | return err | |
| 46 | + | } | |
| 47 | 47 | } | |
| 48 | 48 | ||
| 49 | 49 | return nil | |
| @@ -12,38 +12,38 @@ import ( | |||||
| 12 | // check in the config.yml if BackendURL and Token are set | 12 | // check in the config.yml if BackendURL and Token are set | ||
| 13 | // if not, prompt the user to enter them | 13 | // if not, prompt the user to enter them | ||
| 14 | 14 | ||||
| 15 | - | func checkIfBackendURLisSet(a *cli.App) (string, error) { | 15 | + | // Helper function to check, prompt and set a value |
| 16 | - | if a.Config.Http.BackendURL == "" { | 16 | + | func checkAndSetField(value *string, promptMessage, errorMessage string) (bool, error) { |
| 17 | - | fmt.Println("BackendURL is not set in config.yml") | 17 | + | wasEmpty := false |
| 18 | - | a.Config.Http.BackendURL = common.ReadUserInput("Enter the backend URL (e.g. https://gordon.mydomain.com):") | 18 | + | if *value == "" { |
| 19 | - | return a.Config.Http.BackendURL, nil | 19 | + | wasEmpty = true |
| 20 | + | fmt.Println(errorMessage) | |||
| 21 | + | *value = common.ReadUserInput(promptMessage) | |||
| 20 | } | 22 | } | ||
| 21 | - | return a.Config.Http.BackendURL, nil | 23 | + | return wasEmpty, nil |
| 22 | - | } | |||
| 23 | - | ||||
| 24 | - | func checkIfTokenIsSet(a *cli.App) (string, error) { | |||
| 25 | - | if a.Config.General.Token == "" { | |||
| 26 | - | fmt.Println("Token is not set in config.yml") | |||
| 27 | - | a.Config.General.Token = common.ReadUserInput("Enter the token (check your backend config.yml):") | |||
| 28 | - | return a.Config.General.Token, nil | |||
| 29 | - | } | |||
| 30 | - | return a.Config.General.Token, nil | |||
| 31 | } | 24 | } | ||
| 32 | 25 | ||||
| 33 | func FieldCheck(a *cli.App) error { | 26 | func FieldCheck(a *cli.App) error { | ||
| 34 | - | _, err := checkIfBackendURLisSet(a) | 27 | + | wasBackendURLEmpty, err := checkAndSetField(&a.Config.Http.BackendURL, |
| 28 | + | "Enter the backend URL (e.g. https://gordon.mydomain.com):", | |||
| 29 | + | "BackendURL is not set in config.yml") | |||
| 35 | if err != nil { | 30 | if err != nil { | ||
| 36 | return err | 31 | return err | ||
| 37 | } | 32 | } | ||
| 38 | - | _, err = checkIfTokenIsSet(a) | 33 | + | |
| 34 | + | wasTokenEmpty, err := checkAndSetField(&a.Config.General.Token, | |||
| 35 | + | "Enter the token (check your backend config.yml):", | |||
| 36 | + | "Token is not set in config.yml") | |||
| 39 | if err != nil { | 37 | if err != nil { | ||
| 40 | return err | 38 | return err | ||
| 41 | } | 39 | } | ||
| 42 | 40 | ||||
| 43 | - | // save config | 41 | + | // Save config if one of the fields was empty |
| 44 | - | err = a.Config.SaveConfig() | 42 | + | if wasBackendURLEmpty || wasTokenEmpty { |
| 45 | - | if err != nil { | 43 | + | err = a.Config.SaveConfig() |
| 46 | - | return err | 44 | + | if err != nil { |
| 45 | + | return err | |||
| 46 | + | } | |||
| 47 | } | 47 | } | ||
| 48 | 48 | ||||
| 49 | return nil | 49 | return nil | ||
M internal/db/queries/user.go +0 -2
| @@ -33,8 +33,6 @@ func CreateUser(a *server.App, accessToken string, browserInfo string, userInfo | |||
| 33 | 33 | return err | |
| 34 | 34 | } | |
| 35 | 35 | ||
| 36 | - | fmt.Println("user", user) | |
| 37 | - | ||
| 38 | 36 | // update the global state | |
| 39 | 37 | a.DBTables.User.ID = user.ID | |
| 40 | 38 | a.DBTables.User.Name = user.Name | |
| @@ -33,8 +33,6 @@ func CreateUser(a *server.App, accessToken string, browserInfo string, userInfo | |||||
| 33 | return err | 33 | return err | ||
| 34 | } | 34 | } | ||
| 35 | 35 | ||||
| 36 | - | fmt.Println("user", user) | |||
| 37 | - | ||||
| 38 | // update the global state | 36 | // update the global state | ||
| 39 | a.DBTables.User.ID = user.ID | 37 | a.DBTables.User.ID = user.ID | ||
| 40 | a.DBTables.User.Name = user.Name | 38 | a.DBTables.User.Name = user.Name | ||