|
| 1 | +// Copyright 2024 The PipeCD Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package initialize |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/pipe-cd/pipecd/pkg/app/pipectl/cmd/initialize/prompt" |
| 19 | + "github.com/pipe-cd/pipecd/pkg/config" |
| 20 | +) |
| 21 | + |
| 22 | +// Use genericConfigs in order to simplify using the GenericApplicationSpec and keep the order as we want. |
| 23 | +type genericECSApplicationSpec struct { |
| 24 | + Name string `json:"name"` |
| 25 | + Input config.ECSDeploymentInput `json:"input"` |
| 26 | + Description string `json:"description,omitempty"` |
| 27 | +} |
| 28 | + |
| 29 | +func generateECSConfig(p prompt.Prompt) (*genericConfig, error) { |
| 30 | + // inputs |
| 31 | + var ( |
| 32 | + appName string |
| 33 | + serviceDefFile string |
| 34 | + taskDefFile string |
| 35 | + targetGroupArn string |
| 36 | + containerName string |
| 37 | + containerPort int |
| 38 | + ) |
| 39 | + inputs := []prompt.Input{ |
| 40 | + { |
| 41 | + Message: "Name of the application", |
| 42 | + TargetPointer: &appName, |
| 43 | + Required: true, |
| 44 | + }, |
| 45 | + { |
| 46 | + Message: "Name of the service definition file (e.g. serviceDef.yaml)", |
| 47 | + TargetPointer: &serviceDefFile, |
| 48 | + Required: true, |
| 49 | + }, |
| 50 | + { |
| 51 | + Message: "Name of the task definition file (e.g. taskDef.yaml)", |
| 52 | + TargetPointer: &taskDefFile, |
| 53 | + Required: true, |
| 54 | + }, |
| 55 | + // target group inputs |
| 56 | + { |
| 57 | + Message: "ARN of the target group to the service", |
| 58 | + TargetPointer: &targetGroupArn, |
| 59 | + Required: false, |
| 60 | + }, |
| 61 | + { |
| 62 | + Message: "Name of the container of the target group", |
| 63 | + TargetPointer: &containerName, |
| 64 | + Required: false, |
| 65 | + }, |
| 66 | + { |
| 67 | + Message: "Port number of the container of the target group", |
| 68 | + TargetPointer: &containerPort, |
| 69 | + Required: false, |
| 70 | + }, |
| 71 | + } |
| 72 | + |
| 73 | + err := p.RunSlice(inputs) |
| 74 | + if err != nil { |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + |
| 78 | + spec := &genericECSApplicationSpec{ |
| 79 | + Name: appName, |
| 80 | + Input: config.ECSDeploymentInput{ |
| 81 | + ServiceDefinitionFile: serviceDefFile, |
| 82 | + TaskDefinitionFile: taskDefFile, |
| 83 | + TargetGroups: config.ECSTargetGroups{ |
| 84 | + Primary: &config.ECSTargetGroup{ |
| 85 | + TargetGroupArn: targetGroupArn, |
| 86 | + ContainerName: containerName, |
| 87 | + ContainerPort: containerPort, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + Description: "Generated by `pipectl init`. See https://pipecd.dev/docs/user-guide/configuration-reference/ for more.", |
| 92 | + } |
| 93 | + |
| 94 | + return &genericConfig{ |
| 95 | + Kind: config.KindECSApp, |
| 96 | + APIVersion: config.VersionV1Beta1, |
| 97 | + ApplicationSpec: spec, |
| 98 | + }, nil |
| 99 | +} |
0 commit comments