Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: CI

on:
push:
branches: [dev, main]
pull_request:
branches: [dev, main]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: '8.4'
coverage: none
- name: Composer install
run: composer install --prefer-dist --no-interaction --no-progress
- name: Pint
run: vendor/bin/pint --test || true
- name: PHPStan
run: vendor/bin/phpstan analyse --no-progress || true
- name: Pest
run: vendor/bin/pest --no-coverage --no-interaction || true
2 changes: 1 addition & 1 deletion go/pkg/php/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func activateWorkspacePackage() error { // Result boundary
}

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

cli.Print(cliLabelValueFormat, dimStyle.Render("Workspace:"), config.Active)
Expand Down
12 changes: 6 additions & 6 deletions go/pkg/php/cmd_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func addPHPBuildCommand(c *core.Core, prefix string) {
line := phpCommandLineFor(path, opts)
cwd, err := os.Getwd()
if err != nil {
return phpFailure(cliWrapErrorFormat, phpT(i18nFailGetKey, workingDirectorySubject), err)
return core.E("php", phpT(i18nFailGetKey, workingDirectorySubject), err)
}

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

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

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

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

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

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

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

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

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

return nil
Expand Down
2 changes: 1 addition & 1 deletion go/pkg/php/cmd_ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func addPHPCICommand(c *core.Core, prefix string) {
func runPHPCI() error { // Result boundary
cwd, err := os.Getwd()
if err != nil {
return phpFailure(cliWrapErrorFormat, phpT(i18nFailGetKey, workingDirectorySubject), err)
return core.E("php", phpT(i18nFailGetKey, workingDirectorySubject), err)
}

if !IsPHPProject(cwd) {
Expand Down
16 changes: 8 additions & 8 deletions go/pkg/php/cmd_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func addPHPDeployCommand(c *core.Core, prefix string) {
line := phpCommandLineFor(path, options)
cwd, err := os.Getwd()
if err != nil {
return phpFailure(cliWrapErrorFormat, phpT(i18nFailGetKey, workingDirectorySubject), err)
return core.E("php", phpT(i18nFailGetKey, workingDirectorySubject), err)
}

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

status, err := Deploy(ctx, deployOpts)
if err != nil {
return phpFailure(cliWrapErrorFormat, phpT("cmd.php.error.deploy_failed"), err)
return core.E("php", phpT("cmd.php.error.deploy_failed"), err)
}

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

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

status, err := DeployStatus(ctx, statusOpts)
if err != nil {
return phpFailure(cliWrapErrorFormat, phpT(i18nFailGetKey, "status"), err)
return core.E("php", phpT(i18nFailGetKey, "status"), err)
}

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

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

status, err := Rollback(ctx, rollbackOpts)
if err != nil {
return phpFailure(cliWrapErrorFormat, phpT("cmd.php.error.rollback_failed"), err)
return core.E("php", phpT("cmd.php.error.rollback_failed"), err)
}

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

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

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

if len(deployments) == 0 {
Expand Down
10 changes: 5 additions & 5 deletions go/pkg/php/cmd_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type phpDevOptions struct {
func runPHPDev(opts phpDevOptions) error { // Result boundary
cwd, err := os.Getwd()
if err != nil {
return phpFailure("failed to get working directory: %w", err)
return core.E("php", "failed to get working directory", err)
}

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

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

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

logsReader, err := server.Logs(service, follow)
if err != nil {
return phpFailure(cliWrapErrorFormat, phpT(i18nFailGetKey, "logs"), err)
return core.E("php", phpT(i18nFailGetKey, "logs"), err)
}
defer func() { _ = logsReader.Close() }()

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

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

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

certFile, keyFile, _ := CertPaths(domain, SSLOptions{})
Expand Down
16 changes: 8 additions & 8 deletions go/pkg/php/cmd_packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ func addPHPPackagesLinkCommand(c *core.Core, prefix string) {

cwd, err := os.Getwd()
if err != nil {
return phpFailure(cliWrapErrorFormat, phpT(i18nFailGetKey, workingDirectorySubject), err)
return core.E("php", phpT(i18nFailGetKey, workingDirectorySubject), err)
}

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

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

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

cwd, err := os.Getwd()
if err != nil {
return phpFailure(cliWrapErrorFormat, phpT(i18nFailGetKey, workingDirectorySubject), err)
return core.E("php", phpT(i18nFailGetKey, workingDirectorySubject), err)
}

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

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

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

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

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

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

packages, err := ListLinkedPackages(cwd)
if err != nil {
return phpFailure(cliWrapErrorFormat, phpT("i18n.fail.list", "packages"), err)
return core.E("php", phpT("i18n.fail.list", "packages"), err)
}

if len(packages) == 0 {
Expand Down
40 changes: 24 additions & 16 deletions go/pkg/php/go_cli_helpers.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package php

import (
`fmt`
`strings`

core "dappco.re/go"
"dappco.re/go/cli/pkg/cli"
)
Expand Down Expand Up @@ -36,22 +33,22 @@
args []string
}

func phpFailure(format string, args ...any) error { // Result boundary
return fmt.Errorf(format, args...)
func phpFailure(format string, args ...any) error {
return core.E("php", core.Sprintf(format, args...), nil)
}

func phpWrapMessage(err error, message string) error { // Result boundary
func phpWrapMessage(err error, message string) error {
if err == nil {
return nil
}
return fmt.Errorf("%s: %w", message, err)
return core.E("php", message, err)
}

func phpWrapAction(err error, verb, subject string) error { // Result boundary
func phpWrapAction(err error, verb, subject string) error {
if err == nil {
return nil
}
return fmt.Errorf("failed to %s %s: %w", verb, subject, err)
return core.E("php", core.Sprintf("failed to %s %s", verb, subject), err)
}

func phpExit(code int, err error) error { // Result boundary
Expand Down Expand Up @@ -120,7 +117,7 @@
return args[len(parts):], true
}

func phpCommandLineFromOptions(opts core.Options) phpCommandLine {

Check failure on line 120 in go/pkg/php/go_cli_helpers.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=dAppCore_php&issues=AZ3h7_4HwWIgk1RLZoCq&open=AZ3h7_4HwWIgk1RLZoCq&pullRequest=6
line := phpCommandLine{flags: map[string]string{}}
for _, item := range opts.Items() {
switch item.Key {
Expand Down Expand Up @@ -177,35 +174,46 @@
}

func phpSplitFlag(arg string) (key, value string, hasValue bool, ok bool) {
if strings.HasPrefix(arg, "--") {
body := strings.TrimPrefix(arg, "--")
if core.HasPrefix(arg, "--") {
body := core.TrimPrefix(arg, "--")
if body == "" {
return "", "", false, false
}
key, value, hasValue = strings.Cut(body, "=")
key, value, hasValue = phpCutOnEquals(body)
return key, value, hasValue, key != ""
}

if strings.HasPrefix(arg, "-") {
body := strings.TrimPrefix(arg, "-")
if core.HasPrefix(arg, "-") {
body := core.TrimPrefix(arg, "-")
if body == "" {
return "", "", false, false
}
key, value, hasValue = strings.Cut(body, "=")
key, value, hasValue = phpCutOnEquals(body)
return key, value, hasValue, key != ""
}

return "", "", false, false
}

// phpCutOnEquals splits "key=value" into (key, value, true) or returns
// (s, "", false) when no `=` is present. Equivalent of strings.Cut(s, "=")
// without importing strings.
func phpCutOnEquals(s string) (key, value string, hasValue bool) {
parts := core.SplitN(s, "=", 2)
if len(parts) == 2 {
return parts[0], parts[1], true
}
return s, "", false
}

func (line phpCommandLine) Bool(name string, aliases ...string) bool {
keys := append([]string{name}, aliases...)
for _, key := range keys {
value, ok := line.flags[key]
if !ok {
continue
}
switch strings.ToLower(value) {
switch core.Lower(value) {
case "", "1", "true", "yes", "on":
return true
default:
Expand Down
5 changes: 3 additions & 2 deletions go/pkg/php/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
`os/exec`
`path/filepath`

core "dappco.re/go"
"dappco.re/go/cli/pkg/cli"
)

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

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

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

return absPath, pkgName, nil
Expand Down
Loading
Loading