diff --git a/attachments.go b/attachments.go new file mode 100644 index 0000000..b44dde7 --- /dev/null +++ b/attachments.go @@ -0,0 +1,11 @@ +package obspipeline + +func (p *Pipeline) FindAttachments() error { + + return nil +} + +func (p *Pipeline) MoveAttachments(post string) error { + + return nil +} diff --git a/fsm/fsm.go b/fsm/fsm.go new file mode 100644 index 0000000..bf6f8fd --- /dev/null +++ b/fsm/fsm.go @@ -0,0 +1,59 @@ +package obspipeline + +import ( + "context" + "fmt" +) + +type State string +type Event string + +type Action func(ctx context.Context) error + +type Node struct { + State + Transitions map[Event]*Transition +} + +type Transition struct { + *Node + Action +} + +type StateMachine struct { + initialNode *Node + CurrentNode *Node +} + +func (m *StateMachine) getCurrentNode() *Node { + return m.CurrentNode +} + +func (m *StateMachine) getNextNode(event Event) (*Node, error) { + if m.CurrentNode == nil { + return nil, fmt.Errorf("nowhere to go anymore!\n") + } + + transition, ok := m.CurrentNode.Transitions[event] + if !ok { + return nil, fmt.Errorf("invalid event: %v", event) + } + + return transition.Node, nil +} + +func (m *StateMachine) Transition(ctx context.Context, event Event) (*Node, error) { + node, err := m.getNextNode(event) // gets current node + if err != nil { + return nil, fmt.Errorf(err.Error()) + } + + err = m.CurrentNode.Transitions[event].Action(ctx) + if err != nil { + return nil, fmt.Errorf(err.Error()) + } + + m.CurrentNode = node + + return m.CurrentNode, nil +} diff --git a/fsm/nodes.go b/fsm/nodes.go new file mode 100644 index 0000000..3c24354 --- /dev/null +++ b/fsm/nodes.go @@ -0,0 +1,91 @@ +package obspipeline + +import ( + "context" +) + +var CopyPost = Node{ + State: "CopyPost", + Transitions: map[Event]*Transition{ + "HasAttachments": &Transition{ + Node: &HasAttachments, + Action: func(ctx context.Context) error { + return nil + }, + }, + "NoAttachments": &Transition{ + Node: &NoAttachments, + Action: func(ctx context.Context) error { + return nil + }, + }, + }, +} + +var NoteFound = Node{ + State: "NoteFound", + Transitions: map[Event]*Transition{ + "CopyPost": &Transition{ + Node: &CopyPost, + Action: func(ctx context.Context) error { + // scan for attachments here + // if len() attachments > 0 + // + return nil + }, + }, + }, +} + +var NoAttachments = Node{ + State: "NoAttachments", + Transitions: map[Event]*Transition{ + "Terminate": &Transition{ + Node: &Terminate, + Action: func(ctx context.Context) error { + return nil + }, + }, + }, +} + +var HasAttachments = Node{ + State: "HasAttachments", + Transitions: map[Event]*Transition{ + "CopyAttachments": &Transition{ + Node: &CopyAttachments, + Action: func(ctx context.Context) error { + return nil + }, + }, + }, +} + +var CopyAttachments = Node{ + State: "CopyAttachments", + Transitions: map[Event]*Transition{ + "SanitizeLinks": &Transition{ + Node: &SanitizeLinks, + Action: func(ctx context.Context) error { + return nil + }, + }, + }, +} + +var SanitizeLinks = Node{ + State: "SanitizeLinks", + Transitions: map[Event]*Transition{ + "Terminate": &Transition{ + Node: &Terminate, + Action: func(ctx context.Context) error { + return nil + }, + }, + }, +} + +var Terminate = Node{ + State: "SanitizeLinks", + Transitions: map[Event]*Transition{}, +} diff --git a/notes.go b/notes.go new file mode 100644 index 0000000..b12dc49 --- /dev/null +++ b/notes.go @@ -0,0 +1,5 @@ +package obspipeline + +func (p *Pipeline) FindNotes() error { + return nil +} diff --git a/pipeline.go b/pipeline.go index a031d51..f8817bd 100644 --- a/pipeline.go +++ b/pipeline.go @@ -26,31 +26,3 @@ type Pipeline struct { L *zap.Logger BlogDir, AttachmentsDir string } - -func (p *Pipeline) FindAttachments() error { - - return nil -} - -func (p *Pipeline) FindNotes() error { - return nil -} - -func (p *Pipeline) FindPosts() error { - return nil -} - -func (p *Pipeline) CopyPost(post string) error { - - return nil -} - -func (p *Pipeline) MoveAttachments(post string) error { - - return nil -} - -func (p *Pipeline) SanitizePost(post string) error { - - return nil -} diff --git a/posts.go b/posts.go new file mode 100644 index 0000000..dd2eae3 --- /dev/null +++ b/posts.go @@ -0,0 +1,15 @@ +package obspipeline + +func (p *Pipeline) FindPosts() error { + return nil +} + +func (p *Pipeline) SanitizePost(post string) error { + + return nil +} + +func (p *Pipeline) CopyPost(post string) error { + + return nil +}