Skip to content

Commit

Permalink
refactor and gh-actions
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonWaldherr authored Jun 16, 2024
1 parent c0d018f commit 755efa3
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 258 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.22'

- name: Build
run: go build -v ./cmd/zplgfa

- name: Test
run: go test -v ./...
204 changes: 98 additions & 106 deletions cmd/zplgfa/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,154 +19,146 @@ import (
"simonwaldherr.de/go/zplgfa"
)

func specialCmds(zebraCmdFlag, networkIpFlag, networkPortFlag string) bool {
var cmdSent bool
if networkIpFlag == "" {
return cmdSent
}
if strings.Contains(zebraCmdFlag, "cancel") {
if err := sendCancelCmdToZebra(networkIpFlag, networkPortFlag); err == nil {
cmdSent = true
}
}
if strings.Contains(zebraCmdFlag, "calib") {
if err := sendCalibCmdToZebra(networkIpFlag, networkPortFlag); err == nil {
cmdSent = true
}
func handleZebraCommands(cmd, ip, port string) bool {
if ip == "" {
return false
}
if strings.Contains(zebraCmdFlag, "feed") {
if err := sendFeedCmdToZebra(networkIpFlag, networkPortFlag); err == nil {
cmdSent = true
}

// Define a map of command strings to functions that return error only
cmdActionsErr := map[string]func(string, string) error{
"cancel": sendCancelCmdToZebra,
"calib": sendCalibCmdToZebra,
"feed": sendFeedCmdToZebra,
}
if strings.Contains(zebraCmdFlag, "info") {
info, err := getInfoFromZebra(networkIpFlag, networkPortFlag)
if err == nil {
fmt.Println(info)
cmdSent = true
}

// Define a map of command strings to functions that return (string, error)
cmdActionsStr := map[string]func(string, string) (string, error){
"info": getInfoFromZebra,
"config": getConfigFromZebra,
"diag": getDiagFromZebra,
}
if strings.Contains(zebraCmdFlag, "config") {
info, err := getConfigFromZebra(networkIpFlag, networkPortFlag)
if err == nil {
fmt.Println(info)
cmdSent = true

for key, action := range cmdActionsErr {
if strings.Contains(cmd, key) {
if err := action(ip, port); err == nil {
return true
}
}
}
if strings.Contains(zebraCmdFlag, "diag") {
info, err := getDiagFromZebra(networkIpFlag, networkPortFlag)
if err == nil {
fmt.Println(info)
cmdSent = true

for key, action := range cmdActionsStr {
if strings.Contains(cmd, key) {
result, err := action(ip, port)
if err == nil {
fmt.Println(result)
return true
}
}
}
return cmdSent

return false
}

func main() {
var filenameFlag string
var zebraCmdFlag string
var graphicTypeFlag string
var imageEditFlag string
var networkIpFlag string
var networkPortFlag string
var imageResizeFlag float64
var graphicType zplgfa.GraphicType

flag.StringVar(&filenameFlag, "file", "", "filename to convert to zpl")
flag.StringVar(&zebraCmdFlag, "cmd", "", "send special command to printer [cancel,calib,feed,info,config,diag]")
flag.StringVar(&graphicTypeFlag, "type", "CompressedASCII", "type of graphic field encoding")
flag.StringVar(&imageEditFlag, "edit", "", "manipulate the image [invert,monochrome]")
flag.StringVar(&networkIpFlag, "ip", "", "send zpl to printer")
flag.StringVar(&networkPortFlag, "port", "9100", "network port of printer")
flag.Float64Var(&imageResizeFlag, "resize", 1.0, "zoom/resize the image")

// load flag input arguments
flag.Parse()
func parseFlags() (string, string, string, string, string, string, float64) {
var filename, zebraCmd, graphicType, imageEdit, ip, port string
var resizeFactor float64

// send special commands to printer
cmdSent := specialCmds(zebraCmdFlag, networkIpFlag, networkPortFlag)
flag.StringVar(&filename, "file", "", "filename to convert to zpl")
flag.StringVar(&zebraCmd, "cmd", "", "send special command to printer [cancel,calib,feed,info,config,diag]")
flag.StringVar(&graphicType, "type", "CompressedASCII", "type of graphic field encoding")
flag.StringVar(&imageEdit, "edit", "", "manipulate the image [invert,monochrome]")
flag.StringVar(&ip, "ip", "", "send zpl to printer")
flag.StringVar(&port, "port", "9100", "network port of printer")
flag.Float64Var(&resizeFactor, "resize", 1.0, "zoom/resize the image")

// check input parameter
if filenameFlag == "" {
if cmdSent {
return
}
log.Printf("Warning: no input file specified\n")
return
}
flag.Parse()
return filename, zebraCmd, graphicType, imageEdit, ip, port, resizeFactor
}

// open file
file, err := os.Open(filenameFlag)
func openImageFile(filename string) (image.Image, image.Config, error) {
file, err := os.Open(filename)
if err != nil {
log.Printf("Warning: could not open the file \"%s\": %s\n", filenameFlag, err)
return
return nil, image.Config{}, fmt.Errorf("could not open the file \"%s\": %s", filename, err)
}

// close file when complete
defer file.Close()

// load image head information
config, format, err := image.DecodeConfig(file)
if err != nil {
log.Printf("Warning: image not compatible, format: %s, config: %v, error: %s\n", format, config, err)
return nil, config, fmt.Errorf("image not compatible, format: %s, config: %v, error: %s", format, config, err)
}

// reset file pointer to the beginning of the file
file.Seek(0, 0)

// load and decode image
img, _, err := image.Decode(file)
if err != nil {
log.Printf("Warning: could not decode the file, %s\n", err)
return
return nil, config, fmt.Errorf("could not decode the file, %s", err)
}

// select graphic field type
switch strings.ToUpper(graphicTypeFlag) {
case "ASCII":
graphicType = zplgfa.ASCII
case "BINARY":
graphicType = zplgfa.Binary
case "COMPRESSEDASCII":
graphicType = zplgfa.CompressedASCII
default:
graphicType = zplgfa.CompressedASCII
}
return img, config, nil
}

// apply image manipulation functions
if strings.Contains(imageEditFlag, "monochrome") {
func processImage(img image.Image, editFlag string, resizeFactor float64, config image.Config) image.Image {
if strings.Contains(editFlag, "monochrome") {
img = editImageMonochrome(img)
}
if strings.Contains(imageEditFlag, "blur") {
if strings.Contains(editFlag, "blur") {
img = blur.Gaussian(img, float64(config.Width)/300)
}
if strings.Contains(imageEditFlag, "edge") {
if strings.Contains(editFlag, "edge") {
img = effect.Sobel(img)
}
if strings.Contains(imageEditFlag, "segment") {
if strings.Contains(editFlag, "segment") {
img = segment.Threshold(img, 128)
}
if strings.Contains(imageEditFlag, "invert") {
if strings.Contains(editFlag, "invert") {
img = editImageInvert(img)
}

// resize image
if imageResizeFlag != 1.0 {
img = resize.Resize(uint(float64(config.Width)*imageResizeFlag), uint(float64(config.Height)*imageResizeFlag), img, resize.MitchellNetravali)
if resizeFactor != 1.0 {
img = resize.Resize(uint(float64(config.Width)*resizeFactor), uint(float64(config.Height)*resizeFactor), img, resize.MitchellNetravali)
}

// flatten image
flat := zplgfa.FlattenImage(img)
return img
}

func getGraphicType(typeFlag string) zplgfa.GraphicType {
switch strings.ToUpper(typeFlag) {
case "ASCII":
return zplgfa.ASCII
case "BINARY":
return zplgfa.Binary
case "COMPRESSEDASCII":
return zplgfa.CompressedASCII
default:
return zplgfa.CompressedASCII
}
}

func main() {
filename, zebraCmd, graphicTypeFlag, imageEdit, ip, port, resizeFactor := parseFlags()

// convert image to zpl compatible type
gfimg := zplgfa.ConvertToZPL(flat, graphicType)
if handleZebraCommands(zebraCmd, ip, port) && filename == "" {
return
}

if filename == "" {
log.Printf("Warning: no input file specified\n")
return
}

img, config, err := openImageFile(filename)
if err != nil {
log.Printf("Warning: %s\n", err)
return
}

img = processImage(img, imageEdit, resizeFactor, config)

flat := zplgfa.FlattenImage(img)
gfimg := zplgfa.ConvertToZPL(flat, getGraphicType(graphicTypeFlag))

if networkIpFlag != "" {
// send zpl to printer
sendDataToZebra(networkIpFlag, networkPortFlag, gfimg)
if ip != "" {
sendDataToZebra(ip, port, gfimg)
} else {
// output zpl with graphic field data to stdout
fmt.Println(gfimg)
}
}
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ func ExampleConvertToZPL() {

fmt.Println(zplstr)

// Output: ^XA,^FS^FO0,0^GFA,52,51,3,FFFF00::FE3F00::FFFF00FFE300::FFFF00E22300::FFFF00::^FS,^XZ
// Output: ^XA,^FS^FO0,0^GFA,52,51,3,FFFF80::FE3F80::FFFF80FFE380::FFFF80E22380::FFFF80::^FS,^XZ
}
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module simonwaldherr.de/go/zplgfa

go 1.22

require (
github.com/anthonynsimon/bild v0.13.0
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
)
35 changes: 35 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/anthonynsimon/bild v0.13.0 h1:mN3tMaNds1wBWi1BrJq0ipDBhpkooYfu7ZFSMhXt1C8=
github.com/anthonynsimon/bild v0.13.0/go.mod h1:tpzzp0aYkAsMi1zmfhimaDyX1xjn2OUc1AJZK/TF0AE=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/image v0.0.0-20190703141733-d6a02ce849c9/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Loading

0 comments on commit 755efa3

Please sign in to comment.