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

feat: pretty logs #69

Merged
merged 1 commit into from
Sep 25, 2024
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
4 changes: 2 additions & 2 deletions cmd/actions/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ var buildCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
sq := squadron.New(cwd, "", flagFiles)

if err := sq.MergeConfigFiles(); err != nil {
if err := sq.MergeConfigFiles(cmd.Context()); err != nil {
return errors.Wrap(err, "failed to merge config files")
}

squadronName, unitNames := parseSquadronAndUnitNames(args)
if err := sq.FilterConfig(squadronName, unitNames, flagTags); err != nil {
if err := sq.FilterConfig(cmd.Context(), squadronName, unitNames, flagTags); err != nil {
return errors.Wrap(err, "failed to filter config")
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/actions/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ var configCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
sq := squadron.New(cwd, "", flagFiles)

if err := sq.MergeConfigFiles(); err != nil {
if err := sq.MergeConfigFiles(cmd.Context()); err != nil {
return errors.Wrap(err, "failed to merge config files")
}

squadronName, unitNames := parseSquadronAndUnitNames(args)
if err := sq.FilterConfig(squadronName, unitNames, flagTags); err != nil {
if err := sq.FilterConfig(cmd.Context(), squadronName, unitNames, flagTags); err != nil {
return errors.Wrap(err, "failed to filter config")
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/actions/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ var diffCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
sq := squadron.New(cwd, flagNamespace, flagFiles)

if err := sq.MergeConfigFiles(); err != nil {
if err := sq.MergeConfigFiles(cmd.Context()); err != nil {
return errors.Wrap(err, "failed to merge config files")
}

args, helmArgs := parseExtraArgs(args)

squadronName, unitNames := parseSquadronAndUnitNames(args)
if err := sq.FilterConfig(squadronName, unitNames, flagTags); err != nil {
if err := sq.FilterConfig(cmd.Context(), squadronName, unitNames, flagTags); err != nil {
return errors.Wrap(err, "failed to filter config")
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/actions/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ var downCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
sq := squadron.New(cwd, flagNamespace, flagFiles)

if err := sq.MergeConfigFiles(); err != nil {
if err := sq.MergeConfigFiles(cmd.Context()); err != nil {
return errors.Wrap(err, "failed to merge config files")
}

args, helmArgs := parseExtraArgs(args)

squadronName, unitNames := parseSquadronAndUnitNames(args)
if err := sq.FilterConfig(squadronName, unitNames, flagTags); err != nil {
if err := sq.FilterConfig(cmd.Context(), squadronName, unitNames, flagTags); err != nil {
return errors.Wrap(err, "failed to filter config")
}

Expand Down
10 changes: 6 additions & 4 deletions cmd/actions/list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package actions

import (
"context"

"github.com/foomo/squadron"
"github.com/foomo/squadron/internal/config"
"github.com/pkg/errors"
Expand Down Expand Up @@ -30,21 +32,21 @@ var listCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
sq := squadron.New(cwd, "", flagFiles)

if err := sq.MergeConfigFiles(); err != nil {
if err := sq.MergeConfigFiles(cmd.Context()); err != nil {
return errors.Wrap(err, "failed to merge config files")
}

squadronName, unitNames := parseSquadronAndUnitNames(args)
if err := sq.FilterConfig(squadronName, unitNames, flagTags); err != nil {
if err := sq.FilterConfig(cmd.Context(), squadronName, unitNames, flagTags); err != nil {
return errors.Wrap(err, "failed to filter config")
}

var list pterm.LeveledList

// List squadrons
_ = sq.Config().Squadrons.Iterate(func(key string, value config.Map[*config.Unit]) error {
_ = sq.Config().Squadrons.Iterate(cmd.Context(), func(ctx context.Context, key string, value config.Map[*config.Unit]) error {
list = append(list, pterm.LeveledListItem{Level: 0, Text: key})
return value.Iterate(func(k string, v *config.Unit) error {
return value.Iterate(ctx, func(ctx context.Context, k string, v *config.Unit) error {
list = append(list, pterm.LeveledListItem{Level: 1, Text: k})
if flagWithTags && len(v.Tags) > 0 {
list = append(list, pterm.LeveledListItem{Level: 2, Text: "🔖: " + v.Tags.SortedString()})
Expand Down
4 changes: 2 additions & 2 deletions cmd/actions/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ var pushCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
sq := squadron.New(cwd, flagNamespace, flagFiles)

if err := sq.MergeConfigFiles(); err != nil {
if err := sq.MergeConfigFiles(cmd.Context()); err != nil {
return errors.Wrap(err, "failed to merge config files")
}

squadronName, unitNames := parseSquadronAndUnitNames(args)
if err := sq.FilterConfig(squadronName, unitNames, flagTags); err != nil {
if err := sq.FilterConfig(cmd.Context(), squadronName, unitNames, flagTags); err != nil {
return errors.Wrap(err, "failed to filter config")
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/actions/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ var rollbackCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
sq := squadron.New(cwd, flagNamespace, flagFiles)

if err := sq.MergeConfigFiles(); err != nil {
if err := sq.MergeConfigFiles(cmd.Context()); err != nil {
return errors.Wrap(err, "failed to merge config files")
}

args, helmArgs := parseExtraArgs(args)

squadronName, unitNames := parseSquadronAndUnitNames(args)
if err := sq.FilterConfig(squadronName, unitNames, flagTags); err != nil {
if err := sq.FilterConfig(cmd.Context(), squadronName, unitNames, flagTags); err != nil {
return errors.Wrap(err, "failed to filter config")
}

Expand Down
9 changes: 6 additions & 3 deletions cmd/actions/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,12 @@ func init() {

rootCmd.AddCommand(upCmd, diffCmd, downCmd, buildCmd, pushCmd, listCmd, rollbackCmd, statusCmd, configCmd, versionCmd, completionCmd, templateCmd, postRendererCmd)

pterm.Info = *pterm.Info.WithPrefix(pterm.Prefix{Text: "INFO", Style: pterm.Info.Prefix.Style})
pterm.Error = *pterm.Info.WithPrefix(pterm.Prefix{Text: "ERROR", Style: pterm.Error.Prefix.Style})
pterm.Warning = *pterm.Info.WithPrefix(pterm.Prefix{Text: "WARNING", Style: pterm.Warning.Prefix.Style})
pterm.Info = *pterm.Info.WithPrefix(pterm.Prefix{Text: "⎈", Style: pterm.Info.Prefix.Style})
pterm.Debug = *pterm.Debug.WithPrefix(pterm.Prefix{Text: "⚒︎", Style: pterm.Debug.Prefix.Style})
pterm.Fatal = *pterm.Fatal.WithPrefix(pterm.Prefix{Text: "💀", Style: pterm.Fatal.Prefix.Style})
pterm.Error = *pterm.Error.WithPrefix(pterm.Prefix{Text: "⛌", Style: pterm.Error.Prefix.Style})
pterm.Warning = *pterm.Info.WithPrefix(pterm.Prefix{Text: "⚠", Style: pterm.Warning.Prefix.Style})
pterm.Success = *pterm.Success.WithPrefix(pterm.Prefix{Text: "✓", Style: pterm.Success.Prefix.Style})
}

func Execute() {
Expand Down
4 changes: 2 additions & 2 deletions cmd/actions/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ var statusCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
sq := squadron.New(cwd, flagNamespace, flagFiles)

if err := sq.MergeConfigFiles(); err != nil {
if err := sq.MergeConfigFiles(cmd.Context()); err != nil {
return errors.Wrap(err, "failed to merge config files")
}

args, helmArgs := parseExtraArgs(args)

squadronName, unitNames := parseSquadronAndUnitNames(args)
if err := sq.FilterConfig(squadronName, unitNames, flagTags); err != nil {
if err := sq.FilterConfig(cmd.Context(), squadronName, unitNames, flagTags); err != nil {
return errors.Wrap(err, "failed to filter config")
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/actions/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ var templateCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
sq := squadron.New(cwd, flagNamespace, flagFiles)

if err := sq.MergeConfigFiles(); err != nil {
if err := sq.MergeConfigFiles(cmd.Context()); err != nil {
return errors.Wrap(err, "failed to merge config files")
}

args, helmArgs := parseExtraArgs(args)

squadronName, unitNames := parseSquadronAndUnitNames(args)
if err := sq.FilterConfig(squadronName, unitNames, flagTags); err != nil {
if err := sq.FilterConfig(cmd.Context(), squadronName, unitNames, flagTags); err != nil {
return errors.Wrap(err, "failed to filter config")
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/actions/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ var upCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
sq := squadron.New(cwd, flagNamespace, flagFiles)

if err := sq.MergeConfigFiles(); err != nil {
if err := sq.MergeConfigFiles(cmd.Context()); err != nil {
return errors.Wrap(err, "failed to merge config files")
}

args, helmArgs := parseExtraArgs(args)

squadronName, unitNames := parseSquadronAndUnitNames(args)
if err := sq.FilterConfig(squadronName, unitNames, flagTags); err != nil {
if err := sq.FilterConfig(cmd.Context(), squadronName, unitNames, flagTags); err != nil {
return errors.Wrap(err, "failed to filter config")
}

Expand Down
11 changes: 6 additions & 5 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"context"
"fmt"

"github.com/pkg/errors"
Expand All @@ -21,10 +22,10 @@ type Config struct {
}

// BuildDependencies returns a map of requested build dependencies
func (c *Config) BuildDependencies() map[string]Build {
func (c *Config) BuildDependencies(ctx context.Context) map[string]Build {
ret := map[string]Build{}
_ = c.Squadrons.Iterate(func(key string, value Map[*Unit]) error {
return value.Iterate(func(k string, v *Unit) error {
_ = c.Squadrons.Iterate(ctx, func(ctx context.Context, key string, value Map[*Unit]) error {
return value.Iterate(ctx, func(ctx context.Context, k string, v *Unit) error {
for _, build := range v.Builds {
for _, dependency := range build.Dependencies {
b, ok := c.Builds[dependency]
Expand All @@ -44,8 +45,8 @@ func (c *Config) BuildDependencies() map[string]Build {
}

// Trim delete empty squadron recursively
func (c *Config) Trim() {
_ = c.Squadrons.Iterate(func(key string, value Map[*Unit]) error {
func (c *Config) Trim(ctx context.Context) {
_ = c.Squadrons.Iterate(ctx, func(ctx context.Context, key string, value Map[*Unit]) error {
value.Trim()
return nil
})
Expand Down
8 changes: 6 additions & 2 deletions internal/config/map.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"context"
"reflect"
"slices"
"sort"
Expand Down Expand Up @@ -89,12 +90,15 @@ func (m Map[T]) FilterFn(handler func(key string, value T) bool) error {
return nil
}

func (m Map[T]) Iterate(handler func(key string, value T) error) error {
func (m Map[T]) Iterate(ctx context.Context, handler func(ctx context.Context, key string, value T) error) error {
if len(m) == 0 {
return nil
}
for _, key := range m.Keys() {
if err := handler(key, m[key]); err != nil {
if err := ctx.Err(); err != nil {
return err
}
if err := handler(ctx, key, m[key]); err != nil {
return err
}
}
Expand Down
35 changes: 7 additions & 28 deletions internal/config/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"context"
"fmt"
"path"
"sort"
"strings"

"github.com/foomo/squadron/internal/helm"
"github.com/foomo/squadron/internal/util"
"github.com/pkg/errors"
"github.com/pterm/pterm"
yamlv2 "gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -44,34 +44,13 @@ func (u *Unit) ValuesYAML(global, vars map[string]any) ([]byte, error) {
return yamlv2.Marshal(values)
}

func (u *Unit) Build(ctx context.Context, squadron, unit string, args []string) (string, error) {
var i int
for _, build := range u.Builds {
i++
pterm.Info.Printfln("[%d/%d] Building %s/%s", i, len(u.Builds), squadron, unit)
pterm.FgGray.Printfln("└ %s:%s", build.Image, build.Tag)
if out, err := build.Build(ctx, args); err != nil {
pterm.Error.Printfln("[%d/%d] Failed to build squadron unit %s/%s", i, len(u.Builds), squadron, unit)
pterm.FgGray.Printfln("└ %s:%s", build.Image, build.Tag)
return out, err
}
}
return "", nil
}

func (u *Unit) Push(ctx context.Context, squadron, unit string, args []string) (string, error) {
var i int
for _, build := range u.Builds {
i++
pterm.Info.Printfln("[%d/%d] Pushing %s/%s", i, len(u.Builds), squadron, unit)
pterm.FgGray.Printfln("└ %s:%s", build.Image, build.Tag)
if out, err := build.Push(ctx, args); err != nil {
pterm.Error.Printfln("[%d/%d] Failed to push %s/%s", i, len(u.Builds), squadron, unit)
pterm.FgGray.Printfln("└ %s:%s", build.Image, build.Tag)
return out, err
}
func (u *Unit) BuildNames() []string {
ret := make([]string, 0, len(u.Builds))
for name := range u.Builds {
ret = append(ret, name)
}
return "", nil
sort.Strings(ret)
return ret
}

func (u *Unit) Template(ctx context.Context, name, squadron, unit, namespace string, global, vars map[string]any, helmArgs []string) ([]byte, error) {
Expand Down
21 changes: 21 additions & 0 deletions internal/template/format.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
package template

import (
"fmt"
"strings"
)

func quote(str ...any) string {
out := make([]string, 0, len(str))
for _, s := range str {
if s != nil {
out = append(out, fmt.Sprintf("%v", s))
}
}
return "'" + strings.Join(out, " ") + "'"
}

func quoteAll(str ...any) string {
out := make([]string, 0, len(str))
for _, s := range str {
if s != nil {
out = append(out, fmt.Sprintf("'%v'", s))
}
}
return strings.Join(out, " ")
}

func indent(spaces int, v string) string {
pad := strings.Repeat(" ", spaces)
return strings.ReplaceAll(v, "\n", "\n"+pad)
Expand Down
2 changes: 2 additions & 0 deletions internal/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ func ExecuteFileTemplate(ctx context.Context, text string, templateVars any, err
delete(funcMap, "expandenv")

funcMap["env"] = env
funcMap["quote"] = quote
funcMap["quoteAll"] = quoteAll
funcMap["envDefault"] = envDefault

// deprecated
Expand Down
Loading