-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
866 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package dir | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"os" | ||
|
||
"github.com/cardil/ghet/pkg/metadata" | ||
"github.com/kirsle/configdir" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
const ( | ||
ConfigDirEnvName = "GHET_CONFIG_DIR" | ||
CacheDirEnvName = "GHET_CACHE_DIR" | ||
) | ||
|
||
var ( | ||
configDirKey = struct{}{} //nolint:gochecknoglobals | ||
cacheDirKey = struct{}{} //nolint:gochecknoglobals | ||
) | ||
|
||
func Config(ctx context.Context) string { | ||
return userPath(ctx, configDirKey, ConfigDirEnvName, func() string { | ||
return configdir.LocalConfig(metadata.Name) | ||
}) | ||
} | ||
|
||
func Cache(ctx context.Context) string { | ||
return userPath(ctx, cacheDirKey, CacheDirEnvName, func() string { | ||
return configdir.LocalCache(metadata.Name) | ||
}) | ||
} | ||
|
||
func userPath(ctx context.Context, key interface{}, envKey string, fn func() string) string { | ||
if p, ok := ctx.Value(key).(string); ok { | ||
return ensurePathExists(p) | ||
} | ||
p := os.Getenv(envKey) | ||
if p == "" { | ||
p = fn() | ||
} | ||
return ensurePathExists(p) | ||
} | ||
|
||
func ensurePathExists(p string) string { | ||
if err := configdir.MakePath(p); err != nil { | ||
log.Fatal(errors.WithStack(err)) | ||
} | ||
return p | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package dir | ||
|
||
import "context" | ||
|
||
func WithConfigDir(ctx context.Context, p string) context.Context { | ||
return context.WithValue(ctx, configDirKey, p) | ||
} | ||
|
||
func WithCacheDir(ctx context.Context, p string) context.Context { | ||
return context.WithValue(ctx, cacheDirKey, p) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package download | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"hash/crc32" | ||
"os" | ||
"path" | ||
"strconv" | ||
|
||
configdir "github.com/cardil/ghet/pkg/config/dir" | ||
githubapi "github.com/cardil/ghet/pkg/github/api" | ||
log "github.com/go-eden/slf4go" | ||
) | ||
|
||
const executablePerm = 0o750 | ||
|
||
func (p Plan) cachePath(ctx context.Context, asset githubapi.Asset) string { | ||
dir := path.Join(configdir.Cache(ctx), p.transationID()) | ||
if err := os.MkdirAll(dir, executablePerm); err != nil { | ||
log.Fatal(unexpected(err)) | ||
} | ||
return path.Join(dir, asset.Name) | ||
} | ||
|
||
func (p Plan) cleanCache(ctx context.Context) error { | ||
fp := path.Join(configdir.Cache(ctx), p.transationID()) | ||
if err := os.RemoveAll(fp); err != nil { | ||
return unexpected(err) | ||
} | ||
return nil | ||
} | ||
|
||
func (p Plan) transationID() string { | ||
h := crc32.NewIEEE() | ||
for _, asset := range p.Assets { | ||
repr := fmt.Sprintf("%#v", asset) | ||
_, _ = h.Write([]byte(repr)) | ||
} | ||
return strconv.Itoa(int(h.Sum32())) | ||
} |
Oops, something went wrong.