Skip to content

Commit

Permalink
refactor(snap): Refactor and upgrade to edgex-snap-hooks v3 (#217)
Browse files Browse the repository at this point in the history
* ci(snap): Upgrade to v3 snap testing actions

* refactor(snap): Upgrade to snap hooks v3
- Remove enabling of app options since it is enabled by default in the library
- Other code refactoring

Signed-off-by: Farshid Tavakolizadeh <farshid.tavakolizadeh@canonical.com>
  • Loading branch information
farshidtz authored Jan 17, 2023
1 parent 916f6b3 commit 6741b26
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 114 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/snap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
steps:
- name: Build and upload snap
id: build
uses: canonical/edgex-snap-testing/build@v2
uses: canonical/edgex-snap-testing/build@v3
outputs:
snap: ${{steps.build.outputs.snap}}

Expand All @@ -23,7 +23,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Download and test snap
uses: canonical/edgex-snap-testing/test@v2
uses: canonical/edgex-snap-testing/test@v3
with:
name: device-onvif-camera
snap: ${{needs.build.outputs.snap}}
Expand Down
26 changes: 6 additions & 20 deletions snap/local/helper-go/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,18 @@
package main

import (
"github.com/canonical/edgex-snap-hooks/v2/log"
"github.com/canonical/edgex-snap-hooks/v2/options"
"github.com/canonical/edgex-snap-hooks/v2/snapctl"
"github.com/canonical/edgex-snap-hooks/v3/log"
"github.com/canonical/edgex-snap-hooks/v3/options"
)

// configure is called by the main function
func configure() {
const service = "device-onvif-camera"

log.SetComponentName("configure")

log.Info("Enabling config options")
err := snapctl.Set("app-options", "true").Run()
if err != nil {
log.Fatalf("could not enable config options: %v", err)
}

log.Info("Processing config options")
err = options.ProcessConfig(service)
if err != nil {
log.Fatalf("could not process config options: %v", err)
if err := options.ProcessConfig(app); err != nil {
log.Fatalf("Error processing config options: %v", err)
}

log.Info("Processing autostart options")
err = options.ProcessAutostart(service)
if err != nil {
log.Fatalf("could not process autostart options: %v", err)
if err := options.ProcessAutostart(app); err != nil {
log.Fatalf("Error processing autostart options: %v", err)
}
}
4 changes: 2 additions & 2 deletions snap/local/helper-go/go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/edgexfoundry/device-onvif-camera/snap/local/helper-go

require github.com/canonical/edgex-snap-hooks/v2 v2.4.1

go 1.18

require github.com/canonical/edgex-snap-hooks/v3 v3.0.0-20230112170125-c0580fb68dab
16 changes: 3 additions & 13 deletions snap/local/helper-go/go.sum
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
github.com/canonical/edgex-snap-hooks/v2 v2.4.1 h1:TFFF/mHkYTmUd040N8S4q/mp78CUZr1W3Cxx4uRpfOg=
github.com/canonical/edgex-snap-hooks/v2 v2.4.1/go.mod h1:8mjUKSAFNsXYvV0fcfOoYue1dSjTVeJYdaQYtA6pb6Y=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/canonical/edgex-snap-hooks/v3 v3.0.0-20230112170125-c0580fb68dab h1:wpiKN0hX8tqeZNa4jPvgyrqP8ixm1Xu7lcQA3bypR7w=
github.com/canonical/edgex-snap-hooks/v3 v3.0.0-20230112170125-c0580fb68dab/go.mod h1:RvJ48YbdBPZn7L8OcylOpKIlIJD+nMjo5D7WSnPYusY=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
85 changes: 8 additions & 77 deletions snap/local/helper-go/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,91 +17,22 @@
package main

import (
"os"

hooks "github.com/canonical/edgex-snap-hooks/v2"
"github.com/canonical/edgex-snap-hooks/v2/env"
"github.com/canonical/edgex-snap-hooks/v2/log"
hooks "github.com/canonical/edgex-snap-hooks/v3"
"github.com/canonical/edgex-snap-hooks/v3/env"
"github.com/canonical/edgex-snap-hooks/v3/log"
)

// installConfig copies all config files from $SNAP to $SNAP_DATA
func installConfig() error {
resPath := "/config/device-onvif-camera/res"
err := os.MkdirAll(env.SnapData+resPath, 0755)
if err != nil {
return err
}

path := resPath + "/configuration.toml"
err = hooks.CopyFile(
env.Snap+path,
env.SnapData+path)
if err != nil {
return err
}
path := "/config/" + app + "/res"

return nil
return hooks.CopyDir(env.Snap+path, env.SnapData+path)
}

func installDevices() error {
path := "/config/device-onvif-camera/res/devices"

err := hooks.CopyDir(
hooks.Snap+path,
hooks.SnapData+path)
if err != nil {
return err
}

return nil
}

func installDevProfiles() error {
path := "/config/device-onvif-camera/res/profiles"

err := hooks.CopyDir(
hooks.Snap+path,
hooks.SnapData+path)
if err != nil {
return err
}

return nil
}

func installProvisionWatchers() error {
path := "/config/device-onvif-camera/res/provision_watchers"

err := hooks.CopyDir(
hooks.Snap+path,
hooks.SnapData+path)
if err != nil {
return err
}

return nil
}

// install is called by the main function
func install() {
log.SetComponentName("install")

err := installConfig()
if err != nil {
log.Fatalf("error installing config file: %s", err)
}

err = installDevices()
if err != nil {
log.Fatalf("error installing devices config: %s", err)
}

err = installDevProfiles()
if err != nil {
log.Fatalf("error installing device profiles config: %s", err)
}

err = installProvisionWatchers()
if err != nil {
log.Fatalf("error installing provision watchers: %s", err)
if err := installConfig(); err != nil {
log.Fatalf("Error installing config files: %s", err)
}
}
2 changes: 2 additions & 0 deletions snap/local/helper-go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"os"
)

const app = "device-onvif-camera"

func main() {
subCommand := os.Args[1]
switch subCommand {
Expand Down

0 comments on commit 6741b26

Please sign in to comment.