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
83 changes: 4 additions & 79 deletions commands/project_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,14 @@ package commands

import (
"fmt"
"io"
"log"
"os"
"path/filepath"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/symfony-cli/terminal"
"github.com/upsun/lib-sun/detector"
"github.com/upsun/lib-sun/entity"
"github.com/upsun/lib-sun/readers"
utils "github.com/upsun/lib-sun/utility"
"github.com/upsun/lib-sun/writers"
orderedmap "github.com/wk8/go-ordered-map/v2"

"github.com/platformsh/cli/internal/config"
"github.com/platformsh/cli/internal/convert"
)

// innerProjectConvertCommand returns the Command struct for the convert config command.
Expand Down Expand Up @@ -109,81 +101,14 @@ func newProjectConvertCommand(cnf *config.Config) *cobra.Command {

// runProjectConvert is the entry point for the convert config command.
func runProjectConvert(cmd *cobra.Command, _ []string) error {
if viper.GetString("provider") != "platformsh" {
return fmt.Errorf("only the 'platformsh' provider is currently supported")
}
return runPlatformShConvert(cmd)
}

// runPlatformShConvert performs the conversion from Platform.sh config to Upsun config.
func runPlatformShConvert(cmd *cobra.Command) error {
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("could not get current working directory: %w", err)
}

cwd, err = filepath.Abs(filepath.Clean(cwd))
if err != nil {
return fmt.Errorf("could not normalize project workspace path: %w", err)
}

// Disable log for lib-sun
log.Default().SetOutput(io.Discard)

// Find config files
configFiles, err := detector.FindConfig(cwd)
if err != nil {
return fmt.Errorf("could not detect configuration files: %w", err)
}

// Read PSH application config files
var metaConfig entity.MetaConfig
readers.ReadApplications(&metaConfig, configFiles[entity.PSH_APPLICATION], cwd)
readers.ReadPlatforms(&metaConfig, configFiles[entity.PSH_PLATFORM], cwd)
if metaConfig.Applications.IsZero() {
return fmt.Errorf("no Platform.sh applications found")
if viper.GetString("provider") == "platformsh" {
return convert.PlatformshToUpsun(cwd, cmd.ErrOrStderr())
}

// Read PSH services and routes config files
readers.ReadServices(&metaConfig, configFiles[entity.PSH_SERVICE])
readers.ReadRoutes(&metaConfig, configFiles[entity.PSH_ROUTE])

// Remove size and resources entries
readers.RemoveAllEntry(&metaConfig.Services, "size")
readers.RemoveAllEntry(&metaConfig.Applications, "size")
readers.RemoveAllEntry(&metaConfig.Services, "resources")
readers.RemoveAllEntry(&metaConfig.Applications, "resources")

// Fix storage to match Upsun format
readers.ReplaceAllEntry(&metaConfig.Applications, "local", "instance")
readers.ReplaceAllEntry(&metaConfig.Applications, "shared", "storage")
readers.RemoveAllEntry(&metaConfig.Applications, "disk")

upsunDir := filepath.Join(cwd, ".upsun")
if err := os.MkdirAll(upsunDir, os.ModePerm); err != nil {
return fmt.Errorf("could not create .upsun directory: %w", err)
}

configPath := filepath.Join(upsunDir, "config.yaml")
stat, err := os.Stat(configPath)
if err == nil && !stat.IsDir() {
cmd.Printf("The file %v already exists.\n", configPath)
if !viper.GetBool("yes") {
if viper.GetBool("no-interaction") {
return fmt.Errorf("use the -y option to overwrite the file")
}

if !terminal.AskConfirmation("Do you want to overwrite it?", true) {
return nil
}
}
}
writers.GenerateUpsunConfigFile(metaConfig, configPath)

// Move extra config
utils.TransferConfigCustom(cwd, upsunDir)

cmd.Println("Your configuration was successfully converted to the Upsun format.")
cmd.Printf("Check the generated files in %v\n", upsunDir)
return nil
return fmt.Errorf("only the 'platformsh' provider is currently supported")
}
93 changes: 93 additions & 0 deletions internal/convert/platformsh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package convert

import (
"fmt"
"io"
"log"
"os"
"path/filepath"

"github.com/fatih/color"
"github.com/spf13/viper"
"github.com/symfony-cli/terminal"
"github.com/upsun/lib-sun/detector"
"github.com/upsun/lib-sun/entity"
"github.com/upsun/lib-sun/readers"
utils "github.com/upsun/lib-sun/utility"
"github.com/upsun/lib-sun/writers"
)

// PlatformshToUpsun performs the conversion from Platform.sh config to Upsun config.
func PlatformshToUpsun(path string, stderr io.Writer) error {
cwd, err := filepath.Abs(filepath.Clean(path))
if err != nil {
return fmt.Errorf("could not normalize project workspace path: %w", err)
}

upsunDir := filepath.Join(cwd, ".upsun")
configPath := filepath.Join(upsunDir, "config.yaml")
stat, err := os.Stat(configPath)
if err == nil && !stat.IsDir() {
fmt.Fprintln(stderr, "The file already exists:", color.YellowString(configPath))
if !viper.GetBool("yes") {
if viper.GetBool("no-interaction") {
return fmt.Errorf("use the -y option to overwrite the file")
}

if !terminal.AskConfirmation("Do you want to overwrite it?", true) {
return nil
}
}
}

log.Default().SetOutput(stderr)

// Find config files
configFiles, err := detector.FindConfig(cwd)
if err != nil {
return fmt.Errorf("could not detect configuration files: %w", err)
}

// Read PSH application config files
var metaConfig entity.MetaConfig
readers.ReadApplications(&metaConfig, configFiles[entity.PSH_APPLICATION], cwd)
readers.ReadPlatforms(&metaConfig, configFiles[entity.PSH_PLATFORM], cwd)
if metaConfig.Applications.IsZero() {
return fmt.Errorf("no Platform.sh applications found")
}

// Read PSH services and routes config files
readers.ReadServices(&metaConfig, configFiles[entity.PSH_SERVICE])
readers.ReadRoutes(&metaConfig, configFiles[entity.PSH_ROUTE])

// Remove size and resources entries
fmt.Fprintln(stderr, "Removing any `size`, `resources` or `disk` keys.")
fmt.Fprintln(stderr,
"Upsun disk sizes are set using Console or the "+color.GreenString("upsun resources:set")+" command.")
readers.RemoveAllEntry(&metaConfig.Services, "size")
readers.RemoveAllEntry(&metaConfig.Applications, "size")
readers.RemoveAllEntry(&metaConfig.Services, "resources")
readers.RemoveAllEntry(&metaConfig.Applications, "resources")
readers.RemoveAllEntry(&metaConfig.Applications, "disk")
readers.RemoveAllEntry(&metaConfig.Services, "disk")

// Fix storage to match Upsun format
fmt.Fprintln(stderr, "Replacing mount types (`local` becomes `instance`, and `shared` becomes `storage`).")
readers.ReplaceAllEntry(&metaConfig.Applications, "local", "instance")
readers.ReplaceAllEntry(&metaConfig.Applications, "shared", "storage")

if err := os.MkdirAll(upsunDir, os.ModePerm); err != nil {
return fmt.Errorf("could not create .upsun directory: %w", err)
}

fmt.Fprintln(stderr, "Creating combined configuration file.")
writers.GenerateUpsunConfigFile(metaConfig, configPath)

// Move extra config
fmt.Fprintln(stderr, "Copying additional files if necessary.")
utils.TransferConfigCustom(cwd, upsunDir)

fmt.Fprintln(stderr, "Your configuration was successfully converted to the Upsun format.")
fmt.Fprintln(stderr, "Check the generated files in:", color.GreenString(upsunDir))
return nil
}
26 changes: 26 additions & 0 deletions internal/convert/platformsh_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package convert

import (
_ "embed"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

//go:embed testdata/platformsh/.upsun/config-ref.yaml
var configRef string

func TestConvert(t *testing.T) {
tmpDir := t.TempDir()
require.NoError(t, os.CopyFS(tmpDir, os.DirFS("testdata/platformsh")))
assert.NoError(t, PlatformshToUpsun(tmpDir, t.Output()))
assert.FileExists(t, filepath.Join(tmpDir, ".upsun", "config.yaml"))

b, err := os.ReadFile(filepath.Join(tmpDir, ".upsun", "config.yaml"))
assert.NoError(t, err)

assert.Equal(t, configRef, string(b))
}
67 changes: 67 additions & 0 deletions internal/convert/testdata/platformsh/.platform.app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: app

# Runtime pre-install
type: 'php:8.2'

# Disk for App
disk: 2048

# Flexible Ressources
resources:
base_memory: 1024
memory_ratio: 1024

dependencies:
php:
composer/composer: "^2"

# vHost config
web:
locations:
"/":
root: "public"
passthru: "/index.php"
allow: true
scripts: true

relationships:
database: "mysql:mysql"

variables:
env:
CI_ENVIRONMENT: "production"

# RW fs !!
mounts:
"writable/cache":
source: local
source_path: "writable/cache"
"writable/debugbar": { source: local, source_path: "writable/debugbar"}
"writable/logs":
source: local
source_path: "writable/logs"
"writable/session":
source: local
source_path: "writable/session"
"writable/upload":
source: local
source_path: "writable/upload"
"config":
source: local
source_path: "config"

# Custom commands
hooks:
build: |
set -e
composer install --no-dev --optimize-autoloader
deploy: |
set -e
php generate_env.php

source:
operations:
auto-update:
command: |
curl -fsS https://raw.githubusercontent.com/platformsh/source-operations/main/setup.sh | { bash /dev/fd/3 sop-autoupdate; } 3<&0

Loading