Skip to content

Commit

Permalink
Update mistex platforms to use new CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
wizzomafizzo committed Nov 2, 2024
1 parent 88f19a2 commit 172c8a8
Showing 1 changed file with 27 additions and 116 deletions.
143 changes: 27 additions & 116 deletions cmd/mistex/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build linux && cgo
///go:build linux && cgo

/*
TapTo
Expand All @@ -24,31 +24,21 @@ along with TapTo. If not, see <http://www.gnu.org/licenses/>.
package main

import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"net/url"
"github.com/wizzomafizzo/tapto/pkg/cli"
"github.com/wizzomafizzo/tapto/pkg/platforms/mistex"
"github.com/wizzomafizzo/tapto/pkg/utils"
"os"
"os/exec"
"strings"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"

"github.com/wizzomafizzo/tapto/pkg/config"
"github.com/wizzomafizzo/tapto/pkg/platforms/mister"
"github.com/wizzomafizzo/tapto/pkg/platforms/mistex"
"github.com/wizzomafizzo/tapto/pkg/service"
"github.com/wizzomafizzo/tapto/pkg/service/api"
"github.com/wizzomafizzo/tapto/pkg/utils"

"github.com/wizzomafizzo/tapto/pkg/config"
)

const appName = "tapto"

func tryAddToStartup() (bool, error) {
unitPath := "/etc/systemd/system/tapto.service"
unitFile := `[Unit]
Expand Down Expand Up @@ -88,132 +78,53 @@ WantedBy=multi-user.target
return true, nil
}

func handleWriteCommand(textToWrite string, svc *mister.Service, cfg *config.UserConfig) {
log.Info().Msgf("writing text to tag: %s", textToWrite)

if !svc.Running() {
_, _ = fmt.Fprintln(os.Stderr, "TapTo service is not running, please start it before writing.")
log.Error().Msg("TapTo service is not running, exiting")
os.Exit(1)
}

body, err := json.Marshal(api.ReaderWriteRequest{Text: textToWrite})
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, "Error encoding request:", err)
log.Error().Msgf("error encoding request: %s", err)
os.Exit(1)
}

resp, err := http.Post(
"http://127.0.0.1:"+cfg.Api.Port+"/api/v1/readers/0/write",
"application/json",
bytes.NewBuffer(body),
func main() {
flags := cli.SetupFlags()
serviceFlag := flag.String(
"service",
"",
"manage TapTo service (start|stop|restart|status)",
)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, "Error sending request:", err)
log.Error().Msgf("error sending request: %s", err)
os.Exit(1)
}
defer resp.Body.Close()
addStartupFlag := flag.Bool(
"add-startup",
false,
"add TapTo service to MiSTer startup if not already added",
)

pl := &mistex.Platform{}
flags.Pre(pl)

if resp.StatusCode != http.StatusOK {
errBody, err := io.ReadAll(resp.Body)
if *addStartupFlag {
_, err := tryAddToStartup()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, "Error reading response:", err)
log.Error().Msgf("error reading response: %s", err)
_, _ = fmt.Fprintf(os.Stderr, "Error adding to startup: %v\n", err)
os.Exit(1)
}

_, _ = fmt.Fprintf(os.Stderr, "Error writing to tag: %s\n", strings.TrimSpace(string(errBody)))
log.Error().Msgf("error writing to tag: %s", strings.TrimSpace(string(errBody)))
os.Exit(1)
}

_, _ = fmt.Fprintln(os.Stderr, "Successfully wrote to tag.")
log.Info().Msg("successfully wrote to tag")
os.Exit(0)
}

func handleLaunchCommand(tokenToLaunch string, svc *mister.Service, cfg *config.UserConfig) {
log.Info().Msgf("launching token: %s", tokenToLaunch)

if !svc.Running() {
_, _ = fmt.Fprintln(os.Stderr, "TapTo service is not running, please start it before launching.")
log.Error().Msg("TapTo service is not running, exiting")
os.Exit(1)
}

resp, err := http.Get("http://127.0.0.1:" + cfg.Api.Port + "/api/v1/launch/" + url.QueryEscape(tokenToLaunch))
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, "Error sending request:", err)
log.Error().Msgf("error sending request: %s", err)
os.Exit(1)
}
defer resp.Body.Close()

_, _ = fmt.Fprintln(os.Stderr, "Successfully launched token.")
log.Info().Msg("successfully launched token")
os.Exit(0)
}

func main() {
svcOpt := flag.String("service", "", "manage TapTo service (start, stop, restart, status)")
writeOpt := flag.String("write", "", "write text to tag")
launchOpt := flag.String("launch", "", "execute text as if it were a token")
versionOpt := flag.Bool("version", false, "print version and exit")
flag.Parse()

if *versionOpt {
fmt.Println("TapTo v" + config.Version + " (mistex)")
os.Exit(0)
}

pl := &mistex.Platform{}
err := utils.InitLogging(pl)
if err != nil {
fmt.Println("Error initializing logging:", err)
os.Exit(1)
}

cfg, err := config.NewUserConfig(appName, &config.UserConfig{
cfg := cli.Setup(pl, &config.UserConfig{
TapTo: config.TapToConfig{
ProbeDevice: true,
},
Api: config.ApiConfig{
Port: config.DefaultApiPort,
},
})
if err != nil {
log.Error().Msgf("error loading user config: %s", err)
fmt.Println("Error loading config:", err)
os.Exit(1)
}

if cfg.GetDebug() {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
} else {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}

svc, err := mister.NewService(mister.ServiceArgs{
Name: appName,
Entry: func() (func() error, error) {
return service.Start(pl, cfg)
},
})
if err != nil {
log.Error().Msgf("error creating service: %s", err)
fmt.Println("Error creating service:", err)
log.Error().Err(err).Msg("error creating service")
_, _ = fmt.Fprintf(os.Stderr, "Error creating service: %v\n", err)
os.Exit(1)
}
svc.ServiceHandler(serviceFlag)

if *writeOpt != "" {
handleWriteCommand(*writeOpt, svc, cfg)
} else if *launchOpt != "" {
handleLaunchCommand(*launchOpt, svc, cfg)
}

svc.ServiceHandler(svcOpt)
flags.Post(cfg)

fmt.Println("TapTo v" + config.Version)

Expand Down

0 comments on commit 172c8a8

Please sign in to comment.