cli/pkg/cmd/config/clear-cache/clear_cache.go
Kynan Ware df295a1dba Add godoc comments to exported symbols in pkg/cmd/config and pkg/cmd/root
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-04 16:14:05 -07:00

56 lines
1.3 KiB
Go

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"
)
// ClearCacheOptions is documented here.
// ClearCacheOptions holds the options for the config clear-cache command.
type ClearCacheOptions struct {
IO *iostreams.IOStreams
CacheDir string
}
// NewCmdConfigClearCache is documented here.
// NewCmdConfigClearCache returns a cobra command for clearing the gh cache.
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
}