Commit 1c98300

Karn Wong <karn@karnwong.me>
2024-03-03 13:54:33
add generate qrcode
1 parent c6811a9
cmd/generate/generate_qrcode.go
@@ -0,0 +1,60 @@
+package generate
+
+import (
+	"fmt"
+	"os"
+
+	"github.com/kahnwong/swissknife/cmd/utils"
+
+	qrcode "github.com/skip2/go-qrcode"
+	"github.com/spf13/cobra"
+)
+
+// helpers
+func generateQRCode(url string) (string, error) {
+	// init
+	var q *qrcode.QRCode
+	q, err := qrcode.New(url, qrcode.Medium)
+	if err != nil {
+		return "", err
+	}
+
+	// generate pdf
+	png, err := q.PNG(1024)
+	if err != nil {
+		return "", err
+	}
+
+	// copy to clipboard
+	utils.CopyToClipboardImage(png)
+
+	// for stdout
+	//content := q.ToString(false)
+	content := q.ToSmallString(false)
+
+	return content, nil
+}
+
+var generateQRCodeCmd = &cobra.Command{
+	Use:   "qrcode",
+	Short: "Generate QR code",
+	Long:  `Generate QR code from URL and copy resulting image to clipboard.`,
+	Run: func(cmd *cobra.Command, args []string) {
+		//init
+		if len(args) == 0 {
+			fmt.Println("Please specify URL")
+			os.Exit(1)
+		}
+
+		// main
+		qrcodeStr, err := generateQRCode(args[0])
+		if err != nil {
+			fmt.Print(err)
+		}
+		fmt.Println(qrcodeStr)
+	},
+}
+
+func init() {
+	Cmd.AddCommand(generateQRCodeCmd)
+}
cmd/generate/generate_ssh_key.go
@@ -16,7 +16,7 @@ import (
 )
 
 // helpers
-func writeStringToFile(filePath, data string, permission os.FileMode) {
+func writeStringToFile(filePath string, data string, permission os.FileMode) {
 	file, err := os.Create(filePath)
 	if err != nil {
 		log.Fatal(err)
cmd/utils/copy_to_clipboard.go
@@ -5,8 +5,15 @@ import (
 )
 
 func CopyToClipboard(text string) {
-	err := clipboard.Init()
-	if err == nil { // clipboard doesn't work from ssh session
+	err := clipboard.Init() // clipboard doesn't work from ssh session
+	if err == nil {         // clipboard doesn't work from ssh session
 		clipboard.Write(clipboard.FmtText, []byte(text))
 	}
 }
+
+func CopyToClipboardImage(bytes []byte) {
+	err := clipboard.Init()
+	if err == nil {
+		clipboard.Write(clipboard.FmtImage, bytes)
+	}
+}
go.mod
@@ -7,6 +7,7 @@ require (
 	github.com/libp2p/go-netroute v0.2.1
 	github.com/sethvargo/go-diceware v0.3.0
 	github.com/sethvargo/go-password v0.2.0
+	github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
 	github.com/spf13/cobra v1.8.0
 	golang.design/x/clipboard v0.7.0
 	golang.org/x/crypto v0.19.0
go.sum
@@ -18,6 +18,8 @@ github.com/sethvargo/go-diceware v0.3.0 h1:UVVEfmN/uF50JfWAN7nbY6CiAlp5xeSx+5U0l
 github.com/sethvargo/go-diceware v0.3.0/go.mod h1:lH5Q/oSPMivseNdhMERAC7Ti5oOPqsaVddU1BcN1CY0=
 github.com/sethvargo/go-password v0.2.0 h1:BTDl4CC/gjf/axHMaDQtw507ogrXLci6XRiLc7i/UHI=
 github.com/sethvargo/go-password v0.2.0/go.mod h1:Ym4Mr9JXLBycr02MFuVQ/0JHidNetSgbzutTr3zsYXE=
+github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
+github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M=
 github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
 github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
 github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=