Skip to content

Commit f0b953b

Browse files
authored
Merge pull request #186 from rancher/debug-repo
feat(dev) : specify root repo path using DEV_REPO_ROOT for debugging
2 parents 64180d0 + 92d139e commit f0b953b

File tree

12 files changed

+48
-71
lines changed

12 files changed

+48
-71
lines changed

main.go

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"fmt"
5-
"io/ioutil"
65
"os"
76
"strings"
87

@@ -582,7 +581,7 @@ func validateRepo(c *cli.Context) {
582581
u := chartsScriptOptions.ValidateOptions.UpstreamOptions
583582
branch := chartsScriptOptions.ValidateOptions.Branch
584583
logrus.Infof("Performing upstream validation against repository %s at branch %s", u.URL, branch)
585-
compareGeneratedAssetsResponse, err := validate.CompareGeneratedAssets(repoFs, u, branch, releaseOptions)
584+
compareGeneratedAssetsResponse, err := validate.CompareGeneratedAssets(repoRoot, repoFs, u, branch, releaseOptions)
586585
if err != nil {
587586
logrus.Fatal(err)
588587
}
@@ -634,17 +633,17 @@ func createOrUpdateTemplate(c *cli.Context) {
634633
}
635634

636635
func setupCache(c *cli.Context) error {
637-
return puller.InitRootCache(CacheMode, path.DefaultCachePath)
636+
return puller.InitRootCache(getRepoRoot(), CacheMode, path.DefaultCachePath)
638637
}
639638

640639
func cleanCache(c *cli.Context) {
641-
if err := puller.CleanRootCache(path.DefaultCachePath); err != nil {
640+
if err := puller.CleanRootCache(getRepoRoot(), path.DefaultCachePath); err != nil {
642641
logrus.Fatal(err)
643642
}
644643
}
645644

646645
func parseScriptOptions() *options.ChartsScriptOptions {
647-
configYaml, err := ioutil.ReadFile(defaultChartsScriptOptionsFile)
646+
configYaml, err := os.ReadFile(ChartsScriptOptionsFile)
648647
if err != nil {
649648
logrus.Fatalf("Unable to find configuration file: %s", err)
650649
}
@@ -656,6 +655,13 @@ func parseScriptOptions() *options.ChartsScriptOptions {
656655
}
657656

658657
func getRepoRoot() string {
658+
var repoRoot string
659+
repoRoot = os.Getenv("DEV_REPO_ROOT")
660+
if repoRoot != "" {
661+
logrus.Debugf("Using repo root : %s", repoRoot)
662+
return repoRoot
663+
}
664+
659665
repoRoot, err := os.Getwd()
660666
if err != nil {
661667
logrus.Fatalf("Unable to get current working directory: %s", err)
@@ -697,11 +703,12 @@ func checkImages(c *cli.Context) {
697703
}
698704

699705
func checkRCTagsAndVersions(c *cli.Context) {
706+
repoRoot := getRepoRoot()
700707
// Grab all images that contain RC tags
701-
rcImageTagMap := images.CheckRCTags()
708+
rcImageTagMap := images.CheckRCTags(repoRoot)
702709

703710
// Grab all chart versions that contain RC tags
704-
rcChartVersionMap := charts.CheckRCCharts()
711+
rcChartVersionMap := charts.CheckRCCharts(repoRoot)
705712

706713
// If there are any charts that contains RC version or images that contains RC tags
707714
// log them and return an error
@@ -717,8 +724,9 @@ func checkRCTagsAndVersions(c *cli.Context) {
717724
func lifecycleStatus(c *cli.Context) {
718725
// Initialize dependencies with branch-version and current chart
719726
logrus.Info("Initializing dependencies for lifecycle-status")
720-
rootFs := filesystem.GetFilesystem(getRepoRoot())
721-
lifeCycleDep, err := lifecycle.InitDependencies(rootFs, c.String("branch-version"), CurrentChart)
727+
repoRoot := getRepoRoot()
728+
rootFs := filesystem.GetFilesystem(repoRoot)
729+
lifeCycleDep, err := lifecycle.InitDependencies(repoRoot, rootFs, c.String("branch-version"), CurrentChart)
722730
if err != nil {
723731
logrus.Fatalf("encountered error while initializing dependencies: %s", err)
724732
}
@@ -738,8 +746,9 @@ func autoForwardPort(c *cli.Context) {
738746

739747
// Initialize dependencies with branch-version and current chart
740748
logrus.Info("Initializing dependencies for auto-forward-port")
741-
rootFs := filesystem.GetFilesystem(getRepoRoot())
742-
lifeCycleDep, err := lifecycle.InitDependencies(rootFs, c.String("branch-version"), CurrentChart)
749+
repoRoot := getRepoRoot()
750+
rootFs := filesystem.GetFilesystem(repoRoot)
751+
lifeCycleDep, err := lifecycle.InitDependencies(repoRoot, rootFs, c.String("branch-version"), CurrentChart)
743752
if err != nil {
744753
logrus.Fatalf("encountered error while initializing dependencies: %v", err)
745754
}
@@ -773,10 +782,10 @@ func release(c *cli.Context) {
773782
if CurrentChart == "" {
774783
logrus.Fatal("CHART environment variable must be set to run release cmd")
775784
}
785+
repoRoot := getRepoRoot()
786+
rootFs := filesystem.GetFilesystem(repoRoot)
776787

777-
rootFs := filesystem.GetFilesystem(getRepoRoot())
778-
779-
dependencies, err := lifecycle.InitDependencies(rootFs, c.String("branch-version"), CurrentChart)
788+
dependencies, err := lifecycle.InitDependencies(repoRoot, rootFs, c.String("branch-version"), CurrentChart)
780789
if err != nil {
781790
logrus.Fatalf("encountered error while initializing dependencies: %v", err)
782791
}
@@ -825,15 +834,15 @@ func validateRelease(c *cli.Context) {
825834
fmt.Println("BRANCH environment variable must be set to run validate-release-charts")
826835
os.Exit(1)
827836
}
828-
829-
rootFs := filesystem.GetFilesystem(getRepoRoot())
837+
repoRoot := getRepoRoot()
838+
rootFs := filesystem.GetFilesystem(repoRoot)
830839

831840
if !strings.HasPrefix(Branch, "release-v") {
832841
fmt.Println("Branch must be in the format release-v2.x")
833842
os.Exit(1)
834843
}
835844

836-
dependencies, err := lifecycle.InitDependencies(rootFs, strings.TrimPrefix(Branch, "release-v"), "")
845+
dependencies, err := lifecycle.InitDependencies(repoRoot, rootFs, strings.TrimPrefix(Branch, "release-v"), "")
837846
if err != nil {
838847
fmt.Printf("encountered error while initializing d: %v \n", err)
839848
os.Exit(1)

pkg/auto/chart_bump.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func SetupBump(repoRoot, targetPackage, targetBranch string, chScriptOpts *optio
7777
}
7878

7979
//Initialize the lifecycle dependencies because of the versioning rules and the index.yaml mapping.
80-
dependencies, err := lifecycle.InitDependencies(filesystem.GetFilesystem(repoRoot), branch, bump.targetChart)
80+
dependencies, err := lifecycle.InitDependencies(repoRoot, filesystem.GetFilesystem(repoRoot), branch, bump.targetChart)
8181
if err != nil {
8282
err = fmt.Errorf("failure at SetupBump: %w ", err)
8383
return bump, err

pkg/charts/checkRCCharts.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package charts
22

33
import (
4-
"os"
54
"strings"
65

76
"github.com/rancher/charts-build-scripts/pkg/filesystem"
@@ -10,14 +9,7 @@ import (
109
)
1110

1211
// CheckRCCharts checks for any charts that have RC versions
13-
func CheckRCCharts() map[string][]string {
14-
15-
// Get the current working directory
16-
repoRoot, err := os.Getwd()
17-
if err != nil {
18-
logrus.Fatalf("Unable to get current working directory: %s", err)
19-
}
20-
12+
func CheckRCCharts(repoRoot string) map[string][]string {
2113
// Get the filesystem on the repo root
2214
repoFs := filesystem.GetFilesystem(repoRoot)
2315

pkg/charts/crdchart.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"fmt"
66
"io"
7-
"io/ioutil"
87
"os"
98
"path/filepath"
109
"strings"
@@ -77,7 +76,7 @@ func AddCRDValidationToChart(fs billy.Filesystem, helmChartPathWithoutCRDs, helm
7776
return nil
7877
}
7978
absPath := filesystem.GetAbsPath(fs, path)
80-
yamlFile, err := ioutil.ReadFile(absPath)
79+
yamlFile, err := os.ReadFile(absPath)
8180
if err != nil {
8281
return fmt.Errorf("unable to read file %s: %s", absPath, err)
8382
}
@@ -133,7 +132,7 @@ func AddCRDValidationToChart(fs billy.Filesystem, helmChartPathWithoutCRDs, helm
133132
validateInstallCRDsContents := fmt.Sprintf(ValidateInstallCRDContentsFmt, strings.Join(formattedCRDs, "\n"))
134133
validateInstallCRDsDestpath := filepath.Join(helmChartPathWithoutCRDs, path.ChartValidateInstallCRDFile)
135134
// Write to file
136-
err = ioutil.WriteFile(filesystem.GetAbsPath(fs, validateInstallCRDsDestpath), []byte(validateInstallCRDsContents), os.ModePerm)
135+
err = os.WriteFile(filesystem.GetAbsPath(fs, validateInstallCRDsDestpath), []byte(validateInstallCRDsContents), os.ModePerm)
137136
if err != nil {
138137
return fmt.Errorf("encountered error while writing into %s: %s", validateInstallCRDsDestpath, err)
139138
}

pkg/filesystem/filesystem.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import (
66
"compress/gzip"
77
"crypto/sha1"
88
"fmt"
9-
"github.com/rancher/charts-build-scripts/pkg/util"
109
"io"
11-
"io/ioutil"
1210
"net/http"
1311
"os"
1412
"path/filepath"
1513
"sort"
1614
"strings"
1715

16+
"github.com/rancher/charts-build-scripts/pkg/util"
17+
1818
"github.com/go-git/go-billy/v5"
1919
"github.com/go-git/go-billy/v5/osfs"
2020
"github.com/sirupsen/logrus"
@@ -591,11 +591,11 @@ func CopyDir(fs billy.Filesystem, srcDir string, dstDir string) error {
591591
if isDir {
592592
return fs.MkdirAll(dstPath, os.ModePerm)
593593
}
594-
data, err := ioutil.ReadFile(GetAbsPath(fs, srcPath))
594+
data, err := os.ReadFile(GetAbsPath(fs, srcPath))
595595
if err != nil {
596596
return err
597597
}
598-
return ioutil.WriteFile(GetAbsPath(fs, dstPath), data, os.ModePerm)
598+
return os.WriteFile(GetAbsPath(fs, dstPath), data, os.ModePerm)
599599
})
600600
}
601601

@@ -608,7 +608,7 @@ func MakeSubdirectoryRoot(fs billy.Filesystem, path, subdirectory string) error
608608
if !exists {
609609
return fmt.Errorf("subdirectory %s does not exist in path %s in filesystem %s", subdirectory, path, fs.Root())
610610
}
611-
absTempDir, err := ioutil.TempDir(fs.Root(), "make-subdirectory-root")
611+
absTempDir, err := os.MkdirTemp(fs.Root(), "make-subdirectory-root")
612612
if err != nil {
613613
return err
614614
}

pkg/images/checkRCTags.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package images
22

33
import (
4-
"os"
54
"strings"
65

76
"github.com/rancher/charts-build-scripts/pkg/filesystem"
@@ -11,10 +10,10 @@ import (
1110
)
1211

1312
// CheckRCTags checks for any images that have RC tags
14-
func CheckRCTags() map[string][]string {
13+
func CheckRCTags(repoRoot string) map[string][]string {
1514

1615
// Get the release options from the release.yaml file
17-
releaseOptions := getReleaseOptions()
16+
releaseOptions := getReleaseOptions(repoRoot)
1817

1918
logrus.Infof("Checking for RC tags in charts: %v", releaseOptions)
2019

@@ -41,13 +40,7 @@ func CheckRCTags() map[string][]string {
4140
}
4241

4342
// getReleaseOptions returns the release options from the release.yaml file
44-
func getReleaseOptions() options.ReleaseOptions {
45-
// Get the current working directory
46-
repoRoot, err := os.Getwd()
47-
if err != nil {
48-
logrus.Fatalf("Unable to get current working directory: %s", err)
49-
}
50-
43+
func getReleaseOptions(repoRoot string) options.ReleaseOptions {
5144
// Get the filesystem on the repo root
5245
repoFs := filesystem.GetFilesystem(repoRoot)
5346

pkg/lifecycle/lifecycle.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package lifecycle
22

33
import (
44
"errors"
5-
"os"
65

76
"github.com/go-git/go-billy/v5"
87
"github.com/rancher/charts-build-scripts/pkg/filesystem"
@@ -37,13 +36,10 @@ type WalkDirFunc func(fs billy.Filesystem, dirPath string, doFunc filesystem.Rel
3736
// InitDependencies will check the filesystem, branch version,
3837
// git status, initialize the Dependencies struct and populate it.
3938
// If anything fails the operation will be aborted.
40-
func InitDependencies(rootFs billy.Filesystem, branchVersion string, currentChart string) (*Dependencies, error) {
39+
func InitDependencies(repoRoot string, rootFs billy.Filesystem, branchVersion string, currentChart string) (*Dependencies, error) {
4140
var err error
4241

43-
workDir, err := os.Getwd()
44-
if err != nil {
45-
return nil, err
46-
}
42+
workDir := repoRoot
4743

4844
git, err := git.OpenGitRepo(workDir)
4945
if err != nil {

pkg/options/package.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package options
22

33
import (
44
"fmt"
5-
"io/ioutil"
65
"os"
76

87
"github.com/go-git/go-billy/v5"
@@ -105,7 +104,7 @@ func LoadPackageOptionsFromFile(fs billy.Filesystem, path string) (PackageOption
105104
if !exists {
106105
return packageOptions, fmt.Errorf("unable to load package options from file %s since it does not exist", filesystem.GetAbsPath(fs, path))
107106
}
108-
chartOptionsBytes, err := ioutil.ReadFile(filesystem.GetAbsPath(fs, path))
107+
chartOptionsBytes, err := os.ReadFile(filesystem.GetAbsPath(fs, path))
109108
if err != nil {
110109
return packageOptions, err
111110
}
@@ -146,7 +145,7 @@ func LoadChartOptionsFromFile(fs billy.Filesystem, path string) (ChartOptions, e
146145
if !exists {
147146
return chartOptions, fmt.Errorf("unable to load chart options from file %s since it does not exist", filesystem.GetAbsPath(fs, path))
148147
}
149-
chartOptionsBytes, err := ioutil.ReadFile(filesystem.GetAbsPath(fs, path))
148+
chartOptionsBytes, err := os.ReadFile(filesystem.GetAbsPath(fs, path))
150149
if err != nil {
151150
return chartOptions, err
152151
}

pkg/options/validate.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package options
22

33
import (
4-
"io/ioutil"
54
"os"
65

76
"github.com/hashicorp/go-version"
@@ -78,7 +77,7 @@ func LoadReleaseOptionsFromFile(fs billy.Filesystem, path string) (ReleaseOption
7877
// If release.yaml does not exist, return an empty ReleaseOptions
7978
return releaseOptions, nil
8079
}
81-
releaseOptionsBytes, err := ioutil.ReadFile(filesystem.GetAbsPath(fs, path))
80+
releaseOptionsBytes, err := os.ReadFile(filesystem.GetAbsPath(fs, path))
8281
if err != nil {
8382
return releaseOptions, err
8483
}

pkg/puller/cache.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,12 @@ import (
1313
var RootCache cacher = &noopCache{}
1414

1515
// InitRootCache initializes a cache at the repository's root to be used, if it does not currently exist
16-
func InitRootCache(cacheMode bool, path string) error {
16+
func InitRootCache(repoRoot string, cacheMode bool, path string) error {
1717
if !cacheMode {
1818
return nil
1919
}
2020
logrus.Infof("Setting up cache at %s", path)
2121
// Get repository filesystem
22-
repoRoot, err := os.Getwd()
23-
if err != nil {
24-
return fmt.Errorf("unable to get current working directory: %s", err)
25-
}
2622
rootFs := filesystem.GetFilesystem(repoRoot)
2723

2824
// Instantiate cache
@@ -41,12 +37,7 @@ func InitRootCache(cacheMode bool, path string) error {
4137
}
4238

4339
// CleanRootCache removes any existing entries in the cache
44-
func CleanRootCache(path string) error {
45-
// Get repository filesystem
46-
repoRoot, err := os.Getwd()
47-
if err != nil {
48-
logrus.Fatalf("Unable to get current working directory: %s", err)
49-
}
40+
func CleanRootCache(repoRoot string, path string) error {
5041
rootFs := filesystem.GetFilesystem(repoRoot)
5142
if err := filesystem.RemoveAll(rootFs, path); err != nil {
5243
return err

0 commit comments

Comments
 (0)