detached 9572ee4

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
22
33 import (
44 "fmt"
5+ "os"
56
67 "github.com/bnema/gordon/internal/cli"
78 "github.com/bnema/gordon/internal/cli/handler"
@@ -11,10 +12,16 @@ import (
1112 )
1213
1314 func NewPingCommand(a *cli.App) *cobra.Command {
14- handler.FieldCheck(a)
15+ // handler.FieldCheck(a)
1516 return &cobra.Command{
1617 Use: "ping",
1718 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+ },
1825 Run: func(cmd *cobra.Command, args []string) {
1926 data := map[string]interface{}{"message": "ping"}
2027 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
22
33 import (
44 "fmt"
5+ "os"
56
67 "github.com/bnema/gordon/internal/cli"
78 "github.com/bnema/gordon/internal/cli/handler"
@@ -28,12 +29,18 @@ func NewPushCommand(a *cli.App) *cobra.Command {
2829 var port string
2930 var targetDomain string
3031
31- handler.FieldCheck(a)
32+ // handler.FieldCheck(a)
3233
3334 pushCmd := &cobra.Command{
3435 Use: "push [image:tag]",
3536 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+ },
3744 Run: func(cmd *cobra.Command, args []string) {
3845 imageName := args[0]
3946 fmt.Println("Exporting image:", imageName)
@@ -45,9 +52,9 @@ func NewPushCommand(a *cli.App) *cobra.Command {
4552 }
4653 fmt.Println("Image exported successfully")
4754 // Create a RequestPayload and populate it
48- payload := common.RequestPayload{
55+ payload := common.RequestPayload{ // <- TODO : Load RequestPayLoad from cli.App
4956 Type: "push",
50- Payload: common.PushPayload{
57+ Payload: common.PushPayload{ // <- Same
5158 Ports: port,
5259 TargetDomain: targetDomain,
5360 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 string29 var port string
29 var targetDomain string30 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:tag37+ 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 it54 // 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 (
1212 // check in the config.yml if BackendURL and Token are set
1313 // if not, prompt the user to enter them
1414
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)
2022 }
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
3124 }
3225
3326 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")
3530 if err != nil {
3631 return err
3732 }
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")
3937 if err != nil {
4038 return err
4139 }
4240
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+ }
4747 }
4848
4949 return nil
@@ -12,38 +12,38 @@ import (
12 // check in the config.yml if BackendURL and Token are set12 // check in the config.yml if BackendURL and Token are set
13 // if not, prompt the user to enter them13 // 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, nil19+ wasEmpty = true
20+ fmt.Println(errorMessage)
21+ *value = common.ReadUserInput(promptMessage)
20 }22 }
21- return a.Config.Http.BackendURL, nil23+ 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 err31 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 err38 return err
41 }39 }
42 40
43- // save config41+ // 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 err44+ if err != nil {
45+ return err
46+ }
47 }47 }
48 48
49 return nil49 return nil
M internal/db/queries/user.go +0 -2
@@ -33,8 +33,6 @@ func CreateUser(a *server.App, accessToken string, browserInfo string, userInfo
3333 return err
3434 }
3535
36- fmt.Println("user", user)
37-
3836 // update the global state
3937 a.DBTables.User.ID = user.ID
4038 a.DBTables.User.Name = user.Name
@@ -33,8 +33,6 @@ func CreateUser(a *server.App, accessToken string, browserInfo string, userInfo
33 return err33 return err
34 }34 }
35 35
36- fmt.Println("user", user)
37-
38 // update the global state36 // update the global state
39 a.DBTables.User.ID = user.ID37 a.DBTables.User.ID = user.ID
40 a.DBTables.User.Name = user.Name38 a.DBTables.User.Name = user.Name

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close