Skip to content

Commit

Permalink
Merge pull request #27 from k1LoW/create-registory-and-storage
Browse files Browse the repository at this point in the history
Separate `repo.Repo` to `registory.Registory` and `storage.Storage`
  • Loading branch information
linyows authored Oct 5, 2023
2 parents 80f74f5 + 8f550ed commit 5ac315e
Show file tree
Hide file tree
Showing 16 changed files with 268 additions and 174 deletions.
8 changes: 4 additions & 4 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (
)

const (
// ExitOK for exit code
// ExitOK for exit code.
ExitOK int = 0

// ExitErr for exit code
// ExitErr for exit code.
ExitErr int = 1
)

Expand All @@ -35,7 +35,7 @@ type cli struct {
Version bool `long:"version" short:"v" description:"prints the version number"`
}

// Env struct
// Env struct.
type Env struct {
Out, Err io.Writer
Args []string
Expand All @@ -44,7 +44,7 @@ type Env struct {
Date string
}

// RunCLI runs as cli
// RunCLI runs as cli.
func RunCLI(env Env) int {
cli := &cli{env: env, Interval: -1, PreRelease: false}
return cli.run()
Expand Down
26 changes: 13 additions & 13 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import (
"github.com/linyows/dewy/repo"
)

// Command for CLI
// Command for CLI.
type Command int

const (
// SERVER command
// SERVER command.
SERVER Command = iota
// ASSETS command
// ASSETS command.
ASSETS
)

// String to string for Command
// String to string for Command.
func (c Command) String() string {
switch c {
case SERVER:
Expand All @@ -29,17 +29,17 @@ func (c Command) String() string {
}
}

// CacheType for cache type
// CacheType for cache type.
type CacheType int

const (
// NONE cache type
// NONE cache type.
NONE CacheType = iota
// FILE cache type
// FILE cache type.
FILE
)

// String to string for CacheType
// String to string for CacheType.
func (c CacheType) String() string {
switch c {
case NONE:
Expand All @@ -51,24 +51,24 @@ func (c CacheType) String() string {
}
}

// CacheConfig struct
// CacheConfig struct.
type CacheConfig struct {
Type CacheType
Expiration int
}

// Config struct
// Config struct.
type Config struct {
Command Command
Repository repo.Config
Cache CacheConfig
Starter starter.Config
}

// OverrideWithEnv overrides by environments
// OverrideWithEnv overrides by environments.
func (c *Config) OverrideWithEnv() {
if c.Repository.Provider == repo.GITHUB {
// Support env GITHUB_ENDPOINT
// Support env GITHUB_ENDPOINT.
e := os.Getenv("GITHUB_ENDPOINT")
if e != "" {
os.Setenv("GITHUB_API_URL", e)
Expand All @@ -80,7 +80,7 @@ func (c *Config) OverrideWithEnv() {
}
}

// DefaultConfig returns default Config
// DefaultConfig returns default Config.
func DefaultConfig() Config {
return Config{
Cache: CacheConfig{
Expand Down
50 changes: 31 additions & 19 deletions dewy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import (
starter "github.com/lestrrat-go/server-starter"
"github.com/linyows/dewy/kvs"
"github.com/linyows/dewy/notice"
"github.com/linyows/dewy/registory"
"github.com/linyows/dewy/repo"
"github.com/linyows/dewy/storage"
)

const (
Expand All @@ -27,10 +29,11 @@ const (
keepReleases = 7
)

// Dewy struct
// Dewy struct.
type Dewy struct {
config Config
repo repo.Repo
registory registory.Registory
fetcher storage.Fetcher
cache kvs.KVS
isServerRunning bool
root string
Expand All @@ -39,7 +42,7 @@ type Dewy struct {
sync.RWMutex
}

// New returns Dewy
// New returns Dewy.
func New(c Config) (*Dewy, error) {
kv := &kvs.File{}
kv.Default()
Expand All @@ -57,27 +60,33 @@ func New(c Config) (*Dewy, error) {
return &Dewy{
config: c,
cache: kv,
repo: r,
registory: r,
fetcher: r,
isServerRunning: false,
root: wd,
}, nil
}

// Start dewy
// Start dewy.
func (d *Dewy) Start(i int) {
ctx, cancel := context.WithCancel(context.WithValue(context.Background(), notice.MetaContextKey, true))
defer cancel()
var err error

d.notice, err = notice.New(&notice.Slack{Meta: &notice.Config{
RepoOwnerLink: d.repo.OwnerURL(),
RepoOwnerIcon: d.repo.OwnerIconURL(),
RepoLink: d.repo.URL(),
RepoOwner: d.config.Repository.Owner,
RepoName: d.config.Repository.Name,
Source: d.config.Repository.Artifact,
Command: d.config.Command.String(),
}})
nc := &notice.Config{
RepoOwner: d.config.Repository.Owner,
RepoName: d.config.Repository.Name,
Source: d.config.Repository.Artifact,
Command: d.config.Command.String(),
}
repo, ok := d.registory.(repo.Repo)
if ok {
nc.RepoOwnerLink = repo.OwnerURL()
nc.RepoOwnerIcon = repo.OwnerIconURL()
nc.RepoLink = repo.URL()
}

d.notice, err = notice.New(&notice.Slack{Meta: nc})
if err != nil {
log.Printf("[ERROR] Notice failure: %#v", err)
return
Expand Down Expand Up @@ -108,13 +117,13 @@ func (d *Dewy) waitSigs() os.Signal {
return sigReceived
}

// Run dewy
// Run dewy.
func (d *Dewy) Run() error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// Get current
res, err := d.repo.Current(&repo.CurrentRequest{
res, err := d.registory.Current(&registory.CurrentRequest{
ArtifactName: d.config.Repository.Artifact,
})
if err != nil {
Expand Down Expand Up @@ -148,7 +157,7 @@ func (d *Dewy) Run() error {
// Download artifact and cache
if !found {
buf := new(bytes.Buffer)
if err := d.repo.Download(buf); err != nil {
if err := d.fetcher.Fetch(res.ArtifactURL, buf); err != nil {
return err
}
if err := d.cache.Write(cacheKey, buf.Bytes()); err != nil {
Expand All @@ -159,7 +168,7 @@ func (d *Dewy) Run() error {

if d.notice != nil {
d.notice.Notify(ctx, fmt.Sprintf("New shipping <%s|%s> was detected",
d.repo.ReleaseURL(), d.repo.ReleaseTag()))
res.ArtifactURL, res.Tag))
}

if err := d.deploy(cacheKey); err != nil {
Expand All @@ -180,7 +189,10 @@ func (d *Dewy) Run() error {
}

log.Print("[DEBUG] Record shipping")
err = d.repo.RecordShipping()
err = d.registory.Report(&registory.ReportRequest{
ID: res.ID,
Tag: res.Tag,
})
if err != nil {
log.Printf("[ERROR] Record shipping failure: %#v", err)
}
Expand Down
3 changes: 2 additions & 1 deletion dewy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ func TestNew(t *testing.T) {
}
expect := &Dewy{
config: c,
repo: r,
registory: r,
fetcher: r,
cache: dewy.cache,
isServerRunning: false,
root: wd,
Expand Down
10 changes: 5 additions & 5 deletions kvs/consul.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package kvs

// Consul struct
// Consul struct.
type Consul struct {
items map[string]*item //nolint
Host string
Port int
Password string
}

// Read data on Consul
// Read data on Consul.
func (c *Consul) Read(key string) {
}

// Write data to Consul
// Write data to Consul.
func (c *Consul) Write(data string) {
}

// Delete data on Consul
// Delete data on Consul.
func (c *Consul) Delete(key string) {
}

// List returns key from Consul
// List returns key from Consul.
func (c *Consul) List() {
}
22 changes: 11 additions & 11 deletions kvs/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
)

var (
// DefaultTempDir creates temp dir
// DefaultTempDir creates temp dir.
DefaultTempDir = createTempDir()
// DefaultMaxSize for data size
// DefaultMaxSize for data size.
DefaultMaxSize int64 = 64 * 1024 * 1024
)

Expand All @@ -23,7 +23,7 @@ func createTempDir() string {
return dir
}

// File struct
// File struct.
type File struct {
items map[string]*item //nolint
dir string
Expand All @@ -32,18 +32,18 @@ type File struct {
MaxSize int64
}

// GetDir returns dir
// GetDir returns dir.
func (f *File) GetDir() string {
return f.dir
}

// Default sets to struct
// Default sets to struct.
func (f *File) Default() {
f.dir = DefaultTempDir
f.MaxSize = DefaultMaxSize
}

// Read data by key from file
// Read data by key from file.
func (f *File) Read(key string) ([]byte, error) {
p := filepath.Join(f.dir, key)
if !IsFileExist(p) {
Expand All @@ -58,7 +58,7 @@ func (f *File) Read(key string) ([]byte, error) {
return content, nil
}

// Write data to file
// Write data to file.
func (f *File) Write(key string, data []byte) error {
dirstat, err := os.Stat(f.dir)
if err != nil {
Expand Down Expand Up @@ -89,7 +89,7 @@ func (f *File) Write(key string, data []byte) error {
return nil
}

// Delete data on file
// Delete data on file.
func (f *File) Delete(key string) error {
p := filepath.Join(f.dir, key)
if !IsFileExist(p) {
Expand All @@ -103,7 +103,7 @@ func (f *File) Delete(key string) error {
return nil
}

// List returns keys from file
// List returns keys from file.
func (f *File) List() ([]string, error) {
files, err := os.ReadDir(f.dir)
if err != nil {
Expand All @@ -118,7 +118,7 @@ func (f *File) List() ([]string, error) {
return list, nil
}

// ExtractArchive extracts by archive
// ExtractArchive extracts by archive.
func ExtractArchive(src, dst string) error {
if !IsFileExist(src) {
return fmt.Errorf("File not found: %s", src)
Expand All @@ -127,7 +127,7 @@ func ExtractArchive(src, dst string) error {
return archiver.Unarchive(src, dst)
}

// IsFileExist checks file exists
// IsFileExist checks file exists.
func IsFileExist(p string) bool {
_, err := os.Stat(p)

Expand Down
6 changes: 3 additions & 3 deletions kvs/kvs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"
)

// KVS interface
// KVS interface.
type KVS interface {
Read(key string) ([]byte, error)
Write(key string, data []byte) error
Expand All @@ -15,11 +15,11 @@ type KVS interface {
GetDir() string
}

// Config struct
// Config struct.
type Config struct {
}

// New returns KVS
// New returns KVS.
func New(t string, c Config) (KVS, error) {
switch t {
case "file":
Expand Down
Loading

0 comments on commit 5ac315e

Please sign in to comment.