// Copyright © 2019 Ettore Di Giacinto // Daniele Rondina // // 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 2 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 . package cmd import ( "io/ioutil" "os" "path/filepath" "github.com/mudler/luet/cmd/util" . "github.com/mudler/luet/pkg/config" fileHelper "github.com/mudler/luet/pkg/helpers/file" . "github.com/mudler/luet/pkg/logger" "github.com/spf13/cobra" ) var cleanupCmd = &cobra.Command{ Use: "cleanup", Short: "Clean packages cache.", Long: `remove downloaded packages tarballs and clean cache directory`, PreRun: func(cmd *cobra.Command, args []string) { util.BindSystemFlags(cmd) }, Run: func(cmd *cobra.Command, args []string) { var cleaned int = 0 util.SetSystemConfig() // Check if cache dir exists if fileHelper.Exists(LuetCfg.GetSystem().GetSystemPkgsCacheDirPath()) { files, err := ioutil.ReadDir(LuetCfg.GetSystem().GetSystemPkgsCacheDirPath()) if err != nil { Fatal("Error on read cachedir ", err.Error()) } for _, file := range files { if file.IsDir() { continue } if LuetCfg.GetGeneral().Debug { Info("Removing ", file.Name()) } err := os.RemoveAll( filepath.Join(LuetCfg.GetSystem().GetSystemPkgsCacheDirPath(), file.Name())) if err != nil { Fatal("Error on removing", file.Name()) } cleaned++ } } Info("Cleaned: ", cleaned, "packages.") }, } func init() { cleanupCmd.Flags().String("system-dbpath", "", "System db path") cleanupCmd.Flags().String("system-target", "", "System rootpath") cleanupCmd.Flags().String("system-engine", "", "System DB engine") RootCmd.AddCommand(cleanupCmd) }