From 71b491e4671e8c25617470198434db163cb0994e Mon Sep 17 00:00:00 2001 From: Kevin Franklin Kim Date: Wed, 21 May 2025 11:07:19 +0200 Subject: [PATCH] fix: multi status rendering --- cmd/actions/up.go | 48 ++++++++++++++++++++------------- squadron.go | 68 +++++++++++++++++------------------------------ status.go | 8 ++++++ 3 files changed, 62 insertions(+), 62 deletions(-) create mode 100644 status.go diff --git a/cmd/actions/up.go b/cmd/actions/up.go index 0374c56..b2775e2 100644 --- a/cmd/actions/up.go +++ b/cmd/actions/up.go @@ -1,11 +1,11 @@ package actions import ( - "os/user" - "strings" + "os" "github.com/foomo/squadron" - "github.com/foomo/squadron/internal/util" + "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing" "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -50,23 +50,35 @@ func NewUp(c *viper.Viper) *cobra.Command { return err } - username := "unknown" - if value, err := util.NewCommand("git").Args("config", "user.name").Run(cmd.Context()); err == nil { - username = strings.TrimSpace(value) - } else if value, err := user.Current(); err == nil { - username = strings.TrimSpace(value.Name) + status := squadron.Status{ + Squadron: version, + User: "unknown", + } + if wd, err := os.Getwd(); err == nil { + if value := os.Getenv("GIT_DIR"); value != "" { + wd = value + } + if repo, err := git.PlainOpen(wd); err == nil { + if c, err := repo.Config(); err == nil { + status.User = c.User.Name + } + if ref, err := repo.Head(); err == nil { + status.Branch = ref.Name().Short() + status.Commit = ref.Hash().String() + if tags, err := repo.Tags(); err == nil { + _ = tags.ForEach(func(r *plumbing.Reference) error { + if r.Hash() == ref.Hash() { + status.Branch = r.Name().Short() + return errors.New("found tag") + } + return nil + }) + } + } + } } - branch := "" - if value, err := util.NewCommand("sh").Args("-c", "git describe --tags --exact-match 2> /dev/null || git symbolic-ref -q --short HEAD || git rev-parse --short HEAD").Run(cmd.Context()); err == nil { - branch = strings.TrimSpace(value) - } - commit := "" - if value, err := util.NewCommand("sh").Args("-c", "git rev-parse --short HEAD").Run(cmd.Context()); err == nil { - commit = strings.TrimSpace(value) - } - - return sq.Up(cmd.Context(), helmArgs, username, version, commit, branch, c.GetInt("parallel")) + return sq.Up(cmd.Context(), helmArgs, status, c.GetInt("parallel")) }, } flags := cmd.Flags() diff --git a/squadron.go b/squadron.go index 93aaf07..c3be5c6 100644 --- a/squadron.go +++ b/squadron.go @@ -11,6 +11,7 @@ import ( "sort" "strings" "sync" + "time" "github.com/foomo/squadron/internal/config" "github.com/foomo/squadron/internal/jsonschema" @@ -38,13 +39,6 @@ type Squadron struct { c config.Config } -type statusDescription struct { - ManagedBy string `json:"managedBy,omitempty"` - DeployedBy string `json:"deployedBy,omitempty"` - GitCommit string `json:"gitCommit,omitempty"` - GitBranch string `json:"gitBranch,omitempty"` -} - func New(basePath, namespace string, files []string) *Squadron { return &Squadron{ basePath: basePath, @@ -557,7 +551,7 @@ func (sq *Squadron) Diff(ctx context.Context, helmArgs []string, parallel int) ( func (sq *Squadron) Status(ctx context.Context, helmArgs []string, parallel int) error { var m sync.Mutex tbd := pterm.TableData{ - {"Name", "Revision", "Status", "Managed by", "Deployed by", "Commit", "Branch", "Last deployed", "Notes"}, + {"Name", "Revision", "Status", "User", "Branch", "Commit", "Squadron", "Last deployed", "Notes"}, } write := func(b []string) { m.Lock() @@ -575,10 +569,10 @@ func (sq *Squadron) Status(ctx context.Context, helmArgs []string, parallel int) LastDeployed string `json:"last_deployed"` Description string `json:"description"` } `json:"info"` - managedBy string `json:"-"` //nolint:revive - deployedBy string `json:"-"` //nolint:revive - gitCommit string `json:"-"` //nolint:revive - gitBranch string `json:"-"` //nolint:revive + user string `json:"-"` //nolint:revive + branch string `json:"-"` //nolint:revive + commit string `json:"-"` //nolint:revive + squadron string `json:"-"` //nolint:revive } wg, ctx := errgroup.WithContext(ctx) @@ -629,41 +623,31 @@ func (sq *Squadron) Status(ctx context.Context, helmArgs []string, parallel int) } var notes []string - lines := strings.Split(status.Info.Description, "\n") - var statusDescription statusDescription + var statusDescription Status if err := json.Unmarshal([]byte(status.Info.Description), &statusDescription); err == nil { - status.managedBy = statusDescription.ManagedBy - status.deployedBy = statusDescription.DeployedBy - status.gitCommit = statusDescription.GitCommit - status.gitBranch = statusDescription.GitBranch - } else if len(lines) > 1 { - for _, line := range lines { - if strings.HasPrefix(line, "Managed-By: ") { - status.managedBy = strings.TrimPrefix(line, "Managed-By: ") - } else if strings.HasPrefix(line, "Deployed-By: ") { - status.deployedBy = strings.TrimPrefix(line, "Deployed-By: ") - } else if strings.HasPrefix(line, "Git-Commit: ") { - status.gitCommit = strings.TrimPrefix(line, "Git-Commit: ") - } else if strings.HasPrefix(line, "Git-Branch: ") { - status.gitBranch = strings.TrimPrefix(line, "Git-Branch: ") - } else { - notes = append(notes, line) - } - } + status.user = statusDescription.User + status.branch = statusDescription.Branch + status.commit = statusDescription.Commit + status.squadron = statusDescription.Squadron } else { notes = append(notes, status.Info.Description) } + lastDeployed := status.Info.LastDeployed + if t, err := time.Parse(time.RFC3339, status.Info.LastDeployed); err == nil { + lastDeployed = t.Format(time.RFC822) + } + write([]string{ status.Name, fmt.Sprintf("%d", status.Version), status.Info.Status, - status.managedBy, - status.deployedBy, - status.gitCommit, - status.gitBranch, - status.Info.LastDeployed, + status.user, + status.branch, + status.commit, + status.squadron, + lastDeployed, strings.Join(notes, "\n"), }) @@ -678,6 +662,7 @@ func (sq *Squadron) Status(ctx context.Context, helmArgs []string, parallel int) if err := wg.Wait(); err != nil { return err } + printer.Stop() out, err := pterm.DefaultTable.WithHasHeader().WithData(tbd).Srender() if err != nil { @@ -782,13 +767,8 @@ func (sq *Squadron) UpdateLocalDependencies(ctx context.Context, parallel int) e return wg.Wait() } -func (sq *Squadron) Up(ctx context.Context, helmArgs []string, username, version, commit, branch string, parallel int) error { - description, err := json.Marshal(statusDescription{ - ManagedBy: version, - DeployedBy: username, - GitCommit: commit, - GitBranch: branch, - }) +func (sq *Squadron) Up(ctx context.Context, helmArgs []string, status Status, parallel int) error { + description, err := json.Marshal(status) if err != nil { return err } diff --git a/status.go b/status.go new file mode 100644 index 0000000..d237cbc --- /dev/null +++ b/status.go @@ -0,0 +1,8 @@ +package squadron + +type Status struct { + User string `json:"user,omitempty"` + Branch string `json:"branch,omitempty"` + Commit string `json:"commit,omitempty"` + Squadron string `json:"squadron,omitempty"` +}