1
1
package cmd
2
2
3
3
import (
4
- "bufio"
5
4
"fmt"
6
- "io"
7
- "os"
8
- "os/exec"
9
- "os/signal"
10
- "path/filepath"
11
- "runtime"
12
- "syscall"
13
- "time"
14
5
15
6
tea "github.com/charmbracelet/bubbletea"
16
7
"github.com/spf13/cobra"
17
8
18
9
"github.com/initia-labs/weave/models"
19
10
"github.com/initia-labs/weave/models/initia"
11
+ "github.com/initia-labs/weave/service"
20
12
"github.com/initia-labs/weave/utils"
21
13
)
22
14
23
- const (
24
- PreviousLogLines = 100
25
- )
26
-
27
15
func InitiaCommand () * cobra.Command {
28
16
cmd := & cobra.Command {
29
17
Use : "initia" ,
@@ -78,10 +66,15 @@ func initiaStartCommand() *cobra.Command {
78
66
Use : "start" ,
79
67
Short : "Start the initiad full node application." ,
80
68
RunE : func (cmd * cobra.Command , args []string ) error {
81
- err := utils .StartService (utils .GetRunL1NodeServiceName ())
69
+ s , err := service .NewService (service .Initia )
70
+ if err != nil {
71
+ return err
72
+ }
73
+ err = s .Start ()
82
74
if err != nil {
83
75
return err
84
76
}
77
+ fmt .Println ("Started Initia full node application. You can see the logs with `initia log`" )
85
78
return nil
86
79
},
87
80
}
@@ -94,10 +87,15 @@ func initiaStopCommand() *cobra.Command {
94
87
Use : "stop" ,
95
88
Short : "Stop the initiad full node application." ,
96
89
RunE : func (cmd * cobra.Command , args []string ) error {
97
- err := utils . StopService ( utils . GetRunL1NodeServiceName () )
90
+ s , err := service . NewService ( service . Initia )
98
91
if err != nil {
99
92
return err
100
93
}
94
+ err = s .Stop ()
95
+ if err != nil {
96
+ return err
97
+ }
98
+ fmt .Println ("Stopped Initia full node application." )
101
99
return nil
102
100
},
103
101
}
@@ -106,124 +104,39 @@ func initiaStopCommand() *cobra.Command {
106
104
}
107
105
108
106
func initiaRestartCommand () * cobra.Command {
109
- reStartCmd := & cobra.Command {
107
+ restartCmd := & cobra.Command {
110
108
Use : "restart" ,
111
109
Short : "Restart the initiad full node application." ,
112
110
RunE : func (cmd * cobra.Command , args []string ) error {
113
- err := utils . StopService ( utils . GetRunL1NodeServiceName () )
111
+ s , err := service . NewService ( service . Initia )
114
112
if err != nil {
115
113
return err
116
114
}
117
-
118
- err = utils .StartService (utils .GetRunL1NodeServiceName ())
115
+ err = s .Restart ()
119
116
if err != nil {
120
117
return err
121
118
}
119
+
120
+ fmt .Println ("Started Initia full node application. You can see the logs with `initia log`" )
122
121
return nil
123
122
},
124
123
}
125
124
126
- return reStartCmd
125
+ return restartCmd
127
126
}
128
127
129
128
func initiaLogCommand () * cobra.Command {
130
129
logCmd := & cobra.Command {
131
130
Use : "log" ,
132
131
Short : "Stream the logs of the initiad full node application." ,
133
132
RunE : func (cmd * cobra.Command , args []string ) error {
134
- // Check if the OS is Linux
135
- switch runtime .GOOS {
136
- case "linux" :
137
- return streamLogsFromJournalctl ()
138
- case "darwin" :
139
- // If not Linux, fall back to file-based log streaming
140
- return streamLogsFromFiles ()
141
- default :
142
- return fmt .Errorf ("unsupported OS: %s" , runtime .GOOS )
133
+ s , err := service .NewService (service .Initia )
134
+ if err != nil {
135
+ return err
143
136
}
137
+ return s .Log ()
144
138
},
145
139
}
146
140
147
141
return logCmd
148
142
}
149
-
150
- // streamLogsFromJournalctl uses journalctl to stream logs from initia.service
151
- func streamLogsFromJournalctl () error {
152
- // Execute the journalctl command to follow logs of initia.service
153
- cmd := exec .Command ("journalctl" , "-f" , "-u" , "initia.service" )
154
- cmd .Stdout = os .Stdout
155
- cmd .Stderr = os .Stderr
156
-
157
- // Run the command and return any errors
158
- if err := cmd .Run (); err != nil {
159
- return fmt .Errorf ("failed to stream logs using journalctl: %v" , err )
160
- }
161
-
162
- return nil
163
- }
164
-
165
- // streamLogsFromFiles streams logs from file-based logs
166
- func streamLogsFromFiles () error {
167
- userHome , err := os .UserHomeDir ()
168
- if err != nil {
169
- return fmt .Errorf ("failed to get user home directory: %v" , err )
170
- }
171
-
172
- logFilePathOut := filepath .Join (userHome , utils .WeaveLogDirectory , "initia.stdout.log" )
173
- logFilePathErr := filepath .Join (userHome , utils .WeaveLogDirectory , "initia.stderr.log" )
174
-
175
- sigChan := make (chan os.Signal , 1 )
176
- signal .Notify (sigChan , os .Interrupt , syscall .SIGTERM )
177
-
178
- go tailLogFile (logFilePathOut , os .Stdout )
179
- go tailLogFile (logFilePathErr , os .Stderr )
180
-
181
- <- sigChan
182
-
183
- fmt .Println ("Stopping log streaming..." )
184
- return nil
185
- }
186
-
187
- func tailLogFile (filePath string , output io.Writer ) {
188
- file , err := os .Open (filePath )
189
- if err != nil {
190
- fmt .Printf ("error opening log file %s: %v\n " , filePath , err )
191
- return
192
- }
193
- defer file .Close ()
194
-
195
- var lines []string
196
- scanner := bufio .NewScanner (file )
197
-
198
- for scanner .Scan () {
199
- lines = append (lines , scanner .Text ())
200
- if len (lines ) > PreviousLogLines {
201
- lines = lines [1 :]
202
- }
203
- }
204
-
205
- for _ , line := range lines {
206
- fmt .Fprintln (output , line )
207
- }
208
-
209
- _ , err = file .Seek (0 , io .SeekEnd )
210
- if err != nil {
211
- fmt .Printf ("error seeking to end of log file %s: %v\n " , filePath , err )
212
- return
213
- }
214
-
215
- for {
216
- var line = make ([]byte , 4096 )
217
- n , err := file .Read (line )
218
- if err != nil && err != io .EOF {
219
- fmt .Printf ("error reading log file %s: %v\n " , filePath , err )
220
- return
221
- }
222
-
223
- if n > 0 {
224
- output .Write (line [:n ])
225
- } else {
226
- time .Sleep (1 * time .Second )
227
- }
228
- }
229
- }
0 commit comments