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: add a switch to control the auto-updating for mod file #566

Merged
merged 6 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 1 addition & 4 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,6 @@ func testMetadataOffline(t *testing.T) {
testDir := getTestDir("test_metadata_offline")
kclMod := filepath.Join(testDir, "kcl.mod")
uglyKclMod := filepath.Join(testDir, "ugly.kcl.mod")
BeautifulKclMod := filepath.Join(testDir, "beautiful.kcl.mod")

uglyContent, err := os.ReadFile(uglyKclMod)
assert.Equal(t, err, nil)
Expand All @@ -1157,8 +1156,6 @@ func testMetadataOffline(t *testing.T) {
assert.Equal(t, err, nil)
}()

beautifulContent, err := os.ReadFile(BeautifulKclMod)
assert.Equal(t, err, nil)
kclPkg, err := pkg.LoadKclPkg(testDir)
assert.Equal(t, err, nil)

Expand All @@ -1178,7 +1175,7 @@ func testMetadataOffline(t *testing.T) {
assert.Equal(t, res, "{\"packages\":{}}")
content_after_metadata, err = os.ReadFile(kclMod)
assert.Equal(t, err, nil)
assert.Equal(t, utils.RmNewline(string(content_after_metadata)), utils.RmNewline(string(beautifulContent)))
assert.Equal(t, utils.RmNewline(string(content_after_metadata)), utils.RmNewline(string(uglyContent)))
}

func testAddWithNoSumCheck(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions pkg/client/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func (c *KpmClient) ResolvePkgDepsMetadata(kclPkg *pkg.KclPkg, update bool) erro
_, err = c.Update(
WithUpdatedKclPkg(kclPkg),
WithOffline(!update),
WithUpdateModFile(false),
)
}
return err
Expand Down
17 changes: 13 additions & 4 deletions pkg/client/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,21 @@ import (
// Updating a package means iterating all the dependencies of the package
// and updating the dependencies and selecting the version of the dependencies by MVS.
type UpdateOptions struct {
kpkg *pkg.KclPkg
offline bool
kpkg *pkg.KclPkg
offline bool
updateModFile bool
}

type UpdateOption func(*UpdateOptions) error

// WithUpdateModFile sets the flag to update the mod file.
func WithUpdateModFile(updateModFile bool) UpdateOption {
return func(opts *UpdateOptions) error {
opts.updateModFile = updateModFile
return nil
}
}

// WithOffline sets the offline option to update the package.
func WithOffline(offline bool) UpdateOption {
return func(opts *UpdateOptions) error {
Expand All @@ -43,7 +52,7 @@ func WithUpdatedKclPkg(kpkg *pkg.KclPkg) UpdateOption {
}

func (c *KpmClient) Update(options ...UpdateOption) (*pkg.KclPkg, error) {
opts := &UpdateOptions{}
opts := &UpdateOptions{updateModFile: true}
for _, option := range options {
if err := option(opts); err != nil {
return nil, err
Expand Down Expand Up @@ -143,7 +152,7 @@ func (c *KpmClient) Update(options ...UpdateOption) (*pkg.KclPkg, error) {
return nil, err
}

if !opts.offline {
if opts.updateModFile {
err = kMod.UpdateModAndLockFile()
if err != nil {
return nil, err
Expand Down
Loading