fix: multi status rendering

This commit is contained in:
Kevin Franklin Kim 2025-05-21 11:07:19 +02:00
parent ceae36d48d
commit 71b491e467
No known key found for this signature in database
3 changed files with 62 additions and 62 deletions

View File

@ -1,11 +1,11 @@
package actions package actions
import ( import (
"os/user" "os"
"strings"
"github.com/foomo/squadron" "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/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -50,23 +50,35 @@ func NewUp(c *viper.Viper) *cobra.Command {
return err return err
} }
username := "unknown" status := squadron.Status{
if value, err := util.NewCommand("git").Args("config", "user.name").Run(cmd.Context()); err == nil { Squadron: version,
username = strings.TrimSpace(value) User: "unknown",
} else if value, err := user.Current(); err == nil { }
username = strings.TrimSpace(value.Name) 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 := "" return sq.Up(cmd.Context(), helmArgs, status, c.GetInt("parallel"))
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"))
}, },
} }
flags := cmd.Flags() flags := cmd.Flags()

View File

@ -11,6 +11,7 @@ import (
"sort" "sort"
"strings" "strings"
"sync" "sync"
"time"
"github.com/foomo/squadron/internal/config" "github.com/foomo/squadron/internal/config"
"github.com/foomo/squadron/internal/jsonschema" "github.com/foomo/squadron/internal/jsonschema"
@ -38,13 +39,6 @@ type Squadron struct {
c config.Config 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 { func New(basePath, namespace string, files []string) *Squadron {
return &Squadron{ return &Squadron{
basePath: basePath, 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 { func (sq *Squadron) Status(ctx context.Context, helmArgs []string, parallel int) error {
var m sync.Mutex var m sync.Mutex
tbd := pterm.TableData{ 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) { write := func(b []string) {
m.Lock() m.Lock()
@ -575,10 +569,10 @@ func (sq *Squadron) Status(ctx context.Context, helmArgs []string, parallel int)
LastDeployed string `json:"last_deployed"` LastDeployed string `json:"last_deployed"`
Description string `json:"description"` Description string `json:"description"`
} `json:"info"` } `json:"info"`
managedBy string `json:"-"` //nolint:revive user string `json:"-"` //nolint:revive
deployedBy string `json:"-"` //nolint:revive branch string `json:"-"` //nolint:revive
gitCommit string `json:"-"` //nolint:revive commit string `json:"-"` //nolint:revive
gitBranch string `json:"-"` //nolint:revive squadron string `json:"-"` //nolint:revive
} }
wg, ctx := errgroup.WithContext(ctx) wg, ctx := errgroup.WithContext(ctx)
@ -629,41 +623,31 @@ func (sq *Squadron) Status(ctx context.Context, helmArgs []string, parallel int)
} }
var notes []string var notes []string
lines := strings.Split(status.Info.Description, "\n") var statusDescription Status
var statusDescription statusDescription
if err := json.Unmarshal([]byte(status.Info.Description), &statusDescription); err == nil { if err := json.Unmarshal([]byte(status.Info.Description), &statusDescription); err == nil {
status.managedBy = statusDescription.ManagedBy status.user = statusDescription.User
status.deployedBy = statusDescription.DeployedBy status.branch = statusDescription.Branch
status.gitCommit = statusDescription.GitCommit status.commit = statusDescription.Commit
status.gitBranch = statusDescription.GitBranch status.squadron = statusDescription.Squadron
} 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)
}
}
} else { } else {
notes = append(notes, status.Info.Description) 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{ write([]string{
status.Name, status.Name,
fmt.Sprintf("%d", status.Version), fmt.Sprintf("%d", status.Version),
status.Info.Status, status.Info.Status,
status.managedBy, status.user,
status.deployedBy, status.branch,
status.gitCommit, status.commit,
status.gitBranch, status.squadron,
status.Info.LastDeployed, lastDeployed,
strings.Join(notes, "\n"), 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 { if err := wg.Wait(); err != nil {
return err return err
} }
printer.Stop()
out, err := pterm.DefaultTable.WithHasHeader().WithData(tbd).Srender() out, err := pterm.DefaultTable.WithHasHeader().WithData(tbd).Srender()
if err != nil { if err != nil {
@ -782,13 +767,8 @@ func (sq *Squadron) UpdateLocalDependencies(ctx context.Context, parallel int) e
return wg.Wait() return wg.Wait()
} }
func (sq *Squadron) Up(ctx context.Context, helmArgs []string, username, version, commit, branch string, parallel int) error { func (sq *Squadron) Up(ctx context.Context, helmArgs []string, status Status, parallel int) error {
description, err := json.Marshal(statusDescription{ description, err := json.Marshal(status)
ManagedBy: version,
DeployedBy: username,
GitCommit: commit,
GitBranch: branch,
})
if err != nil { if err != nil {
return err return err
} }

8
status.go Normal file
View File

@ -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"`
}