github.com/elves/elvish@v0.15.0/pkg/daemon/service.go (about) 1 package daemon 2 3 import ( 4 "syscall" 5 6 "github.com/elves/elvish/pkg/daemon/internal/api" 7 "github.com/elves/elvish/pkg/store" 8 ) 9 10 // A net/rpc service for the daemon. 11 type service struct { 12 store store.Store 13 err error 14 } 15 16 // Implementations of RPC methods. 17 18 // Version returns the API version number. 19 func (s *service) Version(req *api.VersionRequest, res *api.VersionResponse) error { 20 if s.err != nil { 21 return s.err 22 } 23 res.Version = Version 24 return nil 25 } 26 27 // Pid returns the process ID of the daemon. 28 func (s *service) Pid(req *api.PidRequest, res *api.PidResponse) error { 29 res.Pid = syscall.Getpid() 30 return nil 31 } 32 33 func (s *service) NextCmdSeq(req *api.NextCmdSeqRequest, res *api.NextCmdSeqResponse) error { 34 if s.err != nil { 35 return s.err 36 } 37 seq, err := s.store.NextCmdSeq() 38 res.Seq = seq 39 return err 40 } 41 42 func (s *service) AddCmd(req *api.AddCmdRequest, res *api.AddCmdResponse) error { 43 if s.err != nil { 44 return s.err 45 } 46 seq, err := s.store.AddCmd(req.Text) 47 res.Seq = seq 48 return err 49 } 50 51 func (s *service) DelCmd(req *api.DelCmdRequest, res *api.DelCmdResponse) error { 52 if s.err != nil { 53 return s.err 54 } 55 err := s.store.DelCmd(req.Seq) 56 return err 57 } 58 59 func (s *service) Cmd(req *api.CmdRequest, res *api.CmdResponse) error { 60 if s.err != nil { 61 return s.err 62 } 63 text, err := s.store.Cmd(req.Seq) 64 res.Text = text 65 return err 66 } 67 68 func (s *service) Cmds(req *api.CmdsRequest, res *api.CmdsResponse) error { 69 if s.err != nil { 70 return s.err 71 } 72 cmds, err := s.store.Cmds(req.From, req.Upto) 73 res.Cmds = cmds 74 return err 75 } 76 77 func (s *service) CmdsWithSeq(req *api.CmdsWithSeqRequest, res *api.CmdsWithSeqResponse) error { 78 if s.err != nil { 79 return s.err 80 } 81 cmds, err := s.store.CmdsWithSeq(req.From, req.Upto) 82 res.Cmds = cmds 83 return err 84 } 85 86 func (s *service) NextCmd(req *api.NextCmdRequest, res *api.NextCmdResponse) error { 87 if s.err != nil { 88 return s.err 89 } 90 cmd, err := s.store.NextCmd(req.From, req.Prefix) 91 res.Seq, res.Text = cmd.Seq, cmd.Text 92 return err 93 } 94 95 func (s *service) PrevCmd(req *api.PrevCmdRequest, res *api.PrevCmdResponse) error { 96 if s.err != nil { 97 return s.err 98 } 99 cmd, err := s.store.PrevCmd(req.Upto, req.Prefix) 100 res.Seq, res.Text = cmd.Seq, cmd.Text 101 return err 102 } 103 104 func (s *service) AddDir(req *api.AddDirRequest, res *api.AddDirResponse) error { 105 if s.err != nil { 106 return s.err 107 } 108 return s.store.AddDir(req.Dir, req.IncFactor) 109 } 110 111 func (s *service) DelDir(req *api.DelDirRequest, res *api.DelDirResponse) error { 112 if s.err != nil { 113 return s.err 114 } 115 return s.store.DelDir(req.Dir) 116 } 117 118 func (s *service) Dirs(req *api.DirsRequest, res *api.DirsResponse) error { 119 if s.err != nil { 120 return s.err 121 } 122 dirs, err := s.store.Dirs(req.Blacklist) 123 res.Dirs = dirs 124 return err 125 } 126 127 func (s *service) SharedVar(req *api.SharedVarRequest, res *api.SharedVarResponse) error { 128 if s.err != nil { 129 return s.err 130 } 131 value, err := s.store.SharedVar(req.Name) 132 res.Value = value 133 return err 134 } 135 136 func (s *service) SetSharedVar(req *api.SetSharedVarRequest, res *api.SetSharedVarResponse) error { 137 if s.err != nil { 138 return s.err 139 } 140 return s.store.SetSharedVar(req.Name, req.Value) 141 } 142 143 func (s *service) DelSharedVar(req *api.DelSharedVarRequest, res *api.DelSharedVarResponse) error { 144 if s.err != nil { 145 return s.err 146 } 147 return s.store.DelSharedVar(req.Name) 148 }