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: use json description #75

Merged
merged 3 commits into from
Oct 17, 2024
Merged
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
61 changes: 46 additions & 15 deletions squadron.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,10 +469,17 @@ func (sq *Squadron) Diff(ctx context.Context, helmArgs []string, parallel int) e
return wg.Wait()
}

type statusDescription struct {
ManagedBy string `json:"managedBy,omitempty"`
DeployedBy string `json:"deployedBy,omitempty"`
GitCommit string `json:"gitCommit,omitempty"`
GitBranch string `json:"gitBranch,omitempty"`
}

func (sq *Squadron) Status(ctx context.Context, helmArgs []string, parallel int) error {
var m sync.Mutex
tbd := pterm.TableData{
{"Name", "Revision", "Status", "Deployed by", "Commit", "Branch", "Last deployed", "Notes"},
{"Name", "Revision", "Status", "Managed by", "Deployed by", "Commit", "Branch", "Last deployed", "Notes"},
}
write := func(b []string) {
m.Lock()
Expand All @@ -490,6 +497,7 @@ 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
Expand Down Expand Up @@ -520,27 +528,42 @@ func (sq *Squadron) Status(ctx context.Context, helmArgs []string, parallel int)
return errors.Wrap(err, out)
} else {
var notes []string
for _, line := range strings.Split(status.Info.Description, "\n") {
if strings.HasPrefix(line, "Managed-By: ") {
// do nothing
} 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)
lines := strings.Split(status.Info.Description, "\n")
var statusDescription statusDescription

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)
}
}
} else {
notes = append(notes, status.Info.Description)
}

write([]string{
status.Name,
fmt.Sprintf("%d", status.Version),
status.Info.Status,
status.managedBy,
status.deployedBy,
status.gitCommit,
status.gitBranch,
status.Info.LastDeployed, strings.Join(notes, " | "),
status.Info.LastDeployed,
strings.Join(notes, "\n"),
})
}
return nil
Expand Down Expand Up @@ -628,7 +651,15 @@ func (sq *Squadron) UpdateLocalDependencies(ctx context.Context, parallel int) e
}

func (sq *Squadron) Up(ctx context.Context, helmArgs []string, username, version, commit, branch string, parallel int) error {
description := fmt.Sprintf("\nDeployed-By: %s\nManaged-By: Squadron %s\nGit-Commit: %s\nGit-Branch: %s", username, version, commit, branch)
description, err := json.Marshal(statusDescription{
ManagedBy: version,
DeployedBy: username,
GitCommit: commit,
GitBranch: branch,
})
if err != nil {
return err
}

wg, ctx := errgroup.WithContext(ctx)
wg.SetLimit(parallel)
Expand Down Expand Up @@ -657,7 +688,7 @@ func (sq *Squadron) Up(ctx context.Context, helmArgs []string, username, version
Stdout(os.Stdout).
Args("upgrade", name, "--install").
Args("--set", fmt.Sprintf("squadron=%s,unit=%s", key, k)).
Args("--description", description).
Args("--description", string(description)).
Args("--namespace", namespace).
Args("--dependency-update").
Args(v.PostRendererArgs()...).
Expand Down