-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
719 additions
and
516 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// SPDX-FileCopyrightText: 2022 Risk.Ident GmbH <[email protected]> | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
// | ||
// This program is free software: you can redistribute it and/or modify it | ||
// under the terms of the GNU General Public License as published by the | ||
// Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, but WITHOUT | ||
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
// more details. | ||
// | ||
// You should have received a copy of the GNU General Public License along | ||
// with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package patch | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/RiskIdent/jelease/pkg/config" | ||
"github.com/RiskIdent/jelease/pkg/patch/filestore" | ||
"github.com/RiskIdent/jelease/pkg/patch/patches" | ||
) | ||
|
||
// ApplyMany applies a series of patches in sequence using [Apply]. | ||
func ApplyMany(repoDir string, patches []config.PackageRepoPatch, tmplCtx config.TemplateContext) error { | ||
for _, p := range patches { | ||
if err := Apply(repoDir, p, tmplCtx); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Apply applies a single patch to the repository. | ||
func Apply(repoDir string, patch config.PackageRepoPatch, tmplCtx config.TemplateContext) error { | ||
fstore := filestore.NewCached(repoDir) | ||
defer fstore.Close() | ||
switch { | ||
case patch.Regex != nil: | ||
if err := patches.ApplyRegexPatch(fstore, tmplCtx, *patch.Regex); err != nil { | ||
return fmt.Errorf("regex patch: %w", err) | ||
} | ||
case patch.YAML != nil: | ||
if err := patches.ApplyYAMLPatch(fstore, tmplCtx, *patch.YAML); err != nil { | ||
return fmt.Errorf("yaml patch: %w", err) | ||
} | ||
case patch.HelmDepUpdate != nil: | ||
// Flush the store as we need the up-to-date changes on disk | ||
if err := fstore.Flush(); err != nil { | ||
return err | ||
} | ||
if err := patches.ApplyHelmDepUpdatePatch(repoDir, tmplCtx, *patch.HelmDepUpdate); err != nil { | ||
return fmt.Errorf("exec patch: %w", err) | ||
} | ||
default: | ||
return errors.New("missing patch type config") | ||
} | ||
|
||
return fstore.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// SPDX-FileCopyrightText: 2024 Risk.Ident GmbH <[email protected]> | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
// | ||
// This program is free software: you can redistribute it and/or modify it | ||
// under the terms of the GNU General Public License as published by the | ||
// Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, but WITHOUT | ||
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
// more details. | ||
// | ||
// You should have received a copy of the GNU General Public License along | ||
// with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
// Package filestore contains a simple and minimal abstraction over | ||
// OS operations for reading and writing to files. | ||
package filestore | ||
|
||
// FileStore is a minimal abstraction over reading and writing files | ||
// that is used in the patching steps (e.g yaml patch, regex patch, etc). | ||
// | ||
// This abstraction allows us to mock the filesystem during testing, | ||
// as well as adding additional features like caching when you have | ||
// multiple patches on the same file (see [Cached]). | ||
type FileStore interface { | ||
ReadFile(path string) ([]byte, error) | ||
WriteFile(path string, content []byte) error | ||
Close() error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// SPDX-FileCopyrightText: 2024 Risk.Ident GmbH <[email protected]> | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
// | ||
// This program is free software: you can redistribute it and/or modify it | ||
// under the terms of the GNU General Public License as published by the | ||
// Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, but WITHOUT | ||
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
// more details. | ||
// | ||
// You should have received a copy of the GNU General Public License along | ||
// with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package filestore | ||
|
||
import "os" | ||
|
||
func NewTestFileStore(files map[string]string) *TestFileStore { | ||
return &TestFileStore{ | ||
files: files, | ||
} | ||
} | ||
|
||
// TestFileStore is a [FileStore] used during Go unit tests. | ||
// It never touches the OS' underlying file system. | ||
type TestFileStore struct { | ||
files map[string]string | ||
} | ||
|
||
// ensure it implements the interface | ||
var _ FileStore = &TestFileStore{} | ||
|
||
func (s *TestFileStore) ReadFile(path string) ([]byte, error) { | ||
content, ok := s.files[path] | ||
if !ok { | ||
return nil, os.ErrNotExist | ||
} | ||
return []byte(content), nil | ||
} | ||
|
||
func (s *TestFileStore) WriteFile(path string, content []byte) error { | ||
s.files[path] = string(content) | ||
return nil | ||
} | ||
|
||
func (s *TestFileStore) Close() error { | ||
return nil | ||
} |
Oops, something went wrong.