File: clear_cache.go

package info (click to toggle)
gh 2.46.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,548 kB
  • sloc: sh: 227; makefile: 117
file content (51 lines) | stat: -rw-r--r-- 1,071 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package clearcache

import (
	"fmt"
	"os"

	"github.com/MakeNowJust/heredoc"
	"github.com/cli/cli/v2/pkg/cmdutil"
	"github.com/cli/cli/v2/pkg/iostreams"
	"github.com/cli/go-gh/v2/pkg/config"
	"github.com/spf13/cobra"
)

type ClearCacheOptions struct {
	IO       *iostreams.IOStreams
	CacheDir string
}

func NewCmdConfigClearCache(f *cmdutil.Factory, runF func(*ClearCacheOptions) error) *cobra.Command {
	opts := &ClearCacheOptions{
		IO:       f.IOStreams,
		CacheDir: config.CacheDir(),
	}

	cmd := &cobra.Command{
		Use:   "clear-cache",
		Short: "Clear the cli cache",
		Example: heredoc.Doc(`
			# Clear the cli cache
			$ gh config clear-cache
		`),
		Args: cobra.ExactArgs(0),
		RunE: func(_ *cobra.Command, _ []string) error {
			if runF != nil {
				return runF(opts)
			}
			return clearCacheRun(opts)
		},
	}

	return cmd
}

func clearCacheRun(opts *ClearCacheOptions) error {
	if err := os.RemoveAll(opts.CacheDir); err != nil {
		return err
	}
	cs := opts.IO.ColorScheme()
	fmt.Fprintf(opts.IO.Out, "%s Cleared the cache\n", cs.SuccessIcon())
	return nil
}