Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring options #643

Merged
merged 9 commits into from
Dec 18, 2023
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
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ $ asdf plugin add ecspresso
# or
$ asdf plugin add ecspresso https://github.com/kayac/asdf-ecspresso.git

$ asdf install ecspresso 2.0.0
$ asdf global ecspresso 2.0.0
$ asdf install ecspresso 2.3.0
$ asdf global ecspresso 2.3.0
```

### aqua (macOS and Linux)
Expand All @@ -49,13 +49,13 @@ https://circleci.com/orbs/registry/orb/fujiwara/ecspresso
```yaml
version: 2.1
orbs:
ecspresso: fujiwara/ecspresso@2.0.3
ecspresso: fujiwara/ecspresso@2.3.0
jobs:
install:
steps:
- checkout
- ecspresso/install:
version: v2.0.0 # or latest
version: v2.3.0 # or latest
# version-file: .ecspresso-version
- run:
command: |
Expand Down Expand Up @@ -86,7 +86,7 @@ jobs:
- uses: actions/checkout@v3
- uses: kayac/ecspresso@v2
with:
version: v2.0.0 # or latest
version: v2.3.0 # or latest
# version-file: .ecspresso-version
- run: |
ecspresso deploy --config ecspresso.yml
Expand All @@ -108,7 +108,7 @@ Pass the parameter "latest" to use the latest version of ecspresso.

`version: latest` is not recommended because it may cause unexpected behavior when the new version of ecspresso is released.

GitHub Action `kayac/ecspresso@v2` supports `version-file: path/to/file` installs ecspresso that version written in the file. This version number does not have `v` prefix, For example `2.0.0`.
GitHub Action `kayac/ecspresso@v2` supports `version-file: path/to/file` installs ecspresso that version written in the file. This version number does not have `v` prefix, For example `2.3.0`.

## Usage

Expand All @@ -117,13 +117,14 @@ Usage: ecspresso <command>

Flags:
-h, --help Show context-sensitive help.
--envfile=ENVFILE,... environment files
--debug enable debug log
--ext-str=KEY=VALUE;... external string values for Jsonnet
--ext-code=KEY=VALUE;... external code values for Jsonnet
--config="ecspresso.yml" config file
--assume-role-arn="" the ARN of the role to assume
--option=OPTION
--envfile=ENVFILE,... environment files ($ECSPRESSO_ENVFILE)
--debug enable debug log ($ECSPRESSO_DEBUG)
--ext-str=KEY=VALUE;... external string values for Jsonnet ($ECSPRESSO_EXT_STR)
--ext-code=KEY=VALUE;... external code values for Jsonnet ($ECSPRESSO_EXT_CODE)
--config="ecspresso.yml" config file ($ECSPRESSO_CONFIG)
--assume-role-arn="" the ARN of the role to assume ($ECSPRESSO_ASSUME_ROLE_ARN)
--timeout=TIMEOUT timeout. Override in a configuration file ($ECSPRESSO_TIMEOUT).
--filter-command=STRING filter command ($ECSPRESSO_FILTER_COMMAND)

Commands:
appspec
Expand Down
39 changes: 36 additions & 3 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ import (
"context"
"fmt"
"os"
"time"
)

type CLIOptions struct {
Option
Envfile []string `help:"environment files" env:"ECSPRESSO_ENVFILE"`
Debug bool `help:"enable debug log" env:"ECSPRESSO_DEBUG"`
ExtStr map[string]string `help:"external string values for Jsonnet" env:"ECSPRESSO_EXT_STR"`
ExtCode map[string]string `help:"external code values for Jsonnet" env:"ECSPRESSO_EXT_CODE"`
ConfigFilePath string `name:"config" help:"config file" default:"ecspresso.yml" env:"ECSPRESSO_CONFIG"`
AssumeRoleARN string `help:"the ARN of the role to assume" default:"" env:"ECSPRESSO_ASSUME_ROLE_ARN"`
Timeout *time.Duration `help:"timeout. Override in a configuration file." env:"ECSPRESSO_TIMEOUT"`
FilterCommand string `help:"filter command" env:"ECSPRESSO_FILTER_COMMAND"`

Appspec *AppSpecOption `cmd:"" help:"output AppSpec YAML for CodeDeploy to STDOUT"`
Delete *DeleteOption `cmd:"" help:"delete service"`
Expand All @@ -30,6 +38,24 @@ type CLIOptions struct {
Version struct{} `cmd:"" help:"show version"`
}

func (opt *CLIOptions) resolveConfigFilePath() (path string) {
path = DefaultConfigFilePath
defer func() {
opt.ConfigFilePath = path
}()
if opt.ConfigFilePath != "" && opt.ConfigFilePath != DefaultConfigFilePath {
path = opt.ConfigFilePath
return
}
for _, ext := range []string{ymlExt, yamlExt, jsonExt, jsonnetExt} {
if _, err := os.Stat("ecspresso" + ext); err == nil {
path = "ecspresso" + ext
return
}
}
return
}

func (opts *CLIOptions) ForSubCommand(sub string) interface{} {
switch sub {
case "appspec":
Expand Down Expand Up @@ -79,8 +105,15 @@ func dispatchCLI(ctx context.Context, sub string, usage func(), opts *CLIOptions
fmt.Println("ecspresso", Version)
return nil
}

app, err := New(ctx, &opts.Option)
var appOpts []AppOption
if sub == "init" {
config, err := opts.Init.NewConfig(ctx, opts.ConfigFilePath)
if err != nil {
return err
}
appOpts = append(appOpts, WithConfig(config))
}
app, err := New(ctx, opts, appOpts...)
if err != nil {
return err
}
Expand Down
39 changes: 18 additions & 21 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
var cliTests = []struct {
args []string
sub string
option *ecspresso.Option
option *ecspresso.CLIOptions
subOption any
fn func(*testing.T, any)
}{
Expand All @@ -28,13 +28,12 @@ var cliTests = []struct {
"--assume-role-arn", "arn:aws:iam::123456789012:role/exampleRole",
},
sub: "status",
option: &ecspresso.Option{
option: &ecspresso.CLIOptions{
ConfigFilePath: "config.yml",
Debug: true,
Envfile: []string{"tests/envfile"},
ExtStr: map[string]string{"s1": "v1", "s2": "v2"},
ExtCode: map[string]string{"c1": "123", "c2": "1+2"},
InitOption: nil,
AssumeRoleARN: "arn:aws:iam::123456789012:role/exampleRole",
},
subOption: &ecspresso.StatusOption{
Expand All @@ -54,12 +53,11 @@ var cliTests = []struct {
"--events=100",
},
sub: "status",
option: &ecspresso.Option{
option: &ecspresso.CLIOptions{
ConfigFilePath: "config.yml",
Debug: true,
ExtStr: map[string]string{},
ExtCode: map[string]string{},
InitOption: nil,
AssumeRoleARN: "",
},
subOption: &ecspresso.StatusOption{
Expand Down Expand Up @@ -587,7 +585,6 @@ var cliTests = []struct {
subOption: &ecspresso.InitOption{
Region: os.Getenv("AWS_REGION"),
Cluster: "default",
ConfigFilePath: "myconfig.yml",
Service: "myservice",
TaskDefinitionPath: "ecs-task-def.json",
ServiceDefinitionPath: "ecs-service-def.json",
Expand All @@ -598,17 +595,7 @@ var cliTests = []struct {
{
args: []string{"init", "--service", "myservice", "--config", "myconfig.yml"},
sub: "init",
option: &ecspresso.Option{
InitOption: &ecspresso.InitOption{
Region: os.Getenv("AWS_REGION"),
Cluster: "default",
ConfigFilePath: "myconfig.yml",
Service: "myservice",
TaskDefinitionPath: "ecs-task-def.json",
ServiceDefinitionPath: "ecs-service-def.json",
ForceOverwrite: false,
Jsonnet: false,
},
option: &ecspresso.CLIOptions{
ConfigFilePath: "myconfig.yml",
Debug: false,
ExtStr: map[string]string{},
Expand All @@ -617,7 +604,6 @@ var cliTests = []struct {
subOption: &ecspresso.InitOption{
Region: os.Getenv("AWS_REGION"),
Cluster: "default",
ConfigFilePath: "myconfig.yml",
Service: "myservice",
TaskDefinitionPath: "ecs-task-def.json",
ServiceDefinitionPath: "ecs-service-def.json",
Expand All @@ -636,7 +622,6 @@ var cliTests = []struct {
subOption: &ecspresso.InitOption{
Region: os.Getenv("AWS_REGION"),
Cluster: "mycluster",
ConfigFilePath: "myconfig.jsonnet",
Service: "myservice",
TaskDefinitionPath: "taskdef.jsonnet",
ServiceDefinitionPath: "servicedef.jsonnet",
Expand All @@ -650,7 +635,6 @@ var cliTests = []struct {
subOption: &ecspresso.InitOption{
Region: os.Getenv("AWS_REGION"),
Cluster: "default",
ConfigFilePath: "myconfig.yml",
Service: "",
TaskDefinition: "app:123",
TaskDefinitionPath: "ecs-task-def.json",
Expand Down Expand Up @@ -798,7 +782,7 @@ func TestParseCLIv2(t *testing.T) {
t.Errorf("unexpected subcommand: expected %s, got %s", tt.sub, sub)
}
if tt.option != nil {
if diff := cmp.Diff(*tt.option, opt.Option); diff != "" {
if diff := cmp.Diff(tt.option, CLIOptionsGlobalOnly(opt)); diff != "" {
t.Errorf("unexpected option: diff %s", diff)
}
}
Expand All @@ -813,3 +797,16 @@ func TestParseCLIv2(t *testing.T) {
})
}
}

func CLIOptionsGlobalOnly(opts *ecspresso.CLIOptions) *ecspresso.CLIOptions {
return &ecspresso.CLIOptions{
ConfigFilePath: opts.ConfigFilePath,
Debug: opts.Debug,
ExtStr: opts.ExtStr,
ExtCode: opts.ExtCode,
Envfile: opts.Envfile,
AssumeRoleARN: opts.AssumeRoleARN,
Timeout: opts.Timeout,
FilterCommand: opts.FilterCommand,
}
}
13 changes: 4 additions & 9 deletions cliv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,11 @@ func ParseCLIv2(args []string) (string, *CLIOptions, func(), error) {
}
}

if opts.Option.ExtStr == nil {
opts.Option.ExtStr = map[string]string{}
if opts.ExtStr == nil {
opts.ExtStr = map[string]string{}
}
if opts.Option.ExtCode == nil {
opts.Option.ExtCode = map[string]string{}
}
switch sub {
case "init":
opts.Init.ConfigFilePath = opts.ConfigFilePath
opts.Option.InitOption = opts.Init
if opts.ExtCode == nil {
opts.ExtCode = map[string]string{}
}
return sub, &opts, func() { c.PrintUsage(true) }, nil
}
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (l *configLoader) Load(ctx context.Context, path string, version string) (*
return conf, nil
}

func (c *Config) OverrideByOption(opt *Option) {
func (c *Config) OverrideByCLIOptions(opt *CLIOptions) {
if opt.Timeout != nil {
c.Timeout = &Duration{*opt.Timeout}
}
Expand Down
6 changes: 3 additions & 3 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func TestLoadServiceDefinition(t *testing.T) {
ctx := context.Background()
app, err := ecspresso.New(ctx, &ecspresso.Option{ConfigFilePath: "tests/test.yaml"})
app, err := ecspresso.New(ctx, &ecspresso.CLIOptions{ConfigFilePath: "tests/test.yaml"})
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -89,7 +89,7 @@ func testLoadConfigWithPlugin(t *testing.T, path string) {
t.Setenv("JSON", `{"foo":"bar"}`)
t.Setenv("AWS_REGION", "ap-northeast-1")
ctx := context.Background()
app, err := ecspresso.New(ctx, &ecspresso.Option{ConfigFilePath: path})
app, err := ecspresso.New(ctx, &ecspresso.CLIOptions{ConfigFilePath: path})
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -311,7 +311,7 @@ var FilterCommandTests = []struct {
func TestFilterCommandDeprecated(t *testing.T) {
ctx := context.Background()
for _, ts := range FilterCommandTests {
app, err := ecspresso.New(ctx, &ecspresso.Option{
app, err := ecspresso.New(ctx, &ecspresso.CLIOptions{
ConfigFilePath: "tests/filter_command.yml",
FilterCommand: ts.Env,
})
Expand Down
Loading
Loading