Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 58 additions & 11 deletions cmd/logs.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package cmd

import (
"bufio"
"fmt"
"os"
"strings"

"rioctl/internal/transfer"
"rioctl/internal/ui"
"rioctl/internal/utils"

"github.com/spf13/cobra"
)
Expand All @@ -13,7 +17,7 @@
Use: "logs",
Short: "Log operations",
}

var defaultSource = "/media/sda1"
var logsPullCmd = &cobra.Command{
Use: "pull [files...]",
Short: "Pull logs from RoboRIO",
Expand All @@ -23,24 +27,28 @@
if err != nil {
return err
}
defer client.Close()

Check failure on line 30 in cmd/logs.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `client.Close` is not checked (errcheck)

source, _ := cmd.Flags().GetString("source")
// If no dir provided → prompt
if source == "" {
input, err := ui.RunTextInput("/media/sda1")
input, err := ui.RunTextInput(defaultSource)
if err != nil {
return err
}

if input == "" {
source = "/media/sda1"
source = defaultSource
} else {
source = input
}
}

dest, _ := cmd.Flags().GetString("dest")
eventCode, _ := cmd.Flags().GetString("event")
practice, _ := cmd.Flags().GetBool("practice")
qual, _ := cmd.Flags().GetBool("qual")
elim, _ := cmd.Flags().GetBool("elim")

var files []string

Expand All @@ -49,19 +57,54 @@
files = args
} else {
fmt.Println("Fetching file list...")

allFiles, err := client.ListFiles(source)
if err != nil {
return err
}
if eventCode != "" || practice || qual || elim {
allFiles, err := client.ListFiles(source)
if err != nil {
return err
}
var totalSize int64
for _, file := range allFiles {
if eventCode != "" && file.Event != eventCode {
continue
}
if !practice && !elim && !qual {
files = append(files, file.Name)
totalSize += file.Size
} else if practice && file.MatchType == "p" {
files = append(files, file.Name)
totalSize += file.Size
} else if qual && file.MatchType == "q" {
files = append(files, file.Name)
totalSize += file.Size
} else if elim && file.MatchType == "e" {
files = append(files, file.Name)
totalSize += file.Size
}
}

fmt.Printf("⚠️ This will pull %d files (%s). Continue? (yes/no): ", len(files), utils.Humanize(totalSize))

reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')

if strings.ToLower(text) != "yes\n" {
fmt.Println("Aborted.")
return nil
}

// Launch TUI selector
selected, err := ui.RunFilePicker(allFiles)
if err != nil {
return err
}
} else {
// Launch TUI selector
selected, err := ui.RunFilePicker(allFiles)
if err != nil {
return err
}

files = selected
files = selected
}
}

fmt.Println("Downloading selected files...")
Expand All @@ -70,8 +113,12 @@
}

func init() {
logsPullCmd.Flags().String("source", "/media/sda1", "Source directory")
logsPullCmd.Flags().String("source", "", "Source directory")
logsPullCmd.Flags().String("dest", "./logs", "Destination directory")
logsPullCmd.Flags().String("event", "", "Event Code to download Event only logs")
logsPullCmd.Flags().Bool("practice", false, "Pull Practice Match Logs")
logsPullCmd.Flags().Bool("qual", false, "Pull Qual Match Logs")
logsPullCmd.Flags().Bool("elim", false, "Pull Elim Match Logs")

logsCmd.AddCommand(logsPullCmd)
rootCmd.AddCommand(logsCmd)
Expand Down
19 changes: 14 additions & 5 deletions cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,18 @@
return nil, fmt.Errorf("team number is required")
}

host := teamToIP(team)

fmt.Printf("Connecting to %s as %s...\n", host, user)

return sshclient.New(host, user, pass)
hostOptions := []string{
teamToIP(team),
fmt.Sprintf("roboRIO-%d-FRC.local", team),
"172.22.11.2",
}
for _, host := range hostOptions {
fmt.Printf("Connecting to %s as %s...\n", host, user)
client, err := sshclient.New(host, user, pass)
if err == nil {
return client, nil
}
fmt.Printf("Connection to %s failed\n", host)
}
return nil, fmt.Errorf("Could not connect to RoboRio on any IP/Hostname")

Check failure on line 36 in cmd/ssh.go

View workflow job for this annotation

GitHub Actions / lint

ST1005: error strings should not be capitalized (staticcheck)
}
18 changes: 0 additions & 18 deletions cmd/test copy.go

This file was deleted.

42 changes: 0 additions & 42 deletions cmd/test.go

This file was deleted.

10 changes: 1 addition & 9 deletions cmd/usb.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
if err != nil {
return err
}
defer client.Close()

Check failure on line 25 in cmd/usb.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `client.Close` is not checked (errcheck)

out, err := client.Run("lsblk")
if err != nil {
Expand All @@ -42,7 +42,7 @@
if err != nil {
return err
}
defer client.Close()

Check failure on line 45 in cmd/usb.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `client.Close` is not checked (errcheck)

device, _ := cmd.Flags().GetString("device")
if device == "" {
Expand Down Expand Up @@ -79,20 +79,12 @@
}
fmt.Println(out)

mountCmdStr := fmt.Sprintf("mount %s /media/sda1", device)
out, err = client.Run(mountCmdStr)
out, err = client.Run("reboot")
if err != nil {
return err
}
fmt.Println(out)

out, err = client.Run("RESTART=yes /usr/local/frc/bin/frcKillRobot.sh")
if err != nil {
return err
}
fmt.Println(out)

fmt.Println(out)
return nil
},
}
Expand Down
21 changes: 19 additions & 2 deletions internal/sshclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sshclient

import (
"fmt"
"regexp"
"rioctl/internal/utils"
"slices"
"strconv"
Expand All @@ -10,6 +11,8 @@ import (
"github.com/melbahja/goph"
)

var fileRE = regexp.MustCompile(`^akit_.*_(\w+)_([peq])\d+\.wpilog$`)

type Client struct {
client *goph.Client
}
Expand Down Expand Up @@ -39,7 +42,7 @@ func (c *Client) Close() error {
}

func (c *Client) ListFiles(dir string) ([]utils.File, error) {
out, err := c.Run(fmt.Sprintf("ls -lh %s | grep .wpilog | awk '{ print $5\",\"$9 }'", dir))
out, err := c.Run(fmt.Sprintf("ls -l %s | grep .wpilog | awk '{ print $5\",\"$9 }'", dir))
if err != nil {
return nil, err
}
Expand All @@ -51,8 +54,22 @@ func (c *Client) ListFiles(dir string) ([]utils.File, error) {
if l == "" {
continue
}
eventCode := ""
matchType := ""
stringArr := strings.Split(l, ",")
files = append(files, utils.File{Name: stringArr[1], Size: stringArr[0]})
name := stringArr[1]

size, err := strconv.ParseInt(strings.TrimSpace(stringArr[0]), 10, 64)
if err != nil {
continue
}
if fileRE.MatchString(name) {
matches := fileRE.FindStringSubmatch(name)
eventCode = matches[1]
matchType = matches[2]
}

files = append(files, utils.File{Name: name, Size: size, Event: eventCode, MatchType: matchType})
}
slices.Reverse(files)
return files, nil
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/filepicker.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func NewFilePicker(files []utils.File) model {
choices := make([]*item, len(files))

for i, f := range files {
it := &item{title: f.Name, size: f.Size}
it := &item{title: f.Name, size: f.HumanizedSize()}
items[i] = it
choices[i] = it
}
Expand Down
23 changes: 3 additions & 20 deletions internal/ui/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

var (
helpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#626262")).Render
yellow = lipgloss.Color("#FDFF8C")

Check failure on line 20 in internal/ui/progress.go

View workflow job for this annotation

GitHub Actions / lint

var yellow is unused (unused)
pink = lipgloss.Color("#FF7CCB")

Check failure on line 21 in internal/ui/progress.go

View workflow job for this annotation

GitHub Actions / lint

var pink is unused (unused)
)

type progressModel struct {
Expand Down Expand Up @@ -64,8 +64,8 @@
}
case utils.FileUpload:
m.currentFileBytes = msg.Current
m.totalFileBytes = msg.Size
m.currentFile = msg.Name
m.totalFileBytes = msg.File.Size
m.currentFile = msg.File.Name
if m.currentFileBytes >= m.totalFileBytes {
m.completedFiles = append(m.completedFiles, m.currentFile)
}
Expand All @@ -91,7 +91,7 @@
return tea.NewView("\n\n" + a +
fmt.Sprintf("Downloading %s (%d/%d)", m.currentFile, m.finished+1, m.total) + "\n\n" +
m.progress.ViewAs(current) + "\n\n" +
pad + helpStyle(fmt.Sprintf("%s / %s", humanize(m.currentFileBytes), humanize((m.totalFileBytes)))))
pad + helpStyle(fmt.Sprintf("%s / %s", utils.Humanize(m.currentFileBytes), utils.Humanize((m.totalFileBytes)))))
}

func RunProgress(total int, current <-chan utils.FileUpload, finished <-chan int) {
Expand All @@ -109,22 +109,5 @@
}
}()

p.Run()

Check failure on line 112 in internal/ui/progress.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `p.Run` is not checked (errcheck)
}

func RunProgress1(total int, stuff []string) {
m := NewProgress(total)
p := tea.NewProgram(m)

go func() {
for u := range stuff {
p.Send(u)
}
}()

p.Run()
}

func humanize(b int64) string {
return fmt.Sprintf("%.2f MB", float64(b)/1024/1024)
}
27 changes: 21 additions & 6 deletions internal/utils/file.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package utils

import "fmt"

type FileUpload struct {
Size int64
File File
// Size int64
Current int64
Name string
// Name string
}

func (m FileUpload) UpdateCurrent(current int64) FileUpload {
Expand All @@ -13,13 +16,25 @@ func (m FileUpload) UpdateCurrent(current int64) FileUpload {

func NewFile(size int64, current int64, name string) FileUpload {
return FileUpload{
Name: name,
File: File{
Name: name,
Size: size,
},
Current: current,
Size: size,
}
}

type File struct {
Size string
Name string
Size int64
Name string
Event string
MatchType string
}

func (f File) HumanizedSize() string {
return Humanize(f.Size)
}

func Humanize(b int64) string {
return fmt.Sprintf("%.2f MB", float64(b)/1024/1024)
}
Loading