Skip to content

Commit 4d4e400

Browse files
committed
wolfictl bump : add a extra flag increment to reverse the bumps if
necessary
1 parent 591289d commit 4d4e400

File tree

2 files changed

+121
-6
lines changed

2 files changed

+121
-6
lines changed

pkg/cli/bump.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ import (
1515
const epochPattern = `epoch: %d`
1616

1717
type bumpOptions struct {
18-
repoDir string
19-
epoch bool
20-
dryRun bool
18+
repoDir string
19+
epoch bool
20+
dryRun bool
21+
increment bool
2122
}
2223

2324
// this feels very hacky but the Makefile is going away with help from Dag so plan to delete this func soon
@@ -72,6 +73,14 @@ the version in each matching configuration file:
7273
wolfictl bump openssl
7374
wolfictl bump lib*.yaml
7475
76+
wolfictl bump can take a filename, a package or a file glob,
77+
and extra flag increment set to false , decreasing the version in each
78+
matching configuration file:
79+
80+
wolfictl bump --increment=false zlib.yaml
81+
wolfictl bump --increment=false openssl
82+
wolfictl bump --increment=false lib*.yaml
83+
7584
The command assumes it is being run from the top of the wolfi/os
7685
repository. To look for files in another location use the --repo flag.
7786
You can use --dry-run to see which versions will be bumped without
@@ -120,6 +129,7 @@ modifying anything in the filesystem.
120129
cmd.Flags().BoolVar(&opts.epoch, "epoch", true, "bump the package epoch")
121130
cmd.Flags().BoolVar(&opts.dryRun, "dry-run", false, "don't change anything, just print what would be done")
122131
cmd.Flags().StringVar(&opts.repoDir, "repo", ".", "path to the wolfi/os repository")
132+
cmd.Flags().BoolVar(&opts.increment, "increment", true, "increments/decrements the package epoch")
123133

124134
return cmd
125135
}
@@ -130,9 +140,14 @@ func bumpEpoch(ctx context.Context, opts bumpOptions, path string) error {
130140
return fmt.Errorf("unable to parse configuration at %q: %w", path, err)
131141
}
132142

143+
newEpoch := cfg.Package.Epoch + 1
144+
if !opts.increment {
145+
newEpoch = cfg.Package.Epoch - 1
146+
}
147+
133148
fmt.Fprintf(
134149
os.Stderr, "bumping %s-%s-%d in %s to epoch %d\n", cfg.Package.Name,
135-
cfg.Package.Version, cfg.Package.Epoch, path, cfg.Package.Epoch+1,
150+
cfg.Package.Version, cfg.Package.Epoch, path, newEpoch,
136151
)
137152

138153
if opts.dryRun {
@@ -154,7 +169,7 @@ func bumpEpoch(ctx context.Context, opts bumpOptions, path string) error {
154169
found = true
155170
newFile = append(
156171
newFile, strings.ReplaceAll(
157-
scanner.Text(), old, fmt.Sprintf(epochPattern, cfg.Package.Epoch+1),
172+
scanner.Text(), old, fmt.Sprintf(epochPattern, newEpoch),
158173
),
159174
)
160175
} else {
@@ -173,7 +188,7 @@ func bumpEpoch(ctx context.Context, opts bumpOptions, path string) error {
173188
return fmt.Errorf("writing %s: %w", path, err)
174189
}
175190

176-
if err := updateMakefile(opts.repoDir, cfg.Package.Name, cfg.Package.Version, cfg.Package.Epoch+1); err != nil {
191+
if err := updateMakefile(opts.repoDir, cfg.Package.Name, cfg.Package.Version, newEpoch); err != nil {
177192
return fmt.Errorf("updating makefile: %w", err)
178193
}
179194

pkg/cli/bump_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package cli
2+
3+
import (
4+
"context"
5+
"os"
6+
"path/filepath"
7+
"testing"
8+
9+
"gopkg.in/yaml.v3"
10+
)
11+
12+
func TestBumpEpoch(t *testing.T) {
13+
ctx := context.Background()
14+
15+
// Create a temporary directory for testing
16+
tmpDir, err := os.MkdirTemp("", "test")
17+
if err != nil {
18+
t.Fatalf("Error creating temp directory: %v", err)
19+
}
20+
defer os.RemoveAll(tmpDir)
21+
22+
// Create a temporary config file with a known epoch
23+
configFilePath := filepath.Join(tmpDir, "test_config.yaml")
24+
configContent := `
25+
package:
26+
name: clickhouse
27+
version: 24.2.1.2248
28+
epoch: 6
29+
`
30+
err = os.WriteFile(configFilePath, []byte(configContent), 0644)
31+
if err != nil {
32+
t.Fatalf("Error creating temp config file: %v", err)
33+
}
34+
35+
tests := []struct {
36+
desc string
37+
increment bool
38+
expectedEpoch int
39+
expectedMessage string
40+
}{
41+
{
42+
desc: "Incrementing epoch",
43+
increment: true,
44+
expectedEpoch: 7,
45+
expectedMessage: "bumping clickhouse-24.2.1.2248-0 in " + configFilePath +
46+
" to epoch 7\n",
47+
},
48+
{
49+
desc: "Decrementing epoch",
50+
increment: false,
51+
expectedEpoch: 6,
52+
expectedMessage: "bumping clickhouse-24.2.1.2248-0 in " + configFilePath +
53+
" to epoch 6\n",
54+
},
55+
{
56+
desc: "Decrementing epoch",
57+
increment: false,
58+
expectedEpoch: 5,
59+
expectedMessage: "bumping clickhouse-24.2.1.2248-0 in " + configFilePath +
60+
" to epoch 5\n",
61+
},
62+
}
63+
64+
for _, tc := range tests {
65+
t.Run(tc.desc, func(t *testing.T) {
66+
opts := bumpOptions{
67+
repoDir: tmpDir,
68+
dryRun: false,
69+
increment: tc.increment,
70+
}
71+
72+
// Test bumping epoch
73+
err := bumpEpoch(ctx, opts, configFilePath)
74+
if err != nil {
75+
t.Fatalf("Error bumping epoch: %v", err)
76+
}
77+
78+
// Read the modified config file to check if the epoch is bumped
79+
modifiedConfigData, err := os.ReadFile(configFilePath)
80+
if err != nil {
81+
t.Fatalf("Error reading modified config file: %v", err)
82+
}
83+
84+
var modifiedConfig map[string]interface{}
85+
err = yaml.Unmarshal(modifiedConfigData, &modifiedConfig)
86+
if err != nil {
87+
t.Fatalf("Error unmarshalling modified config: %v", err)
88+
}
89+
90+
actualEpoch, ok := modifiedConfig["package"].(map[string]interface{})["epoch"].(int)
91+
if !ok {
92+
t.Fatalf("Error retrieving actual epoch from modified config")
93+
}
94+
95+
if actualEpoch != tc.expectedEpoch {
96+
t.Errorf("Epoch not bumped correctly. Expected: %d, Got: %d", tc.expectedEpoch, actualEpoch)
97+
}
98+
})
99+
}
100+
}

0 commit comments

Comments
 (0)