feat: Initial revision

This commit is contained in:
Adrian Rumpold
2022-07-04 06:51:34 +02:00
commit 0054a2d9ae
13 changed files with 468 additions and 0 deletions

64
cmd/client.go Normal file
View File

@@ -0,0 +1,64 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"context"
"net"
"net/http"
"github.com/AdrianoKF/go-clip/internal/util"
"github.com/AdrianoKF/go-clip/pkg/model"
n "github.com/AdrianoKF/go-clip/pkg/net"
"github.com/spf13/cobra"
"golang.design/x/clipboard"
)
// clientCmd represents the client command
var clientCmd = &cobra.Command{
Use: "client",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
util.Logger.Info("client starting")
addr := net.UDPAddr{
IP: net.IPv4(239, 255, 90, 90),
Port: 9090,
}
client := n.NewClient(addr, nil)
util.Logger.Info("Watching for clipboard events")
ch := clipboard.Watch(context.TODO(), clipboard.FmtText)
for data := range ch {
util.Logger.Info("Received clipboard event: ", data)
bytes := []byte(data)
client.SendEvent(model.ClipboardUpdated{
Content: bytes,
ContentType: http.DetectContentType(bytes),
})
}
},
}
func init() {
rootCmd.AddCommand(clientCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// clientCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// clientCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

46
cmd/root.go Normal file
View File

@@ -0,0 +1,46 @@
/*
Copyright © 2022 Adrian Rumpold <a.rumpold@gmail.com>
*/
package cmd
import (
"os"
"github.com/spf13/cobra"
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "go-clip",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.go-clip.yaml)")
// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

69
cmd/server.go Normal file
View File

@@ -0,0 +1,69 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"bytes"
"encoding/json"
"net"
"strings"
"github.com/spf13/cobra"
"golang.design/x/clipboard"
"github.com/AdrianoKF/go-clip/internal/util"
"github.com/AdrianoKF/go-clip/pkg/model"
n "github.com/AdrianoKF/go-clip/pkg/net"
)
func PrintMessage(_ *net.UDPAddr, _ int, buf []byte) {
var ev model.ClipboardUpdated
err := json.NewDecoder(bytes.NewReader(buf)).Decode(&ev)
if err != nil {
util.Logger.Error(err)
}
if strings.HasPrefix(ev.ContentType, "text/") {
util.Logger.Info(string(ev.Content))
clipboard.Write(clipboard.FmtText, ev.Content)
} else {
util.Logger.Info(ev)
}
}
// serverCmd represents the server command
var serverCmd = &cobra.Command{
Use: "server",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
addr := net.UDPAddr{
IP: net.IPv4(239, 255, 90, 90),
Port: 9090,
}
server := n.NewServer(addr, PrintMessage)
util.Logger.Info("Starting server: %+v", server)
server.Listen()
},
}
func init() {
rootCmd.AddCommand(serverCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// server.goCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// server.goCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}