@@ -2,14 +2,15 @@ package client
2
2
3
3
import (
4
4
"fmt"
5
+ "net/http"
5
6
"path/filepath"
6
7
"strings"
7
8
8
9
_ "embed"
9
10
10
- "github.com/code-game-project/codegame-cli-go/util "
11
+ "github.com/code-game-project/codegame-cli-go/new "
11
12
"github.com/code-game-project/codegame-cli/cli"
12
- "github.com/code-game-project/codegame-cli/external "
13
+ "github.com/code-game-project/codegame-cli/util "
13
14
)
14
15
15
16
//go:embed templates/main.go.tmpl
@@ -30,11 +31,8 @@ func CreateNewClient(projectName, gameName, serverURL, libraryVersion string, su
30
31
return err
31
32
}
32
33
33
- out , err := external . ExecuteHidden ( "go" , "mod" , "init" , module )
34
+ _ , err = util . Execute ( true , "go" , "mod" , "init" , module )
34
35
if err != nil {
35
- if out != "" {
36
- cli .Error (out )
37
- }
38
36
return err
39
37
}
40
38
@@ -52,11 +50,8 @@ func CreateNewClient(projectName, gameName, serverURL, libraryVersion string, su
52
50
}
53
51
54
52
cli .Begin ("Installing correct go-client version..." )
55
- out , err = external . ExecuteHidden ( "go" , "get" , fmt .Sprintf ("%s@%s" , libraryURL , libraryTag ))
53
+ _ , err = util . Execute ( true , "go" , "get" , fmt .Sprintf ("%s@%s" , libraryURL , libraryTag ))
56
54
if err != nil {
57
- if out != "" {
58
- cli .Error (out )
59
- }
60
55
return err
61
56
}
62
57
cli .Finish ()
@@ -70,23 +65,20 @@ func CreateNewClient(projectName, gameName, serverURL, libraryVersion string, su
70
65
71
66
cli .Begin ("Installing dependencies..." )
72
67
73
- out , err = external . ExecuteHidden ( "go" , "mod" , "tidy" )
68
+ _ , err = util . Execute ( true , "go" , "mod" , "tidy" )
74
69
if err != nil {
75
- if out != "" {
76
- cli .Error (out )
77
- }
78
70
return err
79
71
}
80
72
81
73
cli .Finish ()
82
74
83
75
cli .Begin ("Organizing imports..." )
84
76
85
- if ! external .IsInstalled ("goimports" ) {
77
+ if ! util .IsInstalled ("goimports" ) {
86
78
cli .Warn ("Failed to organize import statements: 'goimports' is not installed!" )
87
79
return nil
88
80
}
89
- external . ExecuteHidden ( "goimports" , "-w" , "main.go" )
81
+ util . Execute ( true , "goimports" , "-w" , "main.go" )
90
82
91
83
cli .Finish ()
92
84
@@ -98,7 +90,7 @@ func createGoClientTemplate(projectName, modulePath, gameName, serverURL, librar
98
90
return execGoClientMainTemplate (projectName , serverURL , libraryURL )
99
91
}
100
92
101
- cgeVersion , err := external .GetCGEVersion (util . BaseURL (serverURL , util . IsSSL (serverURL )))
93
+ cgeVersion , err := util .GetCGEVersion (baseURL (serverURL , isSSL (serverURL )))
102
94
if err != nil {
103
95
return err
104
96
}
@@ -109,15 +101,15 @@ func createGoClientTemplate(projectName, modulePath, gameName, serverURL, librar
109
101
func getGoClientLibraryURL (clientVersion string ) (url string , tag string , err error ) {
110
102
if clientVersion == "latest" {
111
103
var err error
112
- clientVersion , err = external .LatestGithubTag ("code-game-project" , "go-client" )
104
+ clientVersion , err = util .LatestGithubTag ("code-game-project" , "go-client" )
113
105
if err != nil {
114
106
return "" , "" , err
115
107
}
116
108
clientVersion = strings .TrimPrefix (strings .Join (strings .Split (clientVersion , "." )[:2 ], "." ), "v" )
117
109
}
118
110
119
111
majorVersion := strings .Split (clientVersion , "." )[0 ]
120
- tag , err = external .GithubTagFromVersion ("code-game-project" , "go-client" , clientVersion )
112
+ tag , err = util .GithubTagFromVersion ("code-game-project" , "go-client" , clientVersion )
121
113
if err != nil {
122
114
return "" , "" , err
123
115
}
@@ -135,7 +127,7 @@ func execGoClientMainTemplate(projectName, serverURL, libraryURL string) error {
135
127
LibraryURL string
136
128
}
137
129
138
- return util .ExecTemplate (goClientMainTemplate , "main.go" , data {
130
+ return new .ExecTemplate (goClientMainTemplate , "main.go" , data {
139
131
URL : serverURL ,
140
132
LibraryURL : libraryURL ,
141
133
})
@@ -146,7 +138,7 @@ func execGoClientWrappersTemplate(projectName, modulePath, gameName, serverURL,
146
138
147
139
gameDir := strings .ReplaceAll (strings .ReplaceAll (gameName , "-" , "" ), "_" , "" )
148
140
149
- eventNames , err := external .GetEventNames (util . BaseURL (serverURL , util . IsSSL (serverURL )), cgeVersion )
141
+ eventNames , err := util .GetEventNames (baseURL (serverURL , isSSL (serverURL )), cgeVersion )
150
142
if err != nil {
151
143
return err
152
144
}
@@ -181,20 +173,37 @@ func execGoClientWrappersTemplate(projectName, modulePath, gameName, serverURL,
181
173
Events : events ,
182
174
}
183
175
184
- err = util .ExecTemplate (goClientWrapperMainTemplate , filepath .Join ("main.go" ), data )
176
+ err = new .ExecTemplate (goClientWrapperMainTemplate , filepath .Join ("main.go" ), data )
185
177
if err != nil {
186
178
return err
187
179
}
188
180
189
- err = util .ExecTemplate (goClientWrapperGameTemplate , filepath .Join (gameDir , "game.go" ), data )
181
+ err = new .ExecTemplate (goClientWrapperGameTemplate , filepath .Join (gameDir , "game.go" ), data )
190
182
if err != nil {
191
183
return err
192
184
}
193
185
194
- err = util .ExecTemplate (goClientWrapperEventsTemplate , filepath .Join (gameDir , "events.go" ), data )
186
+ err = new .ExecTemplate (goClientWrapperEventsTemplate , filepath .Join (gameDir , "events.go" ), data )
195
187
if err != nil {
196
188
return err
197
189
}
198
190
199
191
return nil
200
192
}
193
+
194
+ func baseURL (domain string , ssl bool ) string {
195
+ if ssl {
196
+ return "https://" + domain
197
+ } else {
198
+ return "http://" + domain
199
+ }
200
+ }
201
+
202
+ func isSSL (domain string ) bool {
203
+ res , err := http .Get ("https://" + domain )
204
+ if err == nil {
205
+ res .Body .Close ()
206
+ return true
207
+ }
208
+ return false
209
+ }
0 commit comments