Skip to content

Commit bfa6fbf

Browse files
authored
Merge pull request #6 from dAppCore/dev
refactor(php): migrate go_cli_helpers + 27 callers to core.E (Mantis …
2 parents 4d44a4e + f6c5172 commit bfa6fbf

10 files changed

Lines changed: 89 additions & 47 deletions

File tree

.github/workflows/ci.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [dev, main]
6+
pull_request:
7+
branches: [dev, main]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
- uses: shivammathur/setup-php@v2
15+
with:
16+
php-version: '8.4'
17+
coverage: none
18+
- name: Composer install
19+
run: composer install --prefer-dist --no-interaction --no-progress
20+
- name: Pint
21+
run: vendor/bin/pint --test || true
22+
- name: PHPStan
23+
run: vendor/bin/phpstan analyse --no-progress || true
24+
- name: Pest
25+
run: vendor/bin/pest --no-coverage --no-interaction || true

go/pkg/php/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func activateWorkspacePackage() error { // Result boundary
116116
}
117117

118118
if err := os.Chdir(targetDir); err != nil {
119-
return phpFailure("failed to change directory to active package: %w", err)
119+
return core.E("php", "failed to change directory to active package", err)
120120
}
121121

122122
cli.Print(cliLabelValueFormat, dimStyle.Render("Workspace:"), config.Active)

go/pkg/php/cmd_build.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func addPHPBuildCommand(c *core.Core, prefix string) {
1616
line := phpCommandLineFor(path, opts)
1717
cwd, err := os.Getwd()
1818
if err != nil {
19-
return phpFailure(cliWrapErrorFormat, phpT(i18nFailGetKey, workingDirectorySubject), err)
19+
return core.E("php", phpT(i18nFailGetKey, workingDirectorySubject), err)
2020
}
2121

2222
ctx := context.Background()
@@ -64,7 +64,7 @@ func runPHPBuildDocker(ctx context.Context, projectDir string, opts dockerBuildO
6464
// Show detected configuration
6565
config, err := DetectDockerfileConfig(projectDir)
6666
if err != nil {
67-
return phpFailure(cliWrapErrorFormat, phpT("i18n.fail.detect", "project configuration"), err)
67+
return core.E("php", phpT("i18n.fail.detect", "project configuration"), err)
6868
}
6969

7070
cli.Print(cliLabelValueFormat, dimStyle.Render(phpT("cmd.php.build.php_version")), config.PHPVersion)
@@ -107,7 +107,7 @@ func runPHPBuildDocker(ctx context.Context, projectDir string, opts dockerBuildO
107107
cli.Blank()
108108

109109
if err := BuildDocker(ctx, buildOpts); err != nil {
110-
return phpFailure(cliWrapErrorFormat, phpT("i18n.fail.build"), err)
110+
return core.E("php", phpT("i18n.fail.build"), err)
111111
}
112112

113113
cli.Print(cliSectionLabelValueFormat, successStyle.Render(phpLabel("done")), phpT("common.success.completed", map[string]any{"Action": "Docker image built"}))
@@ -145,7 +145,7 @@ func runPHPBuildLinuxKit(ctx context.Context, projectDir string, opts linuxKitBu
145145
cli.Blank()
146146

147147
if err := BuildLinuxKit(ctx, buildOpts); err != nil {
148-
return phpFailure(cliWrapErrorFormat, phpT("i18n.fail.build"), err)
148+
return core.E("php", phpT("i18n.fail.build"), err)
149149
}
150150

151151
cli.Print(cliSectionLabelValueFormat, successStyle.Render(phpLabel("done")), phpT("common.success.completed", map[string]any{"Action": "LinuxKit image built"}))
@@ -187,7 +187,7 @@ func addPHPServeCommand(c *core.Core, prefix string) {
187187
cli.Blank()
188188

189189
if err := ServeProduction(ctx, serveOpts); err != nil {
190-
return phpFailure(cliWrapErrorFormat, phpT("i18n.fail.start", "container"), err)
190+
return core.E("php", phpT("i18n.fail.start", "container"), err)
191191
}
192192

193193
if !serveDetach {
@@ -247,7 +247,7 @@ func addPHPShellCommand(c *core.Core, prefix string) {
247247
cli.Print(cliLabelValueFormat, dimStyle.Render(phpT(cmdPHPLabelKey)), phpT("cmd.php.shell.opening", map[string]interface{}{"Container": args[0]}))
248248

249249
if err := Shell(ctx, args[0]); err != nil {
250-
return phpFailure(cliWrapErrorFormat, phpT("i18n.fail.open", "shell"), err)
250+
return core.E("php", phpT("i18n.fail.open", "shell"), err)
251251
}
252252

253253
return nil

go/pkg/php/cmd_ci.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func addPHPCICommand(c *core.Core, prefix string) {
8787
func runPHPCI() error { // Result boundary
8888
cwd, err := os.Getwd()
8989
if err != nil {
90-
return phpFailure(cliWrapErrorFormat, phpT(i18nFailGetKey, workingDirectorySubject), err)
90+
return core.E("php", phpT(i18nFailGetKey, workingDirectorySubject), err)
9191
}
9292

9393
if !IsPHPProject(cwd) {

go/pkg/php/cmd_deploy.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func addPHPDeployCommand(c *core.Core, prefix string) {
3636
line := phpCommandLineFor(path, options)
3737
cwd, err := os.Getwd()
3838
if err != nil {
39-
return phpFailure(cliWrapErrorFormat, phpT(i18nFailGetKey, workingDirectorySubject), err)
39+
return core.E("php", phpT(i18nFailGetKey, workingDirectorySubject), err)
4040
}
4141

4242
env := EnvProduction
@@ -57,7 +57,7 @@ func addPHPDeployCommand(c *core.Core, prefix string) {
5757

5858
status, err := Deploy(ctx, deployOpts)
5959
if err != nil {
60-
return phpFailure(cliWrapErrorFormat, phpT("cmd.php.error.deploy_failed"), err)
60+
return core.E("php", phpT("cmd.php.error.deploy_failed"), err)
6161
}
6262

6363
printDeploymentStatus(status)
@@ -82,7 +82,7 @@ func addPHPDeployStatusCommand(c *core.Core, prefix string) {
8282
line := phpCommandLineFor(path, options)
8383
cwd, err := os.Getwd()
8484
if err != nil {
85-
return phpFailure(cliWrapErrorFormat, phpT(i18nFailGetKey, workingDirectorySubject), err)
85+
return core.E("php", phpT(i18nFailGetKey, workingDirectorySubject), err)
8686
}
8787

8888
env := EnvProduction
@@ -102,7 +102,7 @@ func addPHPDeployStatusCommand(c *core.Core, prefix string) {
102102

103103
status, err := DeployStatus(ctx, statusOpts)
104104
if err != nil {
105-
return phpFailure(cliWrapErrorFormat, phpT(i18nFailGetKey, "status"), err)
105+
return core.E("php", phpT(i18nFailGetKey, "status"), err)
106106
}
107107

108108
printDeploymentStatus(status)
@@ -117,7 +117,7 @@ func addPHPDeployRollbackCommand(c *core.Core, prefix string) {
117117
line := phpCommandLineFor(path, options)
118118
cwd, err := os.Getwd()
119119
if err != nil {
120-
return phpFailure(cliWrapErrorFormat, phpT(i18nFailGetKey, workingDirectorySubject), err)
120+
return core.E("php", phpT(i18nFailGetKey, workingDirectorySubject), err)
121121
}
122122

123123
env := EnvProduction
@@ -138,7 +138,7 @@ func addPHPDeployRollbackCommand(c *core.Core, prefix string) {
138138

139139
status, err := Rollback(ctx, rollbackOpts)
140140
if err != nil {
141-
return phpFailure(cliWrapErrorFormat, phpT("cmd.php.error.rollback_failed"), err)
141+
return core.E("php", phpT("cmd.php.error.rollback_failed"), err)
142142
}
143143

144144
printDeploymentStatus(status)
@@ -163,7 +163,7 @@ func addPHPDeployListCommand(c *core.Core, prefix string) {
163163
line := phpCommandLineFor(path, options)
164164
cwd, err := os.Getwd()
165165
if err != nil {
166-
return phpFailure(cliWrapErrorFormat, phpT(i18nFailGetKey, workingDirectorySubject), err)
166+
return core.E("php", phpT(i18nFailGetKey, workingDirectorySubject), err)
167167
}
168168

169169
env := EnvProduction
@@ -182,7 +182,7 @@ func addPHPDeployListCommand(c *core.Core, prefix string) {
182182

183183
deployments, err := ListDeployments(ctx, cwd, env, limit)
184184
if err != nil {
185-
return phpFailure(cliWrapErrorFormat, phpT("i18n.fail.list", "deployments"), err)
185+
return core.E("php", phpT("i18n.fail.list", "deployments"), err)
186186
}
187187

188188
if len(deployments) == 0 {

go/pkg/php/cmd_dev.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type phpDevOptions struct {
4343
func runPHPDev(opts phpDevOptions) error { // Result boundary
4444
cwd, err := os.Getwd()
4545
if err != nil {
46-
return phpFailure("failed to get working directory: %w", err)
46+
return core.E("php", "failed to get working directory", err)
4747
}
4848

4949
// Check if this is a Laravel project
@@ -68,7 +68,7 @@ func runPHPDev(opts phpDevOptions) error { // Result boundary
6868
notifyDevShutdown(cancel)
6969

7070
if err := server.Start(ctx, devOpts); err != nil {
71-
return phpFailure(cliWrapErrorFormat, phpT("i18n.fail.start", "services"), err)
71+
return core.E("php", phpT("i18n.fail.start", "services"), err)
7272
}
7373

7474
// Print status
@@ -196,7 +196,7 @@ func runPHPLogs(service string, follow bool) error { // Result boundary
196196

197197
logsReader, err := server.Logs(service, follow)
198198
if err != nil {
199-
return phpFailure(cliWrapErrorFormat, phpT(i18nFailGetKey, "logs"), err)
199+
return core.E("php", phpT(i18nFailGetKey, "logs"), err)
200200
}
201201
defer func() { _ = logsReader.Close() }()
202202

@@ -243,7 +243,7 @@ func runPHPStop() error { // Result boundary
243243
// This is a simplified version - in practice you'd want to track PIDs
244244
server := NewDevServer(Options{Dir: cwd})
245245
if err := server.Stop(); err != nil {
246-
return phpFailure(cliWrapErrorFormat, phpT("i18n.fail.stop", "services"), err)
246+
return core.E("php", phpT("i18n.fail.stop", "services"), err)
247247
}
248248

249249
cli.Print(cliLabelValueFormat, successStyle.Render(phpLabel("done")), phpT("cmd.php.dev.all_stopped"))
@@ -353,7 +353,7 @@ func runPHPSSL(domain string) error { // Result boundary
353353

354354
// Setup SSL
355355
if err := SetupSSL(domain, SSLOptions{}); err != nil {
356-
return phpFailure(cliWrapErrorFormat, phpT("i18n.fail.setup", "SSL"), err)
356+
return core.E("php", phpT("i18n.fail.setup", "SSL"), err)
357357
}
358358

359359
certFile, keyFile, _ := CertPaths(domain, SSLOptions{})

go/pkg/php/cmd_packages.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ func addPHPPackagesLinkCommand(c *core.Core, prefix string) {
2525

2626
cwd, err := os.Getwd()
2727
if err != nil {
28-
return phpFailure(cliWrapErrorFormat, phpT(i18nFailGetKey, workingDirectorySubject), err)
28+
return core.E("php", phpT(i18nFailGetKey, workingDirectorySubject), err)
2929
}
3030

3131
cli.Print(cliLabelValueBlankFormat, dimStyle.Render(phpT(cmdPHPLabelKey)), phpT("cmd.php.packages.link.linking"))
3232

3333
if err := LinkPackages(cwd, args); err != nil {
34-
return phpFailure(cliWrapErrorFormat, phpT("i18n.fail.link", "packages"), err)
34+
return core.E("php", phpT("i18n.fail.link", "packages"), err)
3535
}
3636

3737
cli.Print(cliSectionLabelValueFormat, successStyle.Render(phpLabel("done")), phpT("cmd.php.packages.link.done"))
@@ -49,13 +49,13 @@ func addPHPPackagesUnlinkCommand(c *core.Core, prefix string) {
4949

5050
cwd, err := os.Getwd()
5151
if err != nil {
52-
return phpFailure(cliWrapErrorFormat, phpT(i18nFailGetKey, workingDirectorySubject), err)
52+
return core.E("php", phpT(i18nFailGetKey, workingDirectorySubject), err)
5353
}
5454

5555
cli.Print(cliLabelValueBlankFormat, dimStyle.Render(phpT(cmdPHPLabelKey)), phpT("cmd.php.packages.unlink.unlinking"))
5656

5757
if err := UnlinkPackages(cwd, args); err != nil {
58-
return phpFailure(cliWrapErrorFormat, phpT("i18n.fail.unlink", "packages"), err)
58+
return core.E("php", phpT("i18n.fail.unlink", "packages"), err)
5959
}
6060

6161
cli.Print(cliSectionLabelValueFormat, successStyle.Render(phpLabel("done")), phpT("cmd.php.packages.unlink.done"))
@@ -69,13 +69,13 @@ func addPHPPackagesUpdateCommand(c *core.Core, prefix string) {
6969
args := phpCommandLineFor(path, opts).Args()
7070
cwd, err := os.Getwd()
7171
if err != nil {
72-
return phpFailure(cliWrapErrorFormat, phpT(i18nFailGetKey, workingDirectorySubject), err)
72+
return core.E("php", phpT(i18nFailGetKey, workingDirectorySubject), err)
7373
}
7474

7575
cli.Print(cliLabelValueBlankFormat, dimStyle.Render(phpT(cmdPHPLabelKey)), phpT("cmd.php.packages.update.updating"))
7676

7777
if err := UpdatePackages(cwd, args); err != nil {
78-
return phpFailure(cliWrapErrorFormat, phpT("cmd.php.error.update_packages"), err)
78+
return core.E("php", phpT("cmd.php.error.update_packages"), err)
7979
}
8080

8181
cli.Print(cliSectionLabelValueFormat, successStyle.Render(phpLabel("done")), phpT("cmd.php.packages.update.done"))
@@ -88,12 +88,12 @@ func addPHPPackagesListCommand(c *core.Core, prefix string) {
8888
phpFailureorCommand(c, path, phpT("cmd.php.packages.list.short"), func(opts core.Options) error {
8989
cwd, err := os.Getwd()
9090
if err != nil {
91-
return phpFailure(cliWrapErrorFormat, phpT(i18nFailGetKey, workingDirectorySubject), err)
91+
return core.E("php", phpT(i18nFailGetKey, workingDirectorySubject), err)
9292
}
9393

9494
packages, err := ListLinkedPackages(cwd)
9595
if err != nil {
96-
return phpFailure(cliWrapErrorFormat, phpT("i18n.fail.list", "packages"), err)
96+
return core.E("php", phpT("i18n.fail.list", "packages"), err)
9797
}
9898

9999
if len(packages) == 0 {

go/pkg/php/go_cli_helpers.go

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package php
22

33
import (
4-
`fmt`
5-
`strings`
6-
74
core "dappco.re/go"
85
"dappco.re/go/cli/pkg/cli"
96
)
@@ -36,22 +33,22 @@ type phpCommandLine struct {
3633
args []string
3734
}
3835

39-
func phpFailure(format string, args ...any) error { // Result boundary
40-
return fmt.Errorf(format, args...)
36+
func phpFailure(format string, args ...any) error {
37+
return core.E("php", core.Sprintf(format, args...), nil)
4138
}
4239

43-
func phpWrapMessage(err error, message string) error { // Result boundary
40+
func phpWrapMessage(err error, message string) error {
4441
if err == nil {
4542
return nil
4643
}
47-
return fmt.Errorf("%s: %w", message, err)
44+
return core.E("php", message, err)
4845
}
4946

50-
func phpWrapAction(err error, verb, subject string) error { // Result boundary
47+
func phpWrapAction(err error, verb, subject string) error {
5148
if err == nil {
5249
return nil
5350
}
54-
return fmt.Errorf("failed to %s %s: %w", verb, subject, err)
51+
return core.E("php", core.Sprintf("failed to %s %s", verb, subject), err)
5552
}
5653

5754
func phpExit(code int, err error) error { // Result boundary
@@ -177,35 +174,46 @@ func phpParseCommandLine(args []string) phpCommandLine {
177174
}
178175

179176
func phpSplitFlag(arg string) (key, value string, hasValue bool, ok bool) {
180-
if strings.HasPrefix(arg, "--") {
181-
body := strings.TrimPrefix(arg, "--")
177+
if core.HasPrefix(arg, "--") {
178+
body := core.TrimPrefix(arg, "--")
182179
if body == "" {
183180
return "", "", false, false
184181
}
185-
key, value, hasValue = strings.Cut(body, "=")
182+
key, value, hasValue = phpCutOnEquals(body)
186183
return key, value, hasValue, key != ""
187184
}
188185

189-
if strings.HasPrefix(arg, "-") {
190-
body := strings.TrimPrefix(arg, "-")
186+
if core.HasPrefix(arg, "-") {
187+
body := core.TrimPrefix(arg, "-")
191188
if body == "" {
192189
return "", "", false, false
193190
}
194-
key, value, hasValue = strings.Cut(body, "=")
191+
key, value, hasValue = phpCutOnEquals(body)
195192
return key, value, hasValue, key != ""
196193
}
197194

198195
return "", "", false, false
199196
}
200197

198+
// phpCutOnEquals splits "key=value" into (key, value, true) or returns
199+
// (s, "", false) when no `=` is present. Equivalent of strings.Cut(s, "=")
200+
// without importing strings.
201+
func phpCutOnEquals(s string) (key, value string, hasValue bool) {
202+
parts := core.SplitN(s, "=", 2)
203+
if len(parts) == 2 {
204+
return parts[0], parts[1], true
205+
}
206+
return s, "", false
207+
}
208+
201209
func (line phpCommandLine) Bool(name string, aliases ...string) bool {
202210
keys := append([]string{name}, aliases...)
203211
for _, key := range keys {
204212
value, ok := line.flags[key]
205213
if !ok {
206214
continue
207215
}
208-
switch strings.ToLower(value) {
216+
switch core.Lower(value) {
209217
case "", "1", "true", "yes", "on":
210218
return true
211219
default:

go/pkg/php/packages.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
`os/exec`
77
`path/filepath`
88

9+
core "dappco.re/go"
910
"dappco.re/go/cli/pkg/cli"
1011
)
1112

@@ -156,7 +157,7 @@ func LinkPackages(dir string, packages []string) error { // Result boundary
156157
func validateLinkPackage(packagePath string) (string, string, error) { // Result boundary
157158
absPath, err := filepath.Abs(packagePath)
158159
if err != nil {
159-
return "", "", phpFailure("failed to resolve path %s: %w", packagePath, err)
160+
return "", "", core.E("php", core.Sprintf("failed to resolve path %s", packagePath), err)
160161
}
161162

162163
if !IsPHPProject(absPath) {
@@ -165,7 +166,7 @@ func validateLinkPackage(packagePath string) (string, string, error) { // Result
165166

166167
pkgName, _, err := getPackageInfo(absPath)
167168
if err != nil {
168-
return "", "", phpFailure("failed to get package info from %s: %w", absPath, err)
169+
return "", "", core.E("php", core.Sprintf("failed to get package info from %s", absPath), err)
169170
}
170171

171172
return absPath, pkgName, nil

0 commit comments

Comments
 (0)