Skip to content
This repository was archived by the owner on Jun 29, 2022. It is now read-only.

Commit 151897e

Browse files
authored
Merge pull request #787 from kinvolk/johananl/move-walkers
Move walkers to pkg/assets
2 parents aed5228 + 850cca9 commit 151897e

File tree

7 files changed

+45
-71
lines changed

7 files changed

+45
-71
lines changed

pkg/assets/assets.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// Package assets handles Lokomotive assets. Operations such as storing files as binary data and
16+
// extracting files from memory to disk belong in this package.
1517
package assets
1618

1719
import (
Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,66 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package walkers
15+
package assets
1616

1717
import (
1818
"fmt"
1919
"io"
20+
"io/ioutil"
2021
"os"
2122
"path/filepath"
22-
23-
"github.com/pkg/errors"
24-
25-
"github.com/kinvolk/lokomotive/pkg/assets"
2623
)
2724

28-
func CopyingWalker(path string, newDirPerms os.FileMode) assets.WalkFunc {
25+
// CopyingWalker returns a WalkFunc which writes the given file to disk.
26+
func CopyingWalker(path string, newDirPerms os.FileMode) WalkFunc {
2927
return func(fileName string, fileInfo os.FileInfo, r io.ReadSeeker, err error) error {
3028
if err != nil {
31-
return errors.Wrapf(err, "error during walking at %q", fileName)
29+
return fmt.Errorf("error while walking at %q: %w", fileName, err)
3230
}
3331

3432
fileName = filepath.Join(path, fileName)
3533

3634
if err := os.MkdirAll(filepath.Dir(fileName), newDirPerms); err != nil {
37-
return errors.Wrap(err, "failed to create dir")
35+
return fmt.Errorf("failed to create dir: %w", err)
3836
}
3937

4038
return writeFile(fileName, r)
4139
}
4240
}
4341

42+
// DumpingWalker returns a WalkFunc which sets the contents of the given file in a map.
43+
func DumpingWalker(contentsMap map[string]string, allowedExts ...string) WalkFunc {
44+
var extsMap map[string]struct{}
45+
46+
if len(allowedExts) > 0 {
47+
extsMap = make(map[string]struct{}, len(allowedExts))
48+
for _, ext := range allowedExts {
49+
extsMap[ext] = struct{}{}
50+
}
51+
}
52+
53+
return func(fileName string, fileInfo os.FileInfo, r io.ReadSeeker, err error) error {
54+
if err != nil {
55+
return fmt.Errorf("error while walking at %q: %w", fileName, err)
56+
}
57+
58+
if extsMap != nil {
59+
if _, ok := extsMap[filepath.Ext(fileName)]; !ok {
60+
return nil
61+
}
62+
}
63+
64+
contents, err := ioutil.ReadAll(r)
65+
if err != nil {
66+
return fmt.Errorf("failed to read %q: %w", fileName, err)
67+
}
68+
69+
contentsMap[fileName] = string(contents)
70+
71+
return nil
72+
}
73+
}
74+
4475
// writeFile writes data from given io.Reader to the file and makes sure, that
4576
// this is the only content stored in the file.
4677
func writeFile(p string, r io.Reader) error {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package walkers
15+
package assets
1616

1717
import (
1818
"io/ioutil"

pkg/components/flatcar-linux-update-operator/component.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"github.com/kinvolk/lokomotive/pkg/assets"
2525
"github.com/kinvolk/lokomotive/pkg/components"
2626
"github.com/kinvolk/lokomotive/pkg/k8sutil"
27-
"github.com/kinvolk/lokomotive/pkg/util/walkers"
2827
)
2928

3029
const componentName = "flatcar-linux-update-operator"
@@ -45,7 +44,7 @@ func (c *component) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContex
4544

4645
func (c *component) RenderManifests() (map[string]string, error) {
4746
ret := make(map[string]string)
48-
walk := walkers.DumpingWalker(ret, ".yaml")
47+
walk := assets.DumpingWalker(ret, ".yaml")
4948
if err := assets.Assets.WalkFiles(fmt.Sprintf("/components/%s/manifests", componentName), walk); err != nil {
5049
return nil, errors.Wrap(err, "failed to walk assets")
5150
}

pkg/components/util/helm.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"github.com/kinvolk/lokomotive/pkg/assets"
3333
"github.com/kinvolk/lokomotive/pkg/components"
3434
"github.com/kinvolk/lokomotive/pkg/k8sutil"
35-
"github.com/kinvolk/lokomotive/pkg/util/walkers"
3635
)
3736

3837
// LoadChartFromAssets takes in an asset location and returns a Helm
@@ -46,7 +45,7 @@ func LoadChartFromAssets(location string) (*chart.Chart, error) {
4645

4746
// Rendered files could contain secret data, only allow the
4847
// current user but not others
49-
walk := walkers.CopyingWalker(tmpDir, 0700)
48+
walk := assets.CopyingWalker(tmpDir, 0700)
5049
if err := assets.Assets.WalkFiles(location, walk); err != nil {
5150
return nil, errors.Wrap(err, "walking assets")
5251
}

pkg/install/install.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
"github.com/kinvolk/lokomotive/pkg/assets"
2323
"github.com/kinvolk/lokomotive/pkg/util"
24-
"github.com/kinvolk/lokomotive/pkg/util/walkers"
2524
)
2625

2726
// PrepareTerraformRootDir creates a directory named path including all
@@ -50,7 +49,7 @@ func PrepareTerraformRootDir(path string) error {
5049
// lokoctl binary or from the filesystem, depending on whether the
5150
// LOKOCTL_USE_FS_ASSETS environment variable was specified.
5251
func PrepareLokomotiveTerraformModuleAt(path string) error {
53-
walk := walkers.CopyingWalker(path, 0755)
52+
walk := assets.CopyingWalker(path, 0755)
5453
if err := assets.Assets.WalkFiles("/lokomotive-kubernetes", walk); err != nil {
5554
return errors.Wrap(err, "failed to walk assets")
5655
}

pkg/util/walkers/dumping.go

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)