Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate repo.Repo to registory.Registory and storage.Storage #27

Merged
merged 3 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
65 changes: 36 additions & 29 deletions dewy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import (
"context"
"fmt"
"log"
"net/url"
"os"
"os/signal"
"path/filepath"
"sort"
"strings"
"sync"
"syscall"
"time"
Expand All @@ -19,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 @@ -29,10 +29,11 @@ const (
keepReleases = 7
)

// Dewy struct
// Dewy struct.
type Dewy struct {
config Config
repo repo.Repo
registory registory.Registory
fetcher storage.Fetcher
Comment on lines +35 to +36
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separate repo.Repo to registory.Registory and storage.Fetcher.

cache kvs.KVS
isServerRunning bool
root string
Expand All @@ -41,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 @@ -59,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 @@ -110,25 +117,22 @@ 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()

if err := d.repo.Fetch(); err != nil {
log.Printf("[ERROR] Fetch failure: %#v", err)
return err
}

// Create latest cache key
du, ut := d.repo.LatestKey()
u, err := url.Parse(du)
// Get current
res, err := d.registory.Current(&registory.CurrentRequest{
ArtifactName: d.config.Repository.Artifact,
})
if err != nil {
log.Printf("[ERROR] Current failure: %#v", err)
return err
}
cacheKey := strings.Replace(fmt.Sprintf("%s--%d-%s", u.Host, ut.Unix(), u.RequestURI()), "/", "-", -1)

// Check cache
cacheKey := fmt.Sprintf("%s-%s", res.Tag, filepath.Base(res.ArtifactURL))
currentKey := "current.txt"
currentSourceKey, _ := d.cache.Read(currentKey)
found := false
Expand All @@ -153,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 @@ -164,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 @@ -185,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
Loading