@@ -19,13 +19,27 @@ package assets
1919import (
2020 "fmt"
2121 "io"
22- "net/http"
2322 "os"
24- "time"
2523
26- "github.com/prometheus/alertmanager/pkg/modtimevfs"
27- "github.com/shurcooL/httpfs/union"
28- "github.com/shurcooL/vfsgen"
24+ // Currently pkg/assets/assets_generate.go file is ignored from regular
25+ // builds and it's only used when running 'go generate'.
26+ //
27+ // Because of that, external dependencies of assets_generate.go are not
28+ // normally added to go.mod and go.sum files, unless you run 'go generate'.
29+ // But then 'go mod tidy' removes them again.
30+ //
31+ // Those anonymous imports ensure, that those external dependencies
32+ // will always be pulled into go.mod file.
33+ //
34+ // The alternative would be to refactor the function to actually use the
35+ // dependencies here, but then we have an exported function, which
36+ // shouldn't really be exported.
37+ //
38+ // Also see https://github.com/golang/go/issues/25922 and https://github.com/golang/go/issues/29516
39+ // for more details about this issue.
40+ _ "github.com/prometheus/alertmanager/pkg/modtimevfs"
41+ _ "github.com/shurcooL/httpfs/union"
42+ _ "github.com/shurcooL/vfsgen"
2943)
3044
3145const (
@@ -37,46 +51,24 @@ const (
3751 TerraformModulesSource = "/terraform-modules"
3852)
3953
40- type WalkFunc func (fileName string , fileInfo os.FileInfo , r io.ReadSeeker , err error ) error
54+ type walkFunc func (fileName string , fileInfo os.FileInfo , r io.ReadSeeker , err error ) error
4155
42- type AssetsIface interface {
56+ type assetsIface interface {
4357 // WalkFiles calls cb for every regular file within path.
4458 //
4559 // Usually, fileName passed to the cb will be relative to
4660 // path. But in case of error, it is possible that it will
4761 // not.be relative. Also, in case of error, fileInfo or r may
4862 // be nil.
49- WalkFiles (path string , cb WalkFunc ) error
63+ WalkFiles (path string , cb walkFunc ) error
5064}
5165
52- var Assets AssetsIface
53-
54- func init () {
55- Assets = newEmbeddedAssets ()
66+ func get () assetsIface {
5667 if value , found := os .LookupEnv ("LOKOCTL_USE_FS_ASSETS" ); found {
57- Assets = newFsAssets (value )
68+ return newFsAssets (value )
5869 }
59- }
6070
61- // Generate function wraps vfsgen.Generate function.
62- // Additionally to vfsgen.Generate, it also takes map of directories,
63- // where key represents path in the assets and a value represents path
64- // to the assets directory in local filesystem (which should be relative).
65- //
66- // This function also resets modification time for every file, so creating a new copy
67- // of code does not trigger changes in all asset files.
68- func Generate (fileName string , packageName string , variableName string , dirs map [string ]string ) error {
69- ufs := make (map [string ]http.FileSystem )
70- for assetsPath , fsPath := range dirs {
71- ufs [assetsPath ] = http .Dir (fsPath )
72- }
73- u := union .New (ufs )
74- fs := modtimevfs .New (u , time .Unix (1 , 0 ))
75- return vfsgen .Generate (fs , vfsgen.Options {
76- Filename : fileName ,
77- PackageName : packageName ,
78- VariableName : variableName ,
79- })
71+ return newEmbeddedAssets ()
8072}
8173
8274// Extract recursively extracts the assets at src into the directory dst. If dst doesn't exist, the
@@ -85,8 +77,8 @@ func Generate(fileName string, packageName string, variableName string, dirs map
8577// The assets are read either from data embedded in the binary or from the filesystem, depending on
8678// whether the LOKOCTL_USE_FS_ASSETS environment variable is set.
8779func Extract (src , dst string ) error {
88- walk := CopyingWalker (dst , 0755 )
89- if err := Assets .WalkFiles (src , walk ); err != nil {
80+ walk := copyingWalker (dst , 0700 )
81+ if err := get () .WalkFiles (src , walk ); err != nil {
9082 return fmt .Errorf ("failed to walk assets: %v" , err )
9183 }
9284
0 commit comments