Skip to content

Commit

Permalink
Merge pull request #37 from kanmu/support-jsonnet-config
Browse files Browse the repository at this point in the history
Support ecspresso.jsonnet
  • Loading branch information
winebarrel committed May 28, 2023
2 parents ddc28e3 + a91b745 commit e86cf26
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ brew install kanmu/tools/demitas2
## Usage

```
Usage: dmts --ecspresso-cmd="ecspresso" --conf-dir="~/.demitas" --config="ecspresso.yml" --container-def="ecs-container-def.jsonnet" <command>
Usage: dmts --ecspresso-cmd="ecspresso" --conf-dir="~/.demitas" --config=ecspresso.yml,ecspresso.json,ecspresso.jsonnet,... --container-def="ecs-container-def.jsonnet" <command>
Flags:
-h, --help Show context-sensitive help.
Expand All @@ -31,7 +31,8 @@ Flags:
--dry-run Run ecspresso with dry-run.
-P, --aws-profile=STRING AWS profile name ($AWS_PROFILE)
-d, --conf-dir="~/.demitas" Config file base dir ($DMTS_CONF_DIR).
--config="ecspresso.yml" ecspresso config file name ($ECSPRESSO_CONF).
--config=ecspresso.yml,ecspresso.json,ecspresso.jsonnet,...
ecspresso config file name ($ECSPRESSO_CONF).
--container-def="ecs-container-def.jsonnet"
ECS container definition file name
($DMTS_CONT_DEF).
Expand All @@ -48,19 +49,19 @@ Flags:
--cluster=STRING ECS cluster name ($DMTS_CLUSTER).
Commands:
run --ecspresso-cmd="ecspresso" --conf-dir="~/.demitas" --config="ecspresso.yml" --container-def="ecs-container-def.jsonnet"
run --ecspresso-cmd="ecspresso" --conf-dir="~/.demitas" --config=ecspresso.yml,ecspresso.json,ecspresso.jsonnet,... --container-def="ecs-container-def.jsonnet"
Run ECS task.
exec --ecspresso-cmd="ecspresso" --conf-dir="~/.demitas" --config="ecspresso.yml" --container-def="ecs-container-def.jsonnet" --command="bash"
exec --ecspresso-cmd="ecspresso" --conf-dir="~/.demitas" --config=ecspresso.yml,ecspresso.json,ecspresso.jsonnet,... --container-def="ecs-container-def.jsonnet" --command="bash"
Run ECS task and execute a command on a container.
port-forward --ecspresso-cmd="ecspresso" --conf-dir="~/.demitas" --config="ecspresso.yml" --container-def="ecs-container-def.jsonnet" --remote-host=STRING --remote-port=UINT --local-port=UINT
port-forward --ecspresso-cmd="ecspresso" --conf-dir="~/.demitas" --config=ecspresso.yml,ecspresso.json,ecspresso.jsonnet,... --container-def="ecs-container-def.jsonnet" --remote-host=STRING --remote-port=UINT --local-port=UINT
Forward a local port to a container.
profiles --ecspresso-cmd="ecspresso" --conf-dir="~/.demitas" --config="ecspresso.yml" --container-def="ecs-container-def.jsonnet"
profiles --ecspresso-cmd="ecspresso" --conf-dir="~/.demitas" --config=ecspresso.yml,ecspresso.json,ecspresso.jsonnet,... --container-def="ecs-container-def.jsonnet"
List profiles.
install-completions --ecspresso-cmd="ecspresso" --conf-dir="~/.demitas" --config="ecspresso.yml" --container-def="ecs-container-def.jsonnet"
install-completions --ecspresso-cmd="ecspresso" --conf-dir="~/.demitas" --config=ecspresso.yml,ecspresso.json,ecspresso.jsonnet,... --container-def="ecs-container-def.jsonnet"
Install shell completions
Run "dmts <command> --help" for more information on a command.
Expand Down
34 changes: 25 additions & 9 deletions definition/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ package definition
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/kanmu/demitas2/utils"
tilde "gopkg.in/mattes/go-expand-tilde.v1"
)

type DefinitionOpts struct {
ConfDir string `env:"DMTS_CONF_DIR" short:"d" required:"" default:"~/.demitas" help:"Config file base dir."`
Config string `env:"ECSPRESSO_CONF" required:"" default:"ecspresso.yml" help:"ecspresso config file name."`
ContainerDef string `env:"DMTS_CONT_DEF" required:"" default:"ecs-container-def.jsonnet" help:"ECS container definition file name."`
ConfigOverrides string `short:"e" help:"JSON/YAML string that overrides ecspresso config."`
ServiceOverrides string `short:"s" help:"JSON/YAML string that overrides ECS service definition."`
TaskOverrides string `short:"t" help:"JSON/YAML string that overrides ECS task definition."`
ContainerOverrides string `short:"c" help:"JSON/YAML string that overrides ECS container definition."`
Cluster string `env:"DMTS_CLUSTER" help:"ECS cluster name."`
ConfDir string `env:"DMTS_CONF_DIR" short:"d" required:"" default:"~/.demitas" help:"Config file base dir."`
Config []string `env:"ECSPRESSO_CONF" required:"" default:"ecspresso.yml,ecspresso.json,ecspresso.jsonnet" help:"ecspresso config file name."`
ContainerDef string `env:"DMTS_CONT_DEF" required:"" default:"ecs-container-def.jsonnet" help:"ECS container definition file name."`
ConfigOverrides string `short:"e" help:"JSON/YAML string that overrides ecspresso config."`
ServiceOverrides string `short:"s" help:"JSON/YAML string that overrides ECS service definition."`
TaskOverrides string `short:"t" help:"JSON/YAML string that overrides ECS task definition."`
ContainerOverrides string `short:"c" help:"JSON/YAML string that overrides ECS container definition."`
Cluster string `env:"DMTS_CLUSTER" help:"ECS cluster name."`
}

type Definition struct {
Expand Down Expand Up @@ -105,7 +107,21 @@ func (opts *DefinitionOpts) Load(profile string, command string, image string, c
}

func loadEcsecspressoConf(confDir string, opts *DefinitionOpts) (*EcspressoConfig, error) {
ecspressoConf, err := newEcspressoConfig(filepath.Join(confDir, opts.Config))
var cfgFile string

for _, f := range opts.Config {
if _, err := os.Stat(filepath.Join(confDir, f)); err != nil {
continue
}

cfgFile = filepath.Join(confDir, f)
}

if cfgFile == "" {
return nil, fmt.Errorf("ecspresso config file not found: %s", filepath.Join(confDir, strings.Join(opts.Config, ",")))
}

ecspressoConf, err := newEcspressoConfig(cfgFile)

if err != nil {
return nil, err
Expand Down
8 changes: 6 additions & 2 deletions definition/ecspresso_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ func newEcspressoConfig(path string) (*EcspressoConfig, error) {
return nil, fmt.Errorf("failed to load ecspresso config: %w: %s", err, path)
}

js, err := utils.YAMLToJSON(content)
if strings.HasSuffix(path, ".yml") || strings.HasSuffix(path, ".yaml") {
content, err = utils.YAMLToJSON(content)
} else if strings.HasSuffix(path, ".jsonnet") || strings.HasSuffix(path, ".yaml") {
content, err = utils.EvaluateJsonnet(path)
}

if err != nil {
return nil, fmt.Errorf("failed to parse ecspresso config: %w: %s", err, path)
}

ecsConf := &EcspressoConfig{
Content: js,
Content: content,
}

return ecsConf, nil
Expand Down

0 comments on commit e86cf26

Please sign in to comment.