2021-08-21 08:19:49 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-05-01 18:36:28 +00:00
|
|
|
"context"
|
2021-10-23 22:11:04 +00:00
|
|
|
"os"
|
2022-04-24 01:46:49 +00:00
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2022-08-30 20:01:36 +00:00
|
|
|
"time"
|
2021-08-21 08:19:49 +00:00
|
|
|
|
2022-02-25 05:26:40 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"go.arsenm.dev/itd/api"
|
2023-01-04 23:17:14 +00:00
|
|
|
"go.arsenm.dev/logger"
|
2023-01-04 23:06:05 +00:00
|
|
|
"go.arsenm.dev/logger/log"
|
2021-08-21 08:19:49 +00:00
|
|
|
)
|
|
|
|
|
2022-02-25 05:26:40 +00:00
|
|
|
var client *api.Client
|
2021-08-21 08:19:49 +00:00
|
|
|
|
|
|
|
func main() {
|
2023-01-04 23:17:14 +00:00
|
|
|
log.Logger = logger.NewPretty(os.Stderr)
|
2022-02-25 05:26:40 +00:00
|
|
|
|
2022-05-01 18:36:28 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
ctx, _ = signal.NotifyContext(
|
|
|
|
ctx,
|
|
|
|
syscall.SIGINT,
|
|
|
|
syscall.SIGTERM,
|
|
|
|
)
|
2022-08-30 20:01:36 +00:00
|
|
|
|
2022-07-31 09:40:46 +00:00
|
|
|
// This goroutine ensures that itctl will exit
|
|
|
|
// at most 200ms after the user sends SIGINT/SIGTERM.
|
2022-07-31 09:22:33 +00:00
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
2022-08-30 20:01:36 +00:00
|
|
|
time.Sleep(200 * time.Millisecond)
|
2022-07-31 09:22:33 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}()
|
2022-05-01 18:36:28 +00:00
|
|
|
|
2022-02-25 05:26:40 +00:00
|
|
|
app := cli.App{
|
2022-08-30 20:01:36 +00:00
|
|
|
Name: "itctl",
|
2022-07-31 09:15:42 +00:00
|
|
|
HideHelpCommand: true,
|
2022-02-25 05:26:40 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "socket-path",
|
|
|
|
Aliases: []string{"s"},
|
|
|
|
Value: api.DefaultAddr,
|
|
|
|
Usage: "Path to itd socket",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Commands: []*cli.Command{
|
2022-07-31 09:15:42 +00:00
|
|
|
{
|
2022-08-30 20:01:36 +00:00
|
|
|
Name: "help",
|
2022-07-31 09:15:42 +00:00
|
|
|
ArgsUsage: "<command>",
|
2022-08-30 20:01:36 +00:00
|
|
|
Usage: "Display help screen for a command",
|
|
|
|
Action: helpCmd,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "resources",
|
|
|
|
Aliases: []string{"res"},
|
|
|
|
Usage: "Handle InfiniTime resource loading",
|
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
{
|
|
|
|
Name: "load",
|
|
|
|
ArgsUsage: "<path>",
|
|
|
|
Usage: "Load an InifiniTime resources package",
|
|
|
|
Action: resourcesLoad,
|
|
|
|
},
|
|
|
|
},
|
2022-07-31 09:15:42 +00:00
|
|
|
},
|
2022-02-25 05:26:40 +00:00
|
|
|
{
|
|
|
|
Name: "filesystem",
|
|
|
|
Aliases: []string{"fs"},
|
|
|
|
Usage: "Perform filesystem operations on the PineTime",
|
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
{
|
|
|
|
Name: "list",
|
|
|
|
ArgsUsage: "[dir]",
|
|
|
|
Aliases: []string{"ls"},
|
|
|
|
Usage: "List a directory",
|
|
|
|
Action: fsList,
|
|
|
|
},
|
|
|
|
{
|
2022-09-03 23:28:25 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "parents",
|
|
|
|
Aliases: []string{"p"},
|
|
|
|
Usage: "Make parent directories if needed, no error if already existing",
|
|
|
|
},
|
|
|
|
},
|
2022-02-25 05:26:40 +00:00
|
|
|
Name: "mkdir",
|
|
|
|
ArgsUsage: "<paths...>",
|
|
|
|
Usage: "Create new directories",
|
|
|
|
Action: fsMkdir,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "move",
|
|
|
|
ArgsUsage: "<old> <new>",
|
|
|
|
Aliases: []string{"mv"},
|
|
|
|
Usage: "Move a file or directory",
|
|
|
|
Action: fsMove,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "read",
|
|
|
|
ArgsUsage: `<remote path> <local path>`,
|
|
|
|
Usage: "Read a file from InfiniTime.",
|
|
|
|
Description: `Read is used to read files from InfiniTime's filesystem. A "-" can be used to signify stdout`,
|
|
|
|
Action: fsRead,
|
|
|
|
},
|
|
|
|
{
|
2022-09-03 23:28:25 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "recursive",
|
|
|
|
Aliases: []string{"r", "R"},
|
|
|
|
Usage: "Remove directories and their contents recursively",
|
|
|
|
},
|
|
|
|
},
|
2022-02-25 05:26:40 +00:00
|
|
|
Name: "remove",
|
|
|
|
ArgsUsage: "<paths...>",
|
|
|
|
Aliases: []string{"rm"},
|
2022-03-15 23:16:44 +00:00
|
|
|
Usage: "Remove a file from InfiniTime",
|
2022-02-25 05:26:40 +00:00
|
|
|
Action: fsRemove,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "write",
|
|
|
|
ArgsUsage: `<local path> <remote path>`,
|
|
|
|
Usage: "Write a file to InfiniTime",
|
|
|
|
Description: `Write is used to write files to InfiniTime's filesystem. A "-" can be used to signify stdin`,
|
|
|
|
Action: fsWrite,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "firmware",
|
|
|
|
Aliases: []string{"fw"},
|
|
|
|
Usage: "Manage InfiniTime firmware",
|
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
{
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.PathFlag{
|
|
|
|
Name: "init-packet",
|
|
|
|
Aliases: []string{"i"},
|
|
|
|
Usage: "Path to init packet (.dat file)",
|
|
|
|
},
|
|
|
|
&cli.PathFlag{
|
|
|
|
Name: "firmware",
|
|
|
|
Aliases: []string{"f"},
|
|
|
|
Usage: "Path to firmware image (.bin file)",
|
|
|
|
},
|
2022-10-16 20:17:12 +00:00
|
|
|
&cli.PathFlag{
|
|
|
|
Name: "resources",
|
|
|
|
Aliases: []string{"r"},
|
|
|
|
Usage: "Path to resources file (.zip file)",
|
|
|
|
},
|
2022-02-25 05:26:40 +00:00
|
|
|
&cli.PathFlag{
|
|
|
|
Name: "archive",
|
|
|
|
Aliases: []string{"a"},
|
|
|
|
Usage: "Path to firmware archive (.zip file)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Name: "upgrade",
|
|
|
|
Aliases: []string{"upg"},
|
|
|
|
Usage: "Upgrade InfiniTime firmware using files or archive",
|
|
|
|
Action: fwUpgrade,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "version",
|
|
|
|
Aliases: []string{"ver"},
|
|
|
|
Usage: "Get firmware version of InfiniTime",
|
|
|
|
Action: fwVersion,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "get",
|
|
|
|
Usage: "Get information from InfiniTime",
|
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
{
|
|
|
|
Name: "address",
|
|
|
|
Aliases: []string{"addr"},
|
|
|
|
Usage: "Get InfiniTime's bluetooth address",
|
|
|
|
Action: getAddress,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "battery",
|
|
|
|
Aliases: []string{"batt"},
|
|
|
|
Usage: "Get InfiniTime's battery percentage",
|
|
|
|
Action: getBattery,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "heart",
|
|
|
|
Usage: "Get heart rate from InfiniTime",
|
|
|
|
Action: getHeart,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{Name: "shell"},
|
|
|
|
},
|
|
|
|
Name: "motion",
|
|
|
|
Usage: "Get motion values from InfiniTime",
|
|
|
|
Action: getMotion,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "steps",
|
|
|
|
Usage: "Get step count from InfiniTime",
|
|
|
|
Action: getSteps,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "notify",
|
|
|
|
Usage: "Send notification to InfiniTime",
|
|
|
|
Action: notify,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "set",
|
|
|
|
Usage: "Set information on InfiniTime",
|
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
{
|
|
|
|
Name: "time",
|
|
|
|
ArgsUsage: `<ISO8601|"now">`,
|
|
|
|
Usage: "Set InfiniTime's clock to specified time",
|
|
|
|
Action: setTime,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "update",
|
|
|
|
Usage: "Update information on InfiniTime",
|
|
|
|
Aliases: []string{"upd"},
|
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
{
|
|
|
|
Name: "weather",
|
|
|
|
Usage: "Force an immediate update of weather data",
|
|
|
|
Action: updateWeather,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-04-24 01:46:49 +00:00
|
|
|
{
|
|
|
|
Name: "watch",
|
|
|
|
Usage: "Watch a value for changes",
|
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
{
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{Name: "json"},
|
|
|
|
&cli.BoolFlag{Name: "shell"},
|
|
|
|
},
|
|
|
|
Name: "heart",
|
|
|
|
Usage: "Watch heart rate value for changes",
|
|
|
|
Action: watchHeart,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{Name: "json"},
|
|
|
|
&cli.BoolFlag{Name: "shell"},
|
|
|
|
},
|
|
|
|
Name: "steps",
|
|
|
|
Usage: "Watch step count value for changes",
|
|
|
|
Action: watchStepCount,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{Name: "json"},
|
|
|
|
&cli.BoolFlag{Name: "shell"},
|
|
|
|
},
|
|
|
|
Name: "motion",
|
|
|
|
Usage: "Watch motion coordinates for changes",
|
|
|
|
Action: watchMotion,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{Name: "json"},
|
|
|
|
&cli.BoolFlag{Name: "shell"},
|
|
|
|
},
|
|
|
|
Name: "battery",
|
|
|
|
Aliases: []string{"batt"},
|
|
|
|
Usage: "Watch battery level value for changes",
|
|
|
|
Action: watchBattLevel,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-02-25 05:26:40 +00:00
|
|
|
},
|
|
|
|
Before: func(c *cli.Context) error {
|
2022-07-31 09:15:42 +00:00
|
|
|
if !isHelpCmd() {
|
|
|
|
newClient, err := api.New(c.String("socket-path"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
client = newClient
|
2022-02-25 05:26:40 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
After: func(*cli.Context) error {
|
2022-04-02 22:20:31 +00:00
|
|
|
if client != nil {
|
|
|
|
client.Close()
|
|
|
|
}
|
|
|
|
return nil
|
2022-02-25 05:26:40 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-05-01 18:36:28 +00:00
|
|
|
err := app.RunContext(ctx, os.Args)
|
2022-02-25 05:26:40 +00:00
|
|
|
if err != nil {
|
2023-01-04 23:06:05 +00:00
|
|
|
log.Fatal("Error while running app").Err(err).Send()
|
2022-02-25 05:26:40 +00:00
|
|
|
}
|
2021-08-21 08:19:49 +00:00
|
|
|
}
|
2022-07-31 09:15:42 +00:00
|
|
|
|
|
|
|
func helpCmd(c *cli.Context) error {
|
|
|
|
cmdArgs := append([]string{os.Args[0]}, c.Args().Slice()...)
|
|
|
|
cmdArgs = append(cmdArgs, "-h")
|
|
|
|
return c.App.RunContext(c.Context, cmdArgs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isHelpCmd() bool {
|
|
|
|
if len(os.Args) == 1 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, arg := range os.Args {
|
|
|
|
if arg == "-h" || arg == "help" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2022-08-30 20:01:36 +00:00
|
|
|
}
|