Skip to content

Commit 9cd4563

Browse files
authored
Merge pull request #134 from nicholasSUSE/lifecycle-status
Add feature lifecycle-status
2 parents 2812b45 + cb2bf5e commit 9cd4563

File tree

6 files changed

+591
-1
lines changed

6 files changed

+591
-1
lines changed

.github/workflows/pull-request.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ jobs:
2424
- uses: golangci/golangci-lint-action@v4
2525
with:
2626
version: v1.57
27+
args: --timeout 5m
2728

2829
build:
2930
runs-on: ubuntu-latest

main.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,12 @@ func main() {
257257
Action: lifecycleAssetsClean,
258258
Flags: []cli.Flag{branchVersionFlag, chartFlag, debugFlag},
259259
},
260+
{
261+
Name: "lifecycle-status",
262+
Usage: "Get the status of the current assets and charts based on the branch version and chart version according to the lifecycle rules",
263+
Action: lifecycleStatus,
264+
Flags: []cli.Flag{branchVersionFlag, chartFlag},
265+
},
260266
}
261267

262268
if err := app.Run(os.Args); err != nil {
@@ -571,3 +577,18 @@ func lifecycleAssetsClean(c *cli.Context) {
571577
logrus.Fatalf("Failed to apply versioning rules for lifecycle-assets-clean: %s", err)
572578
}
573579
}
580+
581+
func lifecycleStatus(c *cli.Context) {
582+
// Initialize dependencies with branch-version and current chart
583+
rootFs := filesystem.GetFilesystem(getRepoRoot())
584+
lifeCycleDep, err := lifecycle.InitDependencies(rootFs, c.String("branch-version"), CurrentChart, false)
585+
if err != nil {
586+
logrus.Fatalf("encountered error while initializing dependencies for lifecycle-assets-clean: %s", err)
587+
}
588+
589+
// Execute lifecycle status check and save the logs
590+
err = lifeCycleDep.CheckLifecycleStatusAndSave(CurrentChart)
591+
if err != nil {
592+
logrus.Fatalf("Failed to check lifecycle status: %s", err)
593+
}
594+
}

pkg/lifecycle/git.go

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,62 @@ import (
44
"fmt"
55
"os"
66
"os/exec"
7+
8+
"github.com/sirupsen/logrus"
79
)
810

11+
// Git struct holds necessary data to work with the current git repository
12+
type Git struct {
13+
Dir string
14+
}
15+
16+
// cloneAtDir clones a repository at a given directory
17+
func cloneAtDir(url, dir string) (*Git, error) {
18+
logrus.Infof("Cloning repository %s into %s", url, dir)
19+
cmd := exec.Command("git", "clone", "--depth", "1", url, dir)
20+
cmd.Stdout = os.Stdout
21+
cmd.Stderr = os.Stderr
22+
if err := cmd.Run(); err != nil {
23+
logrus.Errorf("error while cloning repository: %s; err: %v", url, err)
24+
return nil, fmt.Errorf("error while cloning repository: %s", err)
25+
}
26+
return &Git{Dir: dir}, nil
27+
}
28+
29+
// fetchAndCheckoutBranch fetches and checks out a branch
30+
func (g *Git) fetchAndCheckoutBranch(branch string) error {
31+
logrus.Infof("Fetching and checking out at: %s", branch)
32+
err := g.fetchBranch(branch)
33+
if err != nil {
34+
return err
35+
}
36+
return g.checkoutBranch(branch)
37+
}
38+
39+
func (g *Git) fetchBranch(branch string) error {
40+
cmd := exec.Command("git", "-C", g.Dir, "fetch", "origin", branch+":"+branch)
41+
cmd.Stdout = os.Stdout
42+
cmd.Stderr = os.Stderr
43+
if err := cmd.Run(); err != nil {
44+
logrus.Errorf("error while fetching branch: %s; err: %v", branch, err)
45+
return fmt.Errorf("error while fetching branch: %s", err)
46+
}
47+
return nil
48+
}
49+
50+
func (g *Git) checkoutBranch(branch string) error {
51+
cmd := exec.Command("git", "-C", g.Dir, "checkout", branch)
52+
cmd.Stdout = os.Stdout
53+
cmd.Stderr = os.Stderr
54+
if err := cmd.Run(); err != nil {
55+
logrus.Errorf("error while checking out branch: %s; err: %v", branch, err)
56+
return fmt.Errorf("error while checking out branch: %s", err)
57+
}
58+
return nil
59+
}
60+
61+
// checkIfGitIsClean checks if the git repository is clean and,
62+
// returns true if it is clean, false otherwise
963
func checkIfGitIsClean(debug bool) (bool, error) {
1064
cmd := exec.Command("git", "status", "--porcelain")
1165
if debug {
@@ -24,8 +78,9 @@ func checkIfGitIsClean(debug bool) (bool, error) {
2478
return true, nil
2579
}
2680

81+
// gitAddAndCommit stages all changes and commits them with a given message,
82+
// equivalent to: git add -A && git commit -m message
2783
func gitAddAndCommit(message string) error {
28-
2984
// Stage all changes, including deletions
3085
cmd := exec.Command("git", "add", "-A")
3186
if err := cmd.Run(); err != nil {

pkg/lifecycle/logs.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package lifecycle
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"os"
7+
"time"
8+
9+
"github.com/sirupsen/logrus"
10+
)
11+
12+
type logs struct {
13+
file *os.File
14+
filePath string
15+
}
16+
17+
const separator = "\n"
18+
const ender = "\n__________________________________________________________________\n"
19+
20+
// createLogs creates a new log file and returns a logs struct with the file and file path
21+
func createLogs(fileName string) (*logs, error) {
22+
filePath := fmt.Sprintf("logs/%s", fileName)
23+
24+
// Create the logs directory if it doesn't exist
25+
err := os.MkdirAll("logs", 0755)
26+
if err != nil {
27+
logrus.Errorf("Error while creating logs directory: %s", err)
28+
}
29+
30+
// Open the file in append mode
31+
file, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
32+
if err != nil {
33+
return nil, err
34+
}
35+
36+
return &logs{file: file, filePath: filePath}, nil
37+
}
38+
39+
// writeHEAD writes the header of the log file with the version rules, dates and necessary information
40+
// to help analyze the current situation on the charts versions regarding the release process.
41+
func (l *logs) writeHEAD(versionRules *VersionRules, title string) {
42+
l.write(title, "INFO")
43+
currentTime := time.Now()
44+
l.write(currentTime.Format("2006-01-02 15:04:05"), "INFO")
45+
l.write(fmt.Sprintf("Branch Version: %.1f", versionRules.branchVersion), "INFO")
46+
l.write(fmt.Sprintf("minimal version: %d", versionRules.minVersion), "INFO")
47+
l.write(fmt.Sprintf("max version: %d", versionRules.maxVersion), "INFO")
48+
l.write(fmt.Sprintf("development branch: %s", versionRules.devBranch), "INFO")
49+
l.write(fmt.Sprintf("production branch: %s", versionRules.prodBranch), "INFO")
50+
51+
rules := make(map[string]string, len(versionRules.rules))
52+
for k, v := range versionRules.rules {
53+
rules[fmt.Sprintf("%.1f", k)] = fmt.Sprintf("min: %s, max: %s", v.min, v.max)
54+
}
55+
56+
rulesJSON, err := json.MarshalIndent(rules, "", " ")
57+
if err != nil {
58+
logrus.Errorf("JSON marshaling failed: %s", err)
59+
l.write(fmt.Sprintf("rules: %v\n", versionRules.rules), "INFO")
60+
} else {
61+
l.write(fmt.Sprintf("rules: %s\n", rulesJSON), "INFO")
62+
}
63+
}
64+
65+
// write writes the data to the log file and prints it to the console with customizations.
66+
func (l *logs) write(data string, logType string) {
67+
switch logType {
68+
case "INFO":
69+
logrus.Info(data)
70+
if _, err := l.file.WriteString("INFO=" + data + "\n"); err != nil {
71+
logrus.Errorf("Error while writing logs: %s", err)
72+
}
73+
case "WARN":
74+
logrus.Warn(data)
75+
if _, err := l.file.WriteString("WARN=" + data + "\n"); err != nil {
76+
logrus.Errorf("Error while writing logs: %s", err)
77+
}
78+
case "ERROR":
79+
logrus.Error(data)
80+
if _, err := l.file.WriteString("ERROR=" + data + "\n"); err != nil {
81+
logrus.Errorf("Error while writing logs: %s", err)
82+
}
83+
case "SEPARATE":
84+
fmt.Printf(separator)
85+
if _, err := l.file.WriteString(separator); err != nil {
86+
logrus.Errorf("Error while writing logs: %s", err)
87+
}
88+
case "END":
89+
fmt.Printf(ender)
90+
if _, err := l.file.WriteString("\n" + ender + "\n"); err != nil {
91+
logrus.Errorf("Error while writing logs: %s", err)
92+
}
93+
default:
94+
fmt.Printf(data)
95+
if _, err := l.file.WriteString(data + "\n"); err != nil {
96+
logrus.Errorf("Error while writing logs: %s", err)
97+
}
98+
}
99+
}
100+
101+
// writeVersions receives the loaded assets versions map and writes it to the log file
102+
// in human-readable format
103+
func (l *logs) writeVersions(assetsVersions map[string][]Asset, logType string) {
104+
for asset, versions := range assetsVersions {
105+
l.write("", "SEPARATE")
106+
l.write(asset, logType)
107+
for _, version := range versions {
108+
l.write(version.version, "")
109+
}
110+
}
111+
}

0 commit comments

Comments
 (0)