mirror of
https://github.com/syssecfsu/witty.git
synced 2025-04-10 01:37:53 +02:00
add merge subcommand
This commit is contained in:
parent
791298f8d6
commit
7e7ef7bc20
54
main.go
54
main.go
@ -5,30 +5,23 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/syssecfsu/witty/term_conn"
|
||||
"github.com/syssecfsu/witty/web"
|
||||
)
|
||||
|
||||
const (
|
||||
subcmds = "witty (adduser|deluser|listusers|replay|merge|run)"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Println("witty (adduser|deluser|replay|run)")
|
||||
fmt.Println(subcmds)
|
||||
return
|
||||
}
|
||||
|
||||
var naked bool
|
||||
var port uint
|
||||
runCmd := flag.NewFlagSet("run", flag.ExitOnError)
|
||||
runCmd.BoolVar(&naked, "n", false, "Run WiTTY without user authentication")
|
||||
runCmd.BoolVar(&naked, "naked", false, "Run WiTTY without user authentication")
|
||||
runCmd.UintVar(&port, "p", 8080, "Port number to listen on")
|
||||
runCmd.UintVar(&port, "port", 8080, "Port number to listen on")
|
||||
|
||||
var wait uint
|
||||
replayCmd := flag.NewFlagSet("replay", flag.ExitOnError)
|
||||
replayCmd.UintVar(&wait, "w", 2000, "Max wait time between outputs")
|
||||
replayCmd.UintVar(&wait, "wait", 2000, "Max wait time between outputs")
|
||||
|
||||
switch os.Args[1] {
|
||||
case "adduser":
|
||||
if len(os.Args) != 3 {
|
||||
@ -48,6 +41,11 @@ func main() {
|
||||
web.ListUsers()
|
||||
|
||||
case "replay":
|
||||
var wait uint
|
||||
replayCmd := flag.NewFlagSet("replay", flag.ExitOnError)
|
||||
replayCmd.UintVar(&wait, "w", 2000, "Max wait time between outputs")
|
||||
replayCmd.UintVar(&wait, "wait", 2000, "Max wait time between outputs")
|
||||
|
||||
replayCmd.Parse(os.Args[2:])
|
||||
|
||||
if len(replayCmd.Args()) != 1 {
|
||||
@ -57,7 +55,33 @@ func main() {
|
||||
|
||||
term_conn.Replay(replayCmd.Arg(0), wait)
|
||||
|
||||
case "merge":
|
||||
var output string
|
||||
|
||||
defName := "merged_" + strconv.FormatInt(time.Now().Unix(), 16) + ".scr"
|
||||
|
||||
mergeCmd := flag.NewFlagSet("merge", flag.ExitOnError)
|
||||
mergeCmd.StringVar(&output, "o", defName, "Set the name of merged files")
|
||||
mergeCmd.StringVar(&output, "output", defName, "Set the name of merged files")
|
||||
|
||||
mergeCmd.Parse(os.Args[2:])
|
||||
|
||||
if len(mergeCmd.Args()) < 2 {
|
||||
fmt.Println("witty merge -o output_file file1 file2 ... (at least two files)")
|
||||
return
|
||||
}
|
||||
|
||||
term_conn.Merge(mergeCmd.Args(), output)
|
||||
|
||||
case "run":
|
||||
var naked bool
|
||||
var port uint
|
||||
runCmd := flag.NewFlagSet("run", flag.ExitOnError)
|
||||
runCmd.BoolVar(&naked, "n", false, "Run WiTTY without user authentication")
|
||||
runCmd.BoolVar(&naked, "naked", false, "Run WiTTY without user authentication")
|
||||
runCmd.UintVar(&port, "p", 8080, "Port number to listen on")
|
||||
runCmd.UintVar(&port, "port", 8080, "Port number to listen on")
|
||||
|
||||
fp, err := os.OpenFile("witty.log", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
||||
|
||||
if err == nil {
|
||||
@ -78,7 +102,7 @@ func main() {
|
||||
web.StartWeb(fp, cmdToExec, naked, uint16(port))
|
||||
|
||||
default:
|
||||
fmt.Println("witty (adduser|deluser|replay|run)")
|
||||
fmt.Println(subcmds)
|
||||
return
|
||||
}
|
||||
|
||||
|
38
term_conn/merge.go
Normal file
38
term_conn/merge.go
Normal file
@ -0,0 +1,38 @@
|
||||
package term_conn
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
func Merge(fnames []string, output string) {
|
||||
var all_recrods []writeRecord
|
||||
var records []writeRecord
|
||||
|
||||
for _, fname := range fnames {
|
||||
file, err := os.ReadFile(fname)
|
||||
if err != nil {
|
||||
log.Println("Failed to read users file", err)
|
||||
return
|
||||
}
|
||||
|
||||
err = json.Unmarshal(file, &records)
|
||||
|
||||
if err != nil {
|
||||
log.Println("Failed to parse json format", err, "for", fname)
|
||||
return
|
||||
}
|
||||
|
||||
all_recrods = append(all_recrods, records...)
|
||||
}
|
||||
|
||||
data, err := json.Marshal(all_recrods)
|
||||
|
||||
if err != nil {
|
||||
log.Println("Failed to merge into JSON format", err)
|
||||
return
|
||||
}
|
||||
|
||||
os.WriteFile(output, data, 0664)
|
||||
}
|
@ -279,9 +279,9 @@ out:
|
||||
|
||||
tc.record.Write([]byte("[")) // write a [ for an array of json objs
|
||||
|
||||
// write a dummy record to clear the screen.
|
||||
// write a dummy record
|
||||
tc.lastRecTime = time.Now()
|
||||
jbuf, _ := json.Marshal(WriteRecord{Dur: time.Since(tc.lastRecTime), Data: []byte("\033[2J\033[H")})
|
||||
jbuf, _ := json.Marshal(WriteRecord{Dur: time.Since(tc.lastRecTime), Data: []byte("")})
|
||||
tc.record.Write(jbuf)
|
||||
|
||||
} else {
|
||||
|
@ -68,4 +68,5 @@ func Replay(fname string, wait uint) {
|
||||
}
|
||||
|
||||
t.Write([]byte("\n\n---end of replay---\n\n"))
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user