first
This commit is contained in:
56
work_tests/main.go
Normal file
56
work_tests/main.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type outputStruct struct {
|
||||
RootHistory []rootHistoryCommand `json:"root_history_command"`
|
||||
}
|
||||
|
||||
type rootHistoryCommand struct {
|
||||
Id int `json:"id"`
|
||||
ExecutionTime string `json:"excution_time"`
|
||||
Command string `json:"command"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
var o outputStruct
|
||||
var err error
|
||||
o.RootHistory, err = getRootHistory()
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
for _, rh := range o.RootHistory {
|
||||
fmt.Printf("%d %s %s\n", rh.Id, rh.ExecutionTime, rh.Command)
|
||||
}
|
||||
}
|
||||
|
||||
func getRootHistory() ([]rootHistoryCommand, error) {
|
||||
var rootHistoryCommands []rootHistoryCommand
|
||||
rawHistory, err := os.ReadFile("/home/kbn/.bash_history")
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Ошибка при открытии файла /home/kbn/.bash_history : %s", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
i := 0
|
||||
execTime := ""
|
||||
for _, line := range strings.Split(string(rawHistory), "\n") {
|
||||
var c rootHistoryCommand
|
||||
if strings.HasPrefix(line, "#") {
|
||||
execTime = strings.TrimLeft(line, "#")
|
||||
} else {
|
||||
c.Id = i
|
||||
i += 1
|
||||
c.ExecutionTime = execTime
|
||||
c.Command = line
|
||||
execTime = ""
|
||||
rootHistoryCommands = append(rootHistoryCommands, c)
|
||||
}
|
||||
|
||||
}
|
||||
return rootHistoryCommands, err
|
||||
}
|
||||
Reference in New Issue
Block a user