|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + |
| 10 | + "github.com/spf13/cobra" |
| 11 | + "github.com/spf13/viper" |
| 12 | + "github.com/symfony-cli/terminal" |
| 13 | + "github.com/upsun/lib-sun/detector" |
| 14 | + "github.com/upsun/lib-sun/entity" |
| 15 | + "github.com/upsun/lib-sun/readers" |
| 16 | + utils "github.com/upsun/lib-sun/utility" |
| 17 | + "github.com/upsun/lib-sun/writers" |
| 18 | + orderedmap "github.com/wk8/go-ordered-map/v2" |
| 19 | + |
| 20 | + "github.com/platformsh/cli/internal/config" |
| 21 | +) |
| 22 | + |
| 23 | +// innerProjectConvertCommand returns the Command struct for the convert config command. |
| 24 | +func innerProjectConvertCommand(cnf *config.Config) Command { |
| 25 | + noInteractionOption := NoInteractionOption(cnf) |
| 26 | + providerOption := Option{ |
| 27 | + Name: "--provider", |
| 28 | + Shortcut: "-p", |
| 29 | + IsValueRequired: false, |
| 30 | + Default: Any{"platformsh"}, |
| 31 | + Description: "The provider from which to convert the configuration. Currently, only 'platformsh' is supported.", |
| 32 | + } |
| 33 | + |
| 34 | + return Command{ |
| 35 | + Name: CommandName{ |
| 36 | + Namespace: "project", |
| 37 | + Command: "convert", |
| 38 | + }, |
| 39 | + Usage: []string{ |
| 40 | + cnf.Application.Executable + " convert", |
| 41 | + }, |
| 42 | + Aliases: []string{ |
| 43 | + "convert", |
| 44 | + }, |
| 45 | + Description: "Generate an Upsun compatible configuration based on the configuration from another provider.", |
| 46 | + Help: "", |
| 47 | + Examples: []Example{ |
| 48 | + { |
| 49 | + Commandline: "--provider=platformsh", |
| 50 | + Description: "Convert the Platform.sh project configuration files in your current directory", |
| 51 | + }, |
| 52 | + }, |
| 53 | + Definition: Definition{ |
| 54 | + Arguments: &orderedmap.OrderedMap[string, Argument]{}, |
| 55 | + Options: orderedmap.New[string, Option](orderedmap.WithInitialData[string, Option]( |
| 56 | + orderedmap.Pair[string, Option]{ |
| 57 | + Key: HelpOption.GetName(), |
| 58 | + Value: HelpOption, |
| 59 | + }, |
| 60 | + orderedmap.Pair[string, Option]{ |
| 61 | + Key: VerboseOption.GetName(), |
| 62 | + Value: VerboseOption, |
| 63 | + }, |
| 64 | + orderedmap.Pair[string, Option]{ |
| 65 | + Key: VersionOption.GetName(), |
| 66 | + Value: VersionOption, |
| 67 | + }, |
| 68 | + orderedmap.Pair[string, Option]{ |
| 69 | + Key: YesOption.GetName(), |
| 70 | + Value: YesOption, |
| 71 | + }, |
| 72 | + orderedmap.Pair[string, Option]{ |
| 73 | + Key: noInteractionOption.GetName(), |
| 74 | + Value: noInteractionOption, |
| 75 | + }, |
| 76 | + orderedmap.Pair[string, Option]{ |
| 77 | + Key: "provider", |
| 78 | + Value: providerOption, |
| 79 | + }, |
| 80 | + )), |
| 81 | + }, |
| 82 | + Hidden: false, |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// newProjectConvertCommand creates the cobra command for converting config. |
| 87 | +func newProjectConvertCommand(cnf *config.Config) *cobra.Command { |
| 88 | + cmd := &cobra.Command{ |
| 89 | + Use: "project:convert", |
| 90 | + Short: "Generate locally Upsun configuration from another provider", |
| 91 | + Aliases: []string{"convert"}, |
| 92 | + RunE: runProjectConvert, |
| 93 | + } |
| 94 | + |
| 95 | + cmd.Flags().StringP( |
| 96 | + "provider", |
| 97 | + "p", |
| 98 | + "platformsh", |
| 99 | + "The provider from which to convert the configuration. Currently, only 'platformsh' is supported.", |
| 100 | + ) |
| 101 | + |
| 102 | + _ = viper.BindPFlag("provider", cmd.Flags().Lookup("provider")) |
| 103 | + cmd.SetHelpFunc(func(_ *cobra.Command, _ []string) { |
| 104 | + internalCmd := innerProjectConvertCommand(cnf) |
| 105 | + fmt.Println(internalCmd.HelpPage(cnf)) |
| 106 | + }) |
| 107 | + return cmd |
| 108 | +} |
| 109 | + |
| 110 | +// runProjectConvert is the entry point for the convert config command. |
| 111 | +func runProjectConvert(cmd *cobra.Command, _ []string) error { |
| 112 | + if viper.GetString("provider") != "platformsh" { |
| 113 | + return fmt.Errorf("only the 'platformsh' provider is currently supported") |
| 114 | + } |
| 115 | + return runPlatformShConvert(cmd) |
| 116 | +} |
| 117 | + |
| 118 | +// runPlatformShConvert performs the conversion from Platform.sh config to Upsun config. |
| 119 | +func runPlatformShConvert(cmd *cobra.Command) error { |
| 120 | + cwd, err := os.Getwd() |
| 121 | + if err != nil { |
| 122 | + return fmt.Errorf("could not get current working directory: %w", err) |
| 123 | + } |
| 124 | + |
| 125 | + cwd, err = filepath.Abs(filepath.Clean(cwd)) |
| 126 | + if err != nil { |
| 127 | + return fmt.Errorf("could not normalize project workspace path: %w", err) |
| 128 | + } |
| 129 | + |
| 130 | + // Disable log for lib-sun |
| 131 | + log.Default().SetOutput(io.Discard) |
| 132 | + |
| 133 | + // Find config files |
| 134 | + configFiles, err := detector.FindConfig(cwd) |
| 135 | + if err != nil { |
| 136 | + return fmt.Errorf("could not detect configuration files: %w", err) |
| 137 | + } |
| 138 | + |
| 139 | + // Read PSH application config files |
| 140 | + var metaConfig entity.MetaConfig |
| 141 | + readers.ReadApplications(&metaConfig, configFiles[entity.PSH_APPLICATION], cwd) |
| 142 | + readers.ReadPlatforms(&metaConfig, configFiles[entity.PSH_PLATFORM], cwd) |
| 143 | + if metaConfig.Applications.IsZero() { |
| 144 | + return fmt.Errorf("no Platform.sh applications found") |
| 145 | + } |
| 146 | + |
| 147 | + // Read PSH services and routes config files |
| 148 | + readers.ReadServices(&metaConfig, configFiles[entity.PSH_SERVICE]) |
| 149 | + readers.ReadRoutes(&metaConfig, configFiles[entity.PSH_ROUTE]) |
| 150 | + |
| 151 | + // Remove size and resources entries |
| 152 | + readers.RemoveAllEntry(&metaConfig.Services, "size") |
| 153 | + readers.RemoveAllEntry(&metaConfig.Applications, "size") |
| 154 | + readers.RemoveAllEntry(&metaConfig.Services, "resources") |
| 155 | + readers.RemoveAllEntry(&metaConfig.Applications, "resources") |
| 156 | + |
| 157 | + // Fix storage to match Upsun format |
| 158 | + readers.ReplaceAllEntry(&metaConfig.Applications, "local", "instance") |
| 159 | + readers.ReplaceAllEntry(&metaConfig.Applications, "shared", "storage") |
| 160 | + readers.RemoveAllEntry(&metaConfig.Applications, "disk") |
| 161 | + |
| 162 | + upsunDir := filepath.Join(cwd, ".upsun") |
| 163 | + if err := os.MkdirAll(upsunDir, os.ModePerm); err != nil { |
| 164 | + return fmt.Errorf("could not create .upsun directory: %w", err) |
| 165 | + } |
| 166 | + |
| 167 | + configPath := filepath.Join(upsunDir, "config.yaml") |
| 168 | + stat, err := os.Stat(configPath) |
| 169 | + if err == nil && !stat.IsDir() { |
| 170 | + cmd.Printf("The file %v already exists.\n", configPath) |
| 171 | + if !viper.GetBool("yes") { |
| 172 | + if viper.GetBool("no-interaction") { |
| 173 | + return fmt.Errorf("use the -y option to overwrite the file") |
| 174 | + } |
| 175 | + |
| 176 | + if !terminal.AskConfirmation("Do you want to overwrite it?", true) { |
| 177 | + return nil |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + writers.GenerateUpsunConfigFile(metaConfig, configPath) |
| 182 | + |
| 183 | + // Move extra config |
| 184 | + utils.TransferConfigCustom(cwd, upsunDir) |
| 185 | + |
| 186 | + cmd.Println("Your configuration was successfully converted to the Upsun format.") |
| 187 | + cmd.Printf("Check the generated files in %v\n", upsunDir) |
| 188 | + return nil |
| 189 | +} |
0 commit comments