Skip to content

Commit

Permalink
Enable caching for modules
Browse files Browse the repository at this point in the history
- Add `--cache-dir` flag with default location set to `$HOME/.timoni/cache`
- Allow disabling caching with `TIMONI_CACHING=false` env var

Signed-off-by: Stefan Prodan <[email protected]>
  • Loading branch information
stefanprodan committed Nov 1, 2023
1 parent 40ce311 commit efabf2b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/timoni/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ func runApplyCmd(cmd *cobra.Command, args []string) error {
applyArgs.module,
version,
tmpDir,
rootArgs.cacheDir,
applyArgs.creds.String(),
)
mod, err := fetcher.Fetch()
Expand Down
1 change: 1 addition & 0 deletions cmd/timoni/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func runBuildCmd(cmd *cobra.Command, args []string) error {
buildArgs.module,
version,
tmpDir,
rootArgs.cacheDir,
buildArgs.creds.String(),
)
mod, err := fetcher.Fetch()
Expand Down
1 change: 1 addition & 0 deletions cmd/timoni/bundle_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ func fetchBundleInstanceModule(ctx context.Context, instance *engine.BundleInsta
instance.Module.Repository,
moduleVersion,
modDir,
rootArgs.cacheDir,
bundleApplyArgs.creds.String(),
)
mod, err := fetcher.Fetch()
Expand Down
25 changes: 25 additions & 0 deletions cmd/timoni/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main
import (
"context"
"os"
"path"
"time"

"github.com/fatih/color"
Expand Down Expand Up @@ -57,6 +58,7 @@ type rootFlags struct {
timeout time.Duration
prettyLog bool
coloredLog bool
cacheDir string
}

var (
Expand All @@ -76,6 +78,8 @@ func init() {
"Adds timestamps to the logs.")
rootCmd.PersistentFlags().BoolVar(&rootArgs.coloredLog, "log-color", rootArgs.coloredLog,
"Adds colorized output to the logs. (defaults to false when no tty)")
rootCmd.PersistentFlags().StringVar(&rootArgs.cacheDir, "cache-dir", "",
"Artifacts cache dir, can be disable with 'TIMONI_CACHING=false' env var. (defaults to \"$HOME/.timoni/cache\")")

addKubeConfigFlags(rootCmd)

Expand All @@ -85,6 +89,7 @@ func init() {
}

func main() {
setCacheDir()
if err := rootCmd.Execute(); err != nil {
// Ensure a logger is initialized even if the rootCmd
// failed before running its hooks.
Expand All @@ -99,6 +104,26 @@ func main() {
}
}

func setCacheDir() {
caching := os.Getenv("TIMONI_CACHING")
if caching == "false" || caching == "0" {
rootArgs.cacheDir = ""
return
}
if rootArgs.cacheDir == "" {
home, err := os.UserHomeDir()
if err != nil {
return
}
rootArgs.cacheDir = path.Join(home, ".timoni/cache")
}

if err := os.MkdirAll(rootArgs.cacheDir, os.ModePerm); err != nil {
// disable caching if target dir is not writable
rootArgs.cacheDir = ""
}
}

// addKubeConfigFlags maps the kubectl config flags to the given persistent flags.
// The default namespace is set to the value found in current kubeconfig context.
func addKubeConfigFlags(cmd *cobra.Command) {
Expand Down
1 change: 1 addition & 0 deletions cmd/timoni/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func TestMain(m *testing.M) {
}

kubeconfigArgs.KubeConfig = &tmpFilename
rootArgs.cacheDir = tmpDir

code := m.Run()
testEnv.Stop()
Expand Down
1 change: 1 addition & 0 deletions cmd/timoni/mod_vet.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func runVetModCmd(cmd *cobra.Command, args []string) error {
vetModArgs.path,
apiv1.LatestVersion,
tmpDir,
rootArgs.cacheDir,
"",
)
mod, err := fetcher.Fetch()
Expand Down

0 comments on commit efabf2b

Please sign in to comment.