Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / honnef.co / go / tools@v0.0.1-2020.1.5 / internal / cache / default.go
1 // Copyright 2017 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package cache
6
7 import (
8         "fmt"
9         "io/ioutil"
10         "log"
11         "os"
12         "path/filepath"
13         "sync"
14 )
15
16 // Default returns the default cache to use.
17 func Default() (*Cache, error) {
18         defaultOnce.Do(initDefaultCache)
19         return defaultCache, defaultDirErr
20 }
21
22 var (
23         defaultOnce  sync.Once
24         defaultCache *Cache
25 )
26
27 // cacheREADME is a message stored in a README in the cache directory.
28 // Because the cache lives outside the normal Go trees, we leave the
29 // README as a courtesy to explain where it came from.
30 const cacheREADME = `This directory holds cached build artifacts from staticcheck.
31 `
32
33 // initDefaultCache does the work of finding the default cache
34 // the first time Default is called.
35 func initDefaultCache() {
36         dir := DefaultDir()
37         if err := os.MkdirAll(dir, 0777); err != nil {
38                 log.Fatalf("failed to initialize build cache at %s: %s\n", dir, err)
39         }
40         if _, err := os.Stat(filepath.Join(dir, "README")); err != nil {
41                 // Best effort.
42                 ioutil.WriteFile(filepath.Join(dir, "README"), []byte(cacheREADME), 0666)
43         }
44
45         c, err := Open(dir)
46         if err != nil {
47                 log.Fatalf("failed to initialize build cache at %s: %s\n", dir, err)
48         }
49         defaultCache = c
50 }
51
52 var (
53         defaultDirOnce sync.Once
54         defaultDir     string
55         defaultDirErr  error
56 )
57
58 // DefaultDir returns the effective STATICCHECK_CACHE setting.
59 func DefaultDir() string {
60         // Save the result of the first call to DefaultDir for later use in
61         // initDefaultCache. cmd/go/main.go explicitly sets GOCACHE so that
62         // subprocesses will inherit it, but that means initDefaultCache can't
63         // otherwise distinguish between an explicit "off" and a UserCacheDir error.
64
65         defaultDirOnce.Do(func() {
66                 defaultDir = os.Getenv("STATICCHECK_CACHE")
67                 if filepath.IsAbs(defaultDir) {
68                         return
69                 }
70                 if defaultDir != "" {
71                         defaultDirErr = fmt.Errorf("STATICCHECK_CACHE is not an absolute path")
72                         return
73                 }
74
75                 // Compute default location.
76                 dir, err := os.UserCacheDir()
77                 if err != nil {
78                         defaultDirErr = fmt.Errorf("STATICCHECK_CACHE is not defined and %v", err)
79                         return
80                 }
81                 defaultDir = filepath.Join(dir, "staticcheck")
82         })
83
84         return defaultDir
85 }