Skip to content

Commit 1b0150a

Browse files
committed
Reorganized project
1 parent 59d8ca1 commit 1b0150a

File tree

10 files changed

+108
-54
lines changed

10 files changed

+108
-54
lines changed

Taskfile.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ vars:
2222
LDFLAGS: >
2323
-ldflags
2424
'
25-
-X {{.CONFIGURATION_PACKAGE}}.Tag={{.VERSION}}
25+
-X {{.CONFIGURATION_PACKAGE}}.Version={{.VERSION}}
26+
-X {{.CONFIGURATION_PACKAGE}}.Commit={{.COMMIT}}
2627
-X {{.CONFIGURATION_PACKAGE}}.Timestamp={{.TIMESTAMP}}
2728
'
2829

args.go renamed to args/args.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,24 @@
1515
// a commercial license, send an email to license@arduino.cc.
1616
//
1717

18-
package main
18+
package args
1919

2020
import (
2121
"fmt"
2222
"os"
2323
)
2424

25-
var args struct {
26-
showVersion bool
27-
}
25+
// ShowVersion FIXMEDOC
26+
var ShowVersion bool
2827

29-
func parseArgs() {
28+
// Parse arguments passed by the user
29+
func Parse() {
3030
for _, arg := range os.Args[1:] {
3131
if arg == "" {
3232
continue
3333
}
3434
if arg == "-v" || arg == "--version" {
35-
args.showVersion = true
35+
ShowVersion = true
3636
continue
3737
}
3838
fmt.Fprintf(os.Stderr, "invalid argument: %s\n", arg)

main.go

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ import (
2121
"fmt"
2222
"os"
2323

24-
"github.com/arduino/go-properties-orderedmap"
2524
discovery "github.com/arduino/pluggable-discovery-protocol-handler/v2"
25+
"github.com/arduino/serial-discovery/args"
26+
"github.com/arduino/serial-discovery/sync"
2627
"github.com/arduino/serial-discovery/version"
27-
"go.bug.st/serial/enumerator"
2828
)
2929

3030
func main() {
31-
parseArgs()
32-
if args.showVersion {
33-
fmt.Printf("serial-discovery %s (build timestamp: %s)\n", version.Tag, version.Timestamp)
31+
args.Parse()
32+
if args.ShowVersion {
33+
fmt.Printf("%s\n", version.VersionInfo)
3434
return
3535
}
3636

@@ -68,29 +68,10 @@ func (d *SerialDiscovery) Stop() error {
6868

6969
// StartSync is the handler for the pluggable-discovery START_SYNC command
7070
func (d *SerialDiscovery) StartSync(eventCB discovery.EventCallback, errorCB discovery.ErrorCallback) error {
71-
close, err := startSync(eventCB, errorCB)
71+
close, err := sync.Start(eventCB, errorCB)
7272
if err != nil {
7373
return err
7474
}
7575
d.closeChan = close
7676
return nil
7777
}
78-
79-
func toDiscoveryPort(port *enumerator.PortDetails) *discovery.Port {
80-
protocolLabel := "Serial Port"
81-
props := properties.NewMap()
82-
if port.IsUSB {
83-
protocolLabel += " (USB)"
84-
props.Set("vid", "0x"+port.VID)
85-
props.Set("pid", "0x"+port.PID)
86-
props.Set("serialNumber", port.SerialNumber)
87-
}
88-
res := &discovery.Port{
89-
Address: port.Name,
90-
AddressLabel: port.Name,
91-
Protocol: "serial",
92-
ProtocolLabel: protocolLabel,
93-
Properties: props,
94-
}
95-
return res
96-
}

sync.go renamed to sync/sync.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,21 @@
1515
// a commercial license, send an email to license@arduino.cc.
1616
//
1717

18-
package main
18+
package sync
1919

2020
import (
21+
"github.com/arduino/go-properties-orderedmap"
2122
discovery "github.com/arduino/pluggable-discovery-protocol-handler/v2"
2223
"go.bug.st/serial/enumerator"
2324
)
2425

25-
// ProcessUpdates sends 'add' and 'remove' events by comparing two ports enumeration
26+
// processUpdates sends 'add' and 'remove' events by comparing two ports enumeration
2627
// made at different times:
2728
// - ports present in the new list but not in the old list are reported as 'added'
2829
// - ports present in the old list but not in the new list are reported as 'removed'
29-
func ProcessUpdates(old, new []*enumerator.PortDetails, eventCB discovery.EventCallback) {
30+
func processUpdates(old, new []*enumerator.PortDetails, eventCB discovery.EventCallback) {
3031
for _, oldPort := range old {
31-
if !PortListHas(new, oldPort) {
32+
if !portListHas(new, oldPort) {
3233
eventCB("remove", &discovery.Port{
3334
Address: oldPort.Name,
3435
Protocol: "serial",
@@ -37,15 +38,15 @@ func ProcessUpdates(old, new []*enumerator.PortDetails, eventCB discovery.EventC
3738
}
3839

3940
for _, newPort := range new {
40-
if !PortListHas(old, newPort) {
41+
if !portListHas(old, newPort) {
4142
eventCB("add", toDiscoveryPort(newPort))
4243
}
4344
}
4445
}
4546

46-
// PortListHas checks if port is contained in list. The port metadata are
47+
// portListHas checks if port is contained in list. The port metadata are
4748
// compared in particular the port address, and vid/pid if the port is a usb port.
48-
func PortListHas(list []*enumerator.PortDetails, port *enumerator.PortDetails) bool {
49+
func portListHas(list []*enumerator.PortDetails, port *enumerator.PortDetails) bool {
4950
for _, p := range list {
5051
if port.Name == p.Name && port.IsUSB == p.IsUSB {
5152
if p.IsUSB &&
@@ -61,3 +62,22 @@ func PortListHas(list []*enumerator.PortDetails, port *enumerator.PortDetails) b
6162
}
6263
return false
6364
}
65+
66+
func toDiscoveryPort(port *enumerator.PortDetails) *discovery.Port {
67+
protocolLabel := "Serial Port"
68+
props := properties.NewMap()
69+
if port.IsUSB {
70+
protocolLabel += " (USB)"
71+
props.Set("vid", "0x"+port.VID)
72+
props.Set("pid", "0x"+port.PID)
73+
props.Set("serialNumber", port.SerialNumber)
74+
}
75+
res := &discovery.Port{
76+
Address: port.Name,
77+
AddressLabel: port.Name,
78+
Protocol: "serial",
79+
ProtocolLabel: protocolLabel,
80+
Properties: props,
81+
}
82+
return res
83+
}

sync_darwin.go renamed to sync/sync_darwin.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// a commercial license, send an email to license@arduino.cc.
1616
//
1717

18-
package main
18+
package sync
1919

2020
import (
2121
"fmt"
@@ -25,7 +25,10 @@ import (
2525
"go.bug.st/serial/enumerator"
2626
)
2727

28-
func startSync(eventCB discovery.EventCallback, errorCB discovery.ErrorCallback) (chan<- bool, error) {
28+
// Start the sync process, sucessful events will be passed to eventCB, errors to errorCB.
29+
// Returns a channel used to stop the sync process.
30+
// Returns error if sync process can't be started.
31+
func Start(eventCB discovery.EventCallback, errorCB discovery.ErrorCallback) (chan<- bool, error) {
2932
// create kqueue
3033
kq, err := syscall.Kqueue()
3134
if err != nil {
@@ -95,7 +98,7 @@ func startSync(eventCB discovery.EventCallback, errorCB discovery.ErrorCallback)
9598
enumeratorErr = err
9699
break
97100
}
98-
ProcessUpdates(current, updates, eventCB)
101+
processUpdates(current, updates, eventCB)
99102
current = updates
100103
}
101104
if enumeratorErr != nil {

sync_default.go renamed to sync/sync_default.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717

1818
// +build !linux,!windows,!darwin
1919

20-
package main
20+
package sync
2121

2222
import "fmt"
2323

24-
func startSync() (chan<- bool, error) {
24+
// Start fallback implementation
25+
func Start() (chan<- bool, error) {
2526
return nil, fmt.Errorf("Command START_SYNC not supported")
2627
}

sync_linux.go renamed to sync/sync_linux.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// a commercial license, send an email to license@arduino.cc.
1616
//
1717

18-
package main
18+
package sync
1919

2020
import (
2121
"fmt"
@@ -25,7 +25,10 @@ import (
2525
"go.bug.st/serial/enumerator"
2626
)
2727

28-
func startSync(eventCB discovery.EventCallback, errorCB discovery.ErrorCallback) (chan<- bool, error) {
28+
// Start the sync process, sucessful events will be passed to eventCB, errors to errorCB.
29+
// Returns a channel used to stop the sync process.
30+
// Returns error if sync process can't be started.
31+
func Start(eventCB discovery.EventCallback, errorCB discovery.ErrorCallback) (chan<- bool, error) {
2932
// Get the current port list to send as initial "add" events
3033
current, err := enumerator.GetDetailedPortsList()
3134
if err != nil {

sync_windows.go renamed to sync/sync_windows.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// a commercial license, send an email to license@arduino.cc.
1616
//
1717

18-
package main
18+
package sync
1919

2020
import (
2121
"fmt"
@@ -102,7 +102,10 @@ func init() {
102102
runtime.LockOSThread()
103103
}
104104

105-
func startSync(eventCB discovery.EventCallback, errorCB discovery.ErrorCallback) (chan<- bool, error) {
105+
// Start the sync process, sucessful events will be passed to eventCB, errors to errorCB.
106+
// Returns a channel used to stop the sync process.
107+
// Returns error if sync process can't be started.
108+
func Start(eventCB discovery.EventCallback, errorCB discovery.ErrorCallback) (chan<- bool, error) {
106109
startResult := make(chan error)
107110
event := make(chan bool, 1)
108111
go func() {
@@ -139,7 +142,7 @@ func startSync(eventCB discovery.EventCallback, errorCB discovery.ErrorCallback)
139142
errorCB(fmt.Sprintf("Error enumerating serial ports: %s", err))
140143
return
141144
}
142-
ProcessUpdates(current, updates, eventCB)
145+
processUpdates(current, updates, eventCB)
143146
current = updates
144147
}
145148
}()

zsyscall_windows.go renamed to sync/zsyscall_windows.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

version/version.go

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,50 @@
1717

1818
package version
1919

20-
// Tag is the current git tag
21-
var Tag = "snapshot"
20+
import (
21+
"fmt"
22+
"os"
23+
"path/filepath"
24+
)
2225

23-
// Timestamp is the current timestamp
24-
var Timestamp = "unknown"
26+
// VersionInfo FIXMEDOC
27+
var VersionInfo = newInfo(filepath.Base(os.Args[0]))
28+
29+
var (
30+
defaultVersionString = "0.0.0-git"
31+
// Version FIXMEDOC
32+
Version = ""
33+
// Commit FIXMEDOC
34+
Commit = ""
35+
// Timestamp FIXMEDOC
36+
Timestamp = ""
37+
)
38+
39+
// Info FIXMEDOC
40+
type Info struct {
41+
Application string `json:"Application"`
42+
VersionString string `json:"VersionString"`
43+
Commit string `json:"Commit"`
44+
Date string `json:"Date"`
45+
}
46+
47+
// NewInfo FIXMEDOC
48+
func newInfo(application string) *Info {
49+
return &Info{
50+
Application: application,
51+
VersionString: Version,
52+
Commit: Commit,
53+
Date: Timestamp,
54+
}
55+
}
56+
57+
func (i *Info) String() string {
58+
return fmt.Sprintf("%s Version: %s Commit: %s Date: %s", i.Application, i.VersionString, i.Commit, i.Date)
59+
}
60+
61+
//nolint:gochecknoinits
62+
func init() {
63+
if Version == "" {
64+
Version = defaultVersionString
65+
}
66+
}

0 commit comments

Comments
 (0)