From 446637c6bd55d149a5aa4d2fa9e7c9c789fea727 Mon Sep 17 00:00:00 2001 From: eerkkk Date: Fri, 1 Sep 2023 18:15:54 -0500 Subject: [PATCH] Add github action build and release workflows for synse modbus ip plugin --- .github/workflows/build.yml | 46 +++++++++++++++++++++++++++++++++ .github/workflows/release.yml | 33 +++++++++++++++++++++++ .goreleaser.yml | 2 +- pkg/devices/common.go | 14 +++++----- pkg/devices/device_test.go | 11 ++++---- pkg/devices/holding_register.go | 2 +- pkg/devices/input_register.go | 6 ++--- pkg/utils/utils.go | 2 +- 8 files changed, 97 insertions(+), 19 deletions(-) create mode 100644 .github/workflows/build.yml create mode 100644 .github/workflows/release.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..dd40f0d --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,46 @@ +name: build +on: + push: + branches: + - '*' + pull_request: {} + +jobs: + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-go@v3 + with: + go-version-file: 'go.mod' + - run: "go vet ./..." + - name: Lint + uses: dominikh/staticcheck-action@v1.3.0 + with: + version: "2022.1.3" + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-go@v3 + with: + go-version-file: 'go.mod' + - run: "make test" + snapshot-build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - uses: actions/setup-go@v3 + with: + go-version-file: 'go.mod' + - name: Set GOLANG_VERSION + run: | + echo "GOLANG_VERSION=$(go version | awk '{ print $3 }')" >> $GITHUB_ENV + - name: Snapshot Build + uses: goreleaser/goreleaser-action@v4 + with: + distribution: goreleaser + version: latest + args: release --snapshot --skip-publish --clean \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..bbc6cbe --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,33 @@ +name: release +on: + push: + tags: + - 'v*' + +jobs: + goreleaser: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - uses: actions/setup-go@v3 + with: + go-version-file: 'go.mod' + - name: Set GOLANG_VERSION + run: | + echo "GOLANG_VERSION=$(go version | awk '{ print $3 }')" >> $GITHUB_ENV + - name: Login to Docker Hub + uses: docker/login-action@v2 + with: + registry: docker.io + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - name: Tagged Release + uses: goreleaser/goreleaser-action@v3 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + distribution: goreleaser + version: latest + args: release --clean \ No newline at end of file diff --git a/.goreleaser.yml b/.goreleaser.yml index 21a92df..c73c623 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -12,7 +12,7 @@ builds: - -X github.com/vapor-ware/synse-sdk/sdk.BuildDate={{ .Date }} - -X github.com/vapor-ware/synse-sdk/sdk.GitCommit={{ .ShortCommit }} - -X github.com/vapor-ware/synse-sdk/sdk.GitTag={{ .Tag }} - - -X github.com/vapor-ware/synse-sdk/sdk.GoVersion={{ .Env.GOVERSION }} + - -X github.com/vapor-ware/synse-sdk/sdk.GoVersion={{ .Env.GOLANG_VERSION }} - -X github.com/vapor-ware/synse-sdk/sdk.PluginVersion={{ .Version }} goos: - linux diff --git a/pkg/devices/common.go b/pkg/devices/common.go index 53449f8..5d86fba 100644 --- a/pkg/devices/common.go +++ b/pkg/devices/common.go @@ -420,11 +420,11 @@ func MapBulkReadData(bulkReadMap map[ModbusBulkReadKey][]*ModbusBulkRead, keyOrd if int(endDataOffset) > len(readResults) { if k.FailOnError { - return nil, fmt.Errorf("Bounds check failure. startDataOffset: %v, endDataOffset: %v, readResultsLength: %v", + return nil, fmt.Errorf("bounds check failure. startDataOffset: %v, endDataOffset: %v, readResultsLength: %v", startDataOffset, endDataOffset, readResultsLength) } // nil reading. - log.Errorf("No data. Attempt to read beyond bounds. startDataOffset: %v, endDataOffset: %v, readResultsLength: %v", + log.Errorf("no data. Attempt to read beyond bounds. startDataOffset: %v, endDataOffset: %v, readResultsLength: %v", startDataOffset, endDataOffset, readResultsLength) // Make a reading with a nil Reading.Value. reading, err = theOutput.MakeReading(nil) @@ -446,7 +446,7 @@ func MapBulkReadData(bulkReadMap map[ModbusBulkReadKey][]*ModbusBulkRead, keyOrd return nil, err } } - log.Debugf("Appending reading: %#v, device: %v, output: %#v", reading, device, theOutput) + log.Debugf("appending reading: %#v, device: %v, output: %#v", reading, device, theOutput) readings = append(readings, reading) // Add to accounted for. @@ -456,9 +456,9 @@ func MapBulkReadData(bulkReadMap map[ModbusBulkReadKey][]*ModbusBulkRead, keyOrd if len(readings) > 0 { readContext := sdk.NewReadContext(device, readings) readContexts = append(readContexts, readContext) - log.Debugf("Appending readContext: %#v, device: %v", readContext, device) + log.Debugf("appending readContext: %#v, device: %v", readContext, device) } else { - log.Debugf("No readings to append. Not creating read context") + log.Debugf("no readings to append. Not creating read context") } } // End for each device. } // End for each read. @@ -558,7 +558,7 @@ func (brm *bulkReadManager) addModbusDevice(d *sdk.Device) (err error) { return } - return fmt.Errorf("Unknown device handler %s", d.Handler) + return fmt.Errorf("unknown device handler %s", d.Handler) } // bulkReadSetupMutex puts a critical section around bulkReadManager.setup() so @@ -638,7 +638,7 @@ func (brm *bulkReadManager) GetBulkReadMap(mapID string) ( return brm.inputBulkReadMap, brm.inputKeyOrder, nil } - err = fmt.Errorf("Unknown mapId %s", mapID) + err = fmt.Errorf("unknown mapId %s", mapID) return } diff --git a/pkg/devices/device_test.go b/pkg/devices/device_test.go index 634d33b..74c172d 100644 --- a/pkg/devices/device_test.go +++ b/pkg/devices/device_test.go @@ -120,12 +120,11 @@ func dumpReadContexts(t *testing.T, readContexts []*sdk.ReadContext) { // dumpReadings dumps out the given readings to the test log. // func dumpReadings(t *testing.T, readings []*output.Reading) { -func dumpReadings(t *testing.T, readings []*output.Reading) { - for i := 0; i < len(readings); i++ { - t.Logf("reading[%v]: %#v", i, readings[i]) - t.Logf("reading.Value: 0x%04x, type %T", readings[i].Value, readings[i].Value) - } -} +// for i := 0; i < len(readings); i++ { +// t.Logf("reading[%v]: %#v", i, readings[i]) +// t.Logf("reading.Value: 0x%04x, type %T", readings[i].Value, readings[i].Value) +// } +// } // populateBulkReadMap populates a bulk read map with raw modbus data. func populateBulkReadMap(t *testing.T, bulkReadMap map[ModbusBulkReadKey][]*ModbusBulkRead, keyOrder []ModbusBulkReadKey) { diff --git a/pkg/devices/holding_register.go b/pkg/devices/holding_register.go index d3aaa45..e7cf608 100644 --- a/pkg/devices/holding_register.go +++ b/pkg/devices/holding_register.go @@ -118,7 +118,7 @@ func writeHoldingRegister(device *sdk.Device, data *sdk.WriteData) (err error) { dataString := string(modbusData) register64, err := strconv.ParseUint(dataString, 16, 16) if err != nil { - return fmt.Errorf("Unable to parse uint16 %v", dataString) + return fmt.Errorf("unable to parse uint16 %v", dataString) } registerData := uint16(register64) diff --git a/pkg/devices/input_register.go b/pkg/devices/input_register.go index 914cfb1..7d7b687 100644 --- a/pkg/devices/input_register.go +++ b/pkg/devices/input_register.go @@ -33,9 +33,9 @@ func bulkReadInputRegisters(devices []*sdk.Device) (readContexts []*sdk.ReadCont var client modbus.Client var handler *modbus.TCPClientHandler var deviceData *config.ModbusDeviceData - client, handler, deviceData, err = GetBulkReadClient(k) - if err != nil { - return nil, err + client, handler, deviceData, error = GetBulkReadClient(k) + if error != nil { + return nil, error } // For read in v, perform each read. diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 73db1dd..623519b 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -74,7 +74,7 @@ func (b Bytes) Int64() (out int64, err error) { // Bool converts the byte slice to a bool. func (b Bytes) Bool() bool { - if b == nil || len(b) == 0 { + if len(b) == 0 { return false } return !(b[0] == 0)