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

New logger #432

Merged
merged 3 commits into from
Sep 27, 2022
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
14 changes: 6 additions & 8 deletions cmd/ecspresso/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,16 @@ func _main() int {
}
}

app, err := ecspresso.NewApp(c)
opt := &ecspresso.Option{
Debug: *debug,
ExtStr: *extStr,
ExtCode: *extCode,
}
app, err := ecspresso.New(c, opt)
if err != nil {
log.Println(err)
return 1
}
app.Debug = *debug
app.ExtStr = *extStr
app.ExtCode = *extCode

switch sub {
case "deploy":
Expand Down Expand Up @@ -305,7 +307,3 @@ func _main() int {
func boolp(b bool) *bool {
return &b
}

func int64p(i int64) *int64 {
return &i
}
6 changes: 1 addition & 5 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"text/template"
"time"

"github.com/fatih/color"
goVersion "github.com/hashicorp/go-version"
"github.com/kayac/ecspresso/appspec"
goConfig "github.com/kayac/go-config"
Expand Down Expand Up @@ -96,10 +95,7 @@ func (c *Config) ValidateVersion(version string) error {
}
v, err := goVersion.NewVersion(version)
if err != nil {
fmt.Fprintln(
os.Stderr,
color.YellowString("WARNING: Invalid version format \"%s\". Skip checking required_version.", version),
)
Log("[WARNING] Invalid version format \"%s\". Skip checking required_version.", version)
// invalid version string (e.g. "current") always allowed
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestLoadServiceDefinition(t *testing.T) {
if err != nil {
t.Error(err)
}
app, err := ecspresso.NewApp(c)
app, err := ecspresso.New(c, &ecspresso.Option{})
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -56,7 +56,7 @@ func TestLoadConfigWithPluginDuplicate(t *testing.T) {
if err != nil {
t.Error(err)
}
_, err = ecspresso.NewApp(conf)
_, err = ecspresso.New(conf, &ecspresso.Option{})
if err == nil {
t.Log("expected an error to occur, but it didn't.")
t.FailNow()
Expand All @@ -83,7 +83,7 @@ func testLoadConfigWithPlugin(t *testing.T, path string) {
if err != nil {
t.Error(err)
}
app, err := ecspresso.NewApp(conf)
app, err := ecspresso.New(conf, &ecspresso.Option{})
if err != nil {
t.Error(err)
}
Expand Down
2 changes: 1 addition & 1 deletion create.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (d *App) Create(opt CreateOption) error {
ctx, cancel := d.Start()
defer cancel()

d.Log("Starting create service", opt.DryRunString())
d.Log("Starting create service %s", opt.DryRunString())
svd, err := d.LoadServiceDefinition(d.config.ServiceDefinitionPath)
if err != nil {
return err
Expand Down
23 changes: 11 additions & 12 deletions deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (d *App) Deploy(opt DeployOption) error {
defer cancel()

var sv *Service
d.Log("Starting deploy", opt.DryRunString())
d.Log("Starting deploy %s", opt.DryRunString())
sv, err := d.DescribeServiceStatus(ctx, 0)
if err != nil {
return err
Expand Down Expand Up @@ -96,7 +96,7 @@ func (d *App) Deploy(opt DeployOption) error {
count = calcDesiredCount(sv, opt)
}
if count != nil {
d.Log("desired count:", *count)
d.Log("desired count: %d", *count)
} else {
d.Log("desired count: unchanged")
}
Expand Down Expand Up @@ -210,7 +210,6 @@ func (d *App) UpdateServiceAttributes(ctx context.Context, sv *Service, opt Depl
return nil
}
d.Log("Updating service attributes...")
d.DebugLog(in)

if _, err := d.ecs.UpdateService(ctx, in); err != nil {
return fmt.Errorf("failed to update service attributes: %w", err)
Expand All @@ -221,7 +220,7 @@ func (d *App) UpdateServiceAttributes(ctx context.Context, sv *Service, opt Depl

func (d *App) DeployByCodeDeploy(ctx context.Context, taskDefinitionArn string, count *int32, sv *Service, opt DeployOption) error {
if count != nil {
d.Log("updating desired count to", *count)
d.Log("updating desired count to %d", *count)
}
_, err := d.ecs.UpdateService(
ctx,
Expand All @@ -244,7 +243,7 @@ func (d *App) DeployByCodeDeploy(ctx context.Context, taskDefinitionArn string,

func (d *App) findDeploymentInfo(ctx context.Context) (*cdTypes.DeploymentInfo, error) {
// search deploymentGroup in CodeDeploy
d.DebugLog("find all applications in CodeDeploy")
d.Log("[DEBUG] find all applications in CodeDeploy")
la, err := d.codedeploy.ListApplications(ctx, &codedeploy.ListApplicationsInput{})
if err != nil {
return nil, fmt.Errorf("failed to list applications in CodeDeploy: %w", err)
Expand All @@ -265,7 +264,7 @@ func (d *App) findDeploymentInfo(ctx context.Context) (*cdTypes.DeploymentInfo,
return nil, fmt.Errorf("failed to batch get applications in CodeDeploy: %w", err)
}
for _, info := range apps.ApplicationsInfo {
d.DebugLog("application", info)
d.Log("[DEBUG] application %v", info)
if info.ComputePlatform != cdTypes.ComputePlatformEcs {
continue
}
Expand All @@ -276,7 +275,7 @@ func (d *App) findDeploymentInfo(ctx context.Context) (*cdTypes.DeploymentInfo,
return nil, fmt.Errorf("failed to list deployment groups in CodeDeploy: %w", err)
}
if len(lg.DeploymentGroups) == 0 {
d.DebugLog("no deploymentGroups in application", *info.ApplicationName)
d.Log("[DEBUG] no deploymentGroups in application %v", *info.ApplicationName)
continue
}
groups, err := d.codedeploy.BatchGetDeploymentGroups(ctx, &codedeploy.BatchGetDeploymentGroupsInput{
Expand All @@ -287,7 +286,7 @@ func (d *App) findDeploymentInfo(ctx context.Context) (*cdTypes.DeploymentInfo,
return nil, fmt.Errorf("failed to batch get deployment groups in CodeDeploy: %w", err)
}
for _, dg := range groups.DeploymentGroupsInfo {
d.DebugLog("deploymentGroup", dg)
d.Log("[DEBUG] deploymentGroup %v", dg)
for _, ecsService := range dg.EcsServices {
if *ecsService.ClusterName == d.config.Cluster && *ecsService.ServiceName == d.config.Service {
return &cdTypes.DeploymentInfo{
Expand Down Expand Up @@ -315,7 +314,7 @@ func (d *App) createDeployment(ctx context.Context, sv *Service, taskDefinitionA
if d.config.AppSpec != nil {
spec.Hooks = d.config.AppSpec.Hooks
}
d.DebugLog("appSpecContent:", spec.String())
d.Log("[DEBUG] appSpecContent: %s", spec.String())

// deployment
dp, err := d.findDeploymentInfo(ctx)
Expand Down Expand Up @@ -353,7 +352,7 @@ func (d *App) createDeployment(ctx context.Context, sv *Service, taskDefinitionA
}
}

d.DebugLog("creating a deployment to CodeDeploy", dd)
d.Log("[DEBUG] creating a deployment to CodeDeploy %v", dd)

res, err := d.codedeploy.CreateDeployment(ctx, dd)
if err != nil {
Expand All @@ -366,12 +365,12 @@ func (d *App) createDeployment(ctx context.Context, sv *Service, taskDefinitionA
id,
d.config.Region,
)
d.Log(fmt.Sprintf("Deployment %s is created on CodeDeploy:", id))
d.Log("Deployment %s is created on CodeDeploy:", id)
d.Log(u)

if isatty.IsTerminal(os.Stdout.Fd()) {
if err := exec.Command("open", u).Start(); err != nil {
d.Log("Couldn't open URL", u)
d.Log("Couldn't open URL %s", u)
}
}
return nil
Expand Down
24 changes: 12 additions & 12 deletions deregister.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (opt DeregisterOption) DryRunString() string {
func (d *App) Deregister(opt DeregisterOption) error {
ctx, cancel := d.Start()
defer cancel()
d.Log("Starting deregister task definition", opt.DryRunString())
d.Log("Starting deregister task definition %s", opt.DryRunString())

inUse, err := d.inUseRevisions(ctx)
if err != nil {
Expand All @@ -56,18 +56,18 @@ func (d *App) deregiserRevision(ctx context.Context, opt DeregisterOption, inUse
}

if aws.ToBool(opt.DryRun) {
d.Log(fmt.Sprintf("task definition %s will be deregistered", name))
d.Log("task definition %s will be deregistered", name)
d.Log("DRY RUN OK")
return nil
}
if aws.ToBool(opt.Force) || prompter.YesNo(fmt.Sprintf("Deregister %s ?", name), false) {
d.Log(fmt.Sprintf("Deregistring %s", name))
d.Log("Deregistring %s", name)
if _, err := d.ecs.DeregisterTaskDefinition(ctx, &ecs.DeregisterTaskDefinitionInput{
TaskDefinition: aws.String(name),
}); err != nil {
return fmt.Errorf("failed to deregister task definition: %w", err)
}
d.Log(fmt.Sprintf("%s was deregistered successfully", name))
d.Log("%s was deregistered successfully", name)
} else {
d.Log("Aborted")
return fmt.Errorf("confirmation failed")
Expand Down Expand Up @@ -97,9 +97,9 @@ func (d *App) deregisterKeeps(ctx context.Context, opt DeregisterOption, inUse m
continue
}
if s := inUse[name]; s != "" {
d.Log(fmt.Sprintf("%s is in use by %s. skip", name, s))
d.Log("%s is in use by %s. skip", name, s)
} else {
d.DebugLog(fmt.Sprintf("%s is marked to deregister", name))
d.Log("[DEBUG] %s is marked to deregister", name)
names = append(names, name)
}
}
Expand All @@ -116,7 +116,7 @@ func (d *App) deregisterKeeps(ctx context.Context, opt DeregisterOption, inUse m
}
for i, name := range names {
if i < idx {
d.Log(fmt.Sprintf("%s will be deregistered", name))
d.Log("%s will be deregistered", name)
deregs = append(deregs, name)
}
}
Expand All @@ -128,21 +128,21 @@ func (d *App) deregisterKeeps(ctx context.Context, opt DeregisterOption, inUse m
deregistered := 0
if aws.ToBool(opt.Force) || prompter.YesNo(fmt.Sprintf("Deregister %d revisons?", len(deregs)), false) {
for _, name := range deregs {
d.Log(fmt.Sprintf("Deregistring %s", name))
d.Log("Deregistring %s", name)
if _, err := d.ecs.DeregisterTaskDefinition(ctx, &ecs.DeregisterTaskDefinitionInput{
TaskDefinition: aws.String(name),
}); err != nil {
return fmt.Errorf("failed to deregister task definition: %w", err)
}
d.Log(fmt.Sprintf("%s was deregistered successfully", name))
d.Log("%s was deregistered successfully", name)
time.Sleep(time.Second)
deregistered++
}
} else {
d.Log("Aborted")
return fmt.Errorf("confirmation failed")
}
d.Log(fmt.Sprintf("%d task definitions were deregistered", deregistered))
d.Log("%d task definitions were deregistered", deregistered)

return nil
}
Expand All @@ -163,7 +163,7 @@ func (d *App) inUseRevisions(ctx context.Context) (map[string]string, error) {
// ignore STOPPED tasks for in use
inUse[name] = st + " task"
}
d.DebugLog(fmt.Sprintf("%s is in use by tasks", name))
d.Log("[DEBUG] %s is in use by tasks", name)
}

if d.config.Service != "" {
Expand All @@ -174,7 +174,7 @@ func (d *App) inUseRevisions(ctx context.Context) (map[string]string, error) {
for _, dp := range sv.Deployments {
name, _ := taskDefinitionToName(*dp.TaskDefinition)
inUse[name] = fmt.Sprintf("%s deployment", *dp.Status)
d.DebugLog(fmt.Sprintf("%s is in use by deployments", name))
d.Log("[DEBUG] %s is in use by deployments", name)
}
}
return inUse, nil
Expand Down
Loading