Skip to content
Closed
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
50 changes: 29 additions & 21 deletions commands/project_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package commands

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

"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/symfony-cli/terminal"
Expand Down Expand Up @@ -127,8 +127,24 @@ func runPlatformShConvert(cmd *cobra.Command) error {
return fmt.Errorf("could not normalize project workspace path: %w", err)
}

// Disable log for lib-sun
log.Default().SetOutput(io.Discard)
upsunDir := filepath.Join(cwd, ".upsun")
configPath := filepath.Join(upsunDir, "config.yaml")
stat, err := os.Stat(configPath)
if err == nil && !stat.IsDir() {
fmt.Fprintln(cmd.ErrOrStderr(), "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
}
}
}

// Override lib-sun's log output to stderr.
log.Default().SetOutput(cmd.ErrOrStderr())

// Find config files
configFiles, err := detector.FindConfig(cwd)
Expand All @@ -149,41 +165,33 @@ func runPlatformShConvert(cmd *cobra.Command) error {
readers.ReadRoutes(&metaConfig, configFiles[entity.PSH_ROUTE])

// Remove size and resources entries
fmt.Fprintln(cmd.ErrOrStderr(), "Removing any `size`, `resources` or `disk` keys.")
fmt.Fprintln(cmd.ErrOrStderr(),
"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(cmd.ErrOrStderr(), "Replacing mount types (`local` becomes `instance`, and `shared` becomes `storage`).")
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
}
}
}
fmt.Fprintln(cmd.ErrOrStderr(), "Creating combined configuration file.")
writers.GenerateUpsunConfigFile(metaConfig, configPath)

// Move extra config
fmt.Fprintln(cmd.ErrOrStderr(), "Copying additional files if necessary.")
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)
fmt.Fprintln(cmd.ErrOrStderr(), "Your configuration was successfully converted to the Upsun format.")
fmt.Fprintln(cmd.ErrOrStderr(), "Check the generated files in:", color.GreenString(upsunDir))
return nil
}