| 25 | } |
| 26 | |
| 27 | func (a *autoremoveCmd) Command(celer *configs.Celer) *cobra.Command { |
| 28 | a.celer = celer |
| 29 | command := &cobra.Command{ |
| 30 | Use: "autoremove", |
| 31 | Short: "Remove libraries that do not belong to current project.", |
| 32 | Long: `Remove libraries that do not belong to current project. |
| 33 | |
| 34 | This command scans installed runtime and buildtime packages, compares them |
| 35 | against the dependency graph required by the current project, and removes |
| 36 | packages that are no longer needed. |
| 37 | |
| 38 | Use --purge to also remove cached package archives, and --build-cache to |
| 39 | remove build cache together with removed packages. |
| 40 | |
| 41 | Examples: |
| 42 | celer autoremove # Remove unused installed packages |
| 43 | celer autoremove --purge # Also remove package archives |
| 44 | celer autoremove --build-cache # Also remove build cache |
| 45 | celer autoremove --purge --build-cache # Remove packages, archives, and build cache`, |
| 46 | Args: cobra.NoArgs, |
| 47 | RunE: func(cmd *cobra.Command, args []string) error { |
| 48 | if err := a.celer.Init(); err != nil { |
| 49 | return fmt.Errorf("failed to init celer -> %w", err) |
| 50 | } |
| 51 | |
| 52 | return a.autoremove() |
| 53 | }, |
| 54 | ValidArgsFunction: a.completion, |
| 55 | } |
| 56 | |
| 57 | command.Flags().BoolVarP(&a.buildCache, "build-cache", "c", false, "autoremove packages along with build cache.") |
| 58 | command.Flags().BoolVarP(&a.purge, "purge", "p", false, "autoremove packages along with its package file.") |
| 59 | |
| 60 | // Silence cobra's error and usage output to avoid duplicate messages. |
| 61 | command.SilenceErrors = true |
| 62 | command.SilenceUsage = true |
| 63 | return command |
| 64 | } |
| 65 | |
| 66 | func (a *autoremoveCmd) autoremove() error { |
| 67 | // Reset collected state for every run. |