Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-ha committed Mar 10, 2024
1 parent f9f6c95 commit a73aa11
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
15 changes: 12 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
"sync"

"github.com/ben-ha/jcp/logic"
"github.com/ben-ha/jcp/state"
"github.com/ben-ha/jcp/tui"
tea "github.com/charmbracelet/bubbletea"
)

func updaterFunc(jcp logic.Jcp, ui *tea.Program) {
func updaterFunc(jcp logic.Jcp, ui *tea.Program, state *state.JcpState) {
for {
update, more := <-jcp.ProgressChannel
if !more {
Expand All @@ -26,6 +27,7 @@ func updaterFunc(jcp logic.Jcp, ui *tea.Program) {
panic(fmt.Sprintf("An error occurred: %v", update.JcpError.Error()))
}

state.Update(update)
ui.Send(tui.UITransferMsg{Progress: update.Progress})
}

Expand All @@ -43,6 +45,13 @@ func main() {
src := os.Args[1]
dst := os.Args[2]

state, err := state.InitializeState()
if err != nil {
panic(fmt.Sprintf("State: %v", err))
}

defer state.SaveState()

jcp := logic.MakeJcp(10)

ui := tea.NewProgram(tui.UIModel{})
Expand All @@ -51,9 +60,9 @@ func main() {

go StartUI(ui, &uiComplete)

go updaterFunc(jcp, ui)
go updaterFunc(jcp, ui, state)

err := jcp.StartCopy(src, dst)
err = jcp.StartCopy(src, dst)
if err != nil {
panic(fmt.Sprintf("Error: %v", err))
}
Expand Down
1 change: 1 addition & 0 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type CopyDestinationKey = string

type JcpState struct {
CopyStates map[CopySourceKey](map[CopyDestinationKey]JcpCopyState)
StatePath string
}

type CopierType int
Expand Down
29 changes: 27 additions & 2 deletions state/statemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package state
import (
"encoding/json"
"os"
"path"
"sync"
"time"

Expand All @@ -13,19 +14,43 @@ var stateManagerMutex sync.Mutex

const ValidStateWindowInDays = 1

func LoadState(fileName string) *JcpState {
const JcpStateDirectoryName = "jcp"
const JcpStateFileName = "state.json"

func InitializeState() (*JcpState, error) {
cacheDir, err := os.UserCacheDir()
if err != nil {
return nil, err
}

jcpDir := path.Join(cacheDir, JcpStateDirectoryName)

err = os.Mkdir(jcpDir, os.ModePerm)
if !os.IsExist(err) {
return nil, err
}

return loadState(path.Join(jcpDir, JcpStateFileName)), nil
}

func loadState(fileName string) *JcpState {
data, err := os.ReadFile(fileName)
loadedState := &JcpState{}
if err == nil {
json.Unmarshal(data, loadedState)
}

loadedState.Clean()
loadedState.StatePath = fileName

return loadedState
}

func (copierState *JcpState) Save(fileName string) {
func (copierState *JcpState) SaveState() {
copierState.saveState(copierState.StatePath)
}

func (copierState *JcpState) saveState(fileName string) {
serialized, err := json.Marshal(copierState)

if err == nil {
Expand Down

0 comments on commit a73aa11

Please sign in to comment.