Skip to content

Commit

Permalink
chore: add fs interface (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno authored Apr 27, 2024
1 parent 471d74f commit 0f5acec
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 33 deletions.
8 changes: 8 additions & 0 deletions assets/fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package assets

import (
"embed"
)

//go:embed *
var EmbedFS embed.FS
20 changes: 19 additions & 1 deletion cmd/cosmos-proposals-checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

import (
"main/pkg"
"main/pkg/fs"
"main/pkg/logger"
"os"

"github.com/spf13/cobra"
)
Expand All @@ -11,8 +13,24 @@ var (
version = "unknown"
)

type OsFS struct {
}

func (fs *OsFS) ReadFile(name string) ([]byte, error) {
return os.ReadFile(name)
}

func (fs *OsFS) WriteFile(name string, data []byte, perms os.FileMode) error {
return os.WriteFile(name, data, perms)
}

func (fs *OsFS) Create(path string) (fs.File, error) {
return os.Create(path)
}

func Execute(configPath string) {
app := pkg.NewApp(configPath, version)
filesystem := &OsFS{}
app := pkg.NewApp(configPath, filesystem, version)
app.Start()
}

Expand Down
9 changes: 5 additions & 4 deletions pkg/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pkg

import (
"main/pkg/data"
"main/pkg/fs"
"main/pkg/logger"
mutes "main/pkg/mutes"
"main/pkg/report"
Expand All @@ -26,8 +27,8 @@ type App struct {
Reporters []reportersPkg.Reporter
}

func NewApp(configPath string, version string) *App {
config, err := GetConfig(configPath)
func NewApp(configPath string, filesystem fs.FS, version string) *App {
config, err := GetConfig(filesystem, configPath)
if err != nil {
logger.GetDefaultLogger().Fatal().Err(err).Msg("Could not load config")
}
Expand All @@ -38,8 +39,8 @@ func NewApp(configPath string, version string) *App {

log := logger.GetLogger(config.LogConfig)

stateManager := state.NewStateManager(config.StatePath, log)
mutesManager := mutes.NewMutesManager(config.MutesPath, log)
stateManager := state.NewStateManager(config.StatePath, filesystem, log)
mutesManager := mutes.NewMutesManager(config.MutesPath, filesystem, log)
reportGenerator := report.NewReportGenerator(stateManager, log, config.Chains)
stateGenerator := state.NewStateGenerator(log, config.Chains)
dataManager := data.NewManager(log, config.Chains)
Expand Down
16 changes: 16 additions & 0 deletions pkg/fs/fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package fs

import (
"io"
"os"
)

type File interface {
io.WriteCloser
}

type FS interface {
ReadFile(name string) ([]byte, error)
WriteFile(name string, data []byte, perms os.FileMode) error
Create(path string) (File, error)
}
6 changes: 3 additions & 3 deletions pkg/load_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package pkg

import (
"fmt"
"main/pkg/fs"
"main/pkg/logger"
"main/pkg/types"
"os"

"github.com/BurntSushi/toml"
"github.com/creasty/defaults"
)

func GetConfig(path string) (*types.Config, error) {
configBytes, err := os.ReadFile(path)
func GetConfig(filesystem fs.FS, path string) (*types.Config, error) {
configBytes, err := filesystem.ReadFile(path)
if err != nil {
return nil, err
}
Expand Down
22 changes: 12 additions & 10 deletions pkg/mutes/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@ package mutesmanager

import (
"encoding/json"
"os"
"main/pkg/fs"

"github.com/rs/zerolog"
)

type Manager struct {
MutesPath string
Logger zerolog.Logger
Mutes Mutes
Filesystem fs.FS
MutesPath string
Logger zerolog.Logger
Mutes Mutes
}

func NewMutesManager(mutesPath string, logger *zerolog.Logger) *Manager {
func NewMutesManager(mutesPath string, filesystem fs.FS, logger *zerolog.Logger) *Manager {
return &Manager{
MutesPath: mutesPath,
Logger: logger.With().Str("component", "mutes_manager").Logger(),
Mutes: Mutes{},
Filesystem: filesystem,
MutesPath: mutesPath,
Logger: logger.With().Str("component", "mutes_manager").Logger(),
Mutes: Mutes{},
}
}

Expand All @@ -27,7 +29,7 @@ func (m *Manager) Load() {
return
}

content, err := os.ReadFile(m.MutesPath)
content, err := m.Filesystem.ReadFile(m.MutesPath)
if err != nil {
m.Logger.Warn().Err(err).Msg("Could not load mutes")
return
Expand All @@ -54,7 +56,7 @@ func (m *Manager) Save() {
return
}

if err = os.WriteFile(m.MutesPath, content, 0o600); err != nil {
if err = m.Filesystem.WriteFile(m.MutesPath, content, 0o600); err != nil {
m.Logger.Warn().Err(err).Msg("Could not save mutes")
return
}
Expand Down
26 changes: 21 additions & 5 deletions pkg/report/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package report
import (
"errors"
"main/pkg/events"
"main/pkg/fs"
"os"
"testing"

"main/pkg/logger"
Expand All @@ -12,10 +14,24 @@ import (
"github.com/stretchr/testify/assert"
)

type TestFS struct{}

func (fs *TestFS) ReadFile(name string) ([]byte, error) {
return []byte{}, nil
}

func (fs *TestFS) WriteFile(name string, data []byte, perms os.FileMode) error {
return nil
}

func (fs *TestFS) Create(path string) (fs.File, error) {
return nil, nil
}

func TestReportGeneratorWithProposalError(t *testing.T) {
t.Parallel()

stateManager := state.NewStateManager("./state.json", logger.GetDefaultLogger())
stateManager := state.NewStateManager("./state.json", &TestFS{}, logger.GetDefaultLogger())

oldState := state.NewState()
newState := state.State{
Expand Down Expand Up @@ -43,7 +59,7 @@ func TestReportGeneratorWithProposalError(t *testing.T) {
func TestReportGeneratorWithVoteError(t *testing.T) {
t.Parallel()

stateManager := state.NewStateManager("./state.json", logger.GetDefaultLogger())
stateManager := state.NewStateManager("./state.json", &TestFS{}, logger.GetDefaultLogger())

oldState := state.NewState()
newState := state.State{
Expand Down Expand Up @@ -82,7 +98,7 @@ func TestReportGeneratorWithVoteError(t *testing.T) {
func TestReportGeneratorWithNotVoted(t *testing.T) {
t.Parallel()

stateManager := state.NewStateManager("./state.json", logger.GetDefaultLogger())
stateManager := state.NewStateManager("./state.json", &TestFS{}, logger.GetDefaultLogger())

oldState := state.NewState()
newState := state.State{
Expand Down Expand Up @@ -117,7 +133,7 @@ func TestReportGeneratorWithNotVoted(t *testing.T) {
func TestReportGeneratorWithVoted(t *testing.T) {
t.Parallel()

stateManager := state.NewStateManager("./state.json", logger.GetDefaultLogger())
stateManager := state.NewStateManager("./state.json", &TestFS{}, logger.GetDefaultLogger())

oldState := state.State{
ChainInfos: map[string]*state.ChainInfo{
Expand Down Expand Up @@ -171,7 +187,7 @@ func TestReportGeneratorWithVoted(t *testing.T) {
func TestReportGeneratorWithRevoted(t *testing.T) {
t.Parallel()

stateManager := state.NewStateManager("./state.json", logger.GetDefaultLogger())
stateManager := state.NewStateManager("./state.json", &TestFS{}, logger.GetDefaultLogger())

oldState := state.State{
ChainInfos: map[string]*state.ChainInfo{
Expand Down
22 changes: 12 additions & 10 deletions pkg/state/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,29 @@ package state

import (
"encoding/json"
"os"
"main/pkg/fs"

"github.com/rs/zerolog"
)

type Manager struct {
StatePath string
Logger zerolog.Logger
State State
Filesystem fs.FS
StatePath string
Logger zerolog.Logger
State State
}

func NewStateManager(path string, logger *zerolog.Logger) *Manager {
func NewStateManager(path string, filesystem fs.FS, logger *zerolog.Logger) *Manager {
return &Manager{
StatePath: path,
Logger: logger.With().Str("component", "state_manager").Logger(),
State: NewState(),
StatePath: path,
Filesystem: filesystem,
Logger: logger.With().Str("component", "state_manager").Logger(),
State: NewState(),
}
}

func (m *Manager) Load() {
content, err := os.ReadFile(m.StatePath)
content, err := m.Filesystem.ReadFile(m.StatePath)
if err != nil {
m.Logger.Warn().Err(err).Msg("Could not load state")
return
Expand All @@ -45,7 +47,7 @@ func (m *Manager) Save() {
return
}

if err = os.WriteFile(m.StatePath, content, 0o600); err != nil {
if err = m.Filesystem.WriteFile(m.StatePath, content, 0o600); err != nil {
m.Logger.Warn().Err(err).Msg("Could not save state")
return
}
Expand Down

0 comments on commit 0f5acec

Please sign in to comment.