From 33a8a0fb1d1881fd1b73fa23662390bbef39c173 Mon Sep 17 00:00:00 2001 From: Farshid Tavakolizadeh Date: Wed, 18 Jan 2023 17:30:03 +0100 Subject: [PATCH] refactor(snap)!: Drop the support for legacy snap env options (#502) * ci(snap): Add snap's Go module to dependabot * ci(snap): Upgrade to v3 snap testing actions * refactor(snap)!: Remove support for legacy env options BREAKING CHANGE: Drop the support for deprecated snap options starting with `env.` Signed-off-by: Farshid Tavakolizadeh --- .github/dependabot.yml | 4 +++ .github/workflows/snap.yaml | 4 +-- snap/local/helper-go/configure.go | 58 ++++++++++--------------------- snap/local/helper-go/go.mod | 4 +-- snap/local/helper-go/go.sum | 16 ++------- snap/local/helper-go/install.go | 21 ++++------- snap/local/helper-go/main.go | 2 ++ 7 files changed, 38 insertions(+), 71 deletions(-) diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 60a5b8bf..adb10ed7 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -6,3 +6,7 @@ updates: directory: "/" schedule: interval: "daily" + - package-ecosystem: "gomod" + directory: "/snap/local/helper-go" + schedule: + interval: "daily" \ No newline at end of file diff --git a/.github/workflows/snap.yaml b/.github/workflows/snap.yaml index 16131946..72cbde6c 100644 --- a/.github/workflows/snap.yaml +++ b/.github/workflows/snap.yaml @@ -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}} @@ -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: app-service-configurable snap: ${{needs.build.outputs.snap}} \ No newline at end of file diff --git a/snap/local/helper-go/configure.go b/snap/local/helper-go/configure.go index 7b964716..e380c96e 100644 --- a/snap/local/helper-go/configure.go +++ b/snap/local/helper-go/configure.go @@ -18,26 +18,32 @@ package main import ( "fmt" - hooks "github.com/canonical/edgex-snap-hooks/v2" - "github.com/canonical/edgex-snap-hooks/v2/log" - "github.com/canonical/edgex-snap-hooks/v2/options" "os" + + "github.com/canonical/edgex-snap-hooks/v3/env" + "github.com/canonical/edgex-snap-hooks/v3/log" + "github.com/canonical/edgex-snap-hooks/v3/options" + "github.com/canonical/edgex-snap-hooks/v3/snapctl" ) // validateProfile processes the snap 'profile' configure option, ensuring that the directory // and associated configuration.toml file in $SNAP_DATA both exist. -// -func validateProfile(prof string) error { +func validateProfile() error { + prof, err := snapctl.Get("profile").Run() + if err != nil { + return fmt.Errorf("error getting snap option: %v", err) + } + log.Debugf("validateProfile: profile is %s", prof) if prof == "" { return nil } - path := fmt.Sprintf("%s/config/res/%s/configuration.toml", hooks.SnapData, prof) + path := fmt.Sprintf("%s/config/res/%s/configuration.toml", env.SnapData, prof) log.Debugf("validateProfile: checking if %s exists", path) - _, err := os.Stat(path) + _, err = os.Stat(path) if err != nil { return fmt.Errorf("profile %s has no configuration.toml", prof) } @@ -46,44 +52,18 @@ func validateProfile(prof string) error { } func configure() { - - const service = "app-service-configurable" - log.SetComponentName("configure") - log.Info("Processing profile") - prof, err := hooks.NewSnapCtl().Config(hooks.ProfileConfig) - if err != nil { - log.Fatalf("Error reading config 'profile': %v", err) - } - - err = validateProfile(prof) - if err != nil { + log.Info("Validating profile configuration") + if err := validateProfile(); err != nil { log.Fatalf("Error validating profile: %v", err) } - log.Info("Processing legacy env options") - envJSON, err := hooks.NewSnapCtl().Config(hooks.EnvConfig) - if err != nil { - log.Fatalf("Reading config 'env' failed: %v", err) - } - if envJSON != "" { - log.Debugf("envJSON: %s", envJSON) - err = hooks.HandleEdgeXConfig(service, envJSON, nil) - if err != nil { - log.Fatalf("HandleEdgeXConfig failed: %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) } } diff --git a/snap/local/helper-go/go.mod b/snap/local/helper-go/go.mod index 97594af7..da7f540e 100644 --- a/snap/local/helper-go/go.mod +++ b/snap/local/helper-go/go.mod @@ -1,5 +1,5 @@ module github.com/edgexfoundry/app-service-configurable/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 diff --git a/snap/local/helper-go/go.sum b/snap/local/helper-go/go.sum index dcfb3359..81198834 100644 --- a/snap/local/helper-go/go.sum +++ b/snap/local/helper-go/go.sum @@ -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= diff --git a/snap/local/helper-go/install.go b/snap/local/helper-go/install.go index 3ce7258b..50429db9 100644 --- a/snap/local/helper-go/install.go +++ b/snap/local/helper-go/install.go @@ -17,31 +17,22 @@ package main import ( - 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 { path := "/config/res" - err := hooks.CopyDir( - env.Snap+path, - env.SnapData+path) - if err != nil { - return err - } - - return nil + return hooks.CopyDir(env.Snap+path, env.SnapData+path) } -// 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) + if err := installConfig(); err != nil { + log.Fatalf("Error installing config files: %s", err) } } diff --git a/snap/local/helper-go/main.go b/snap/local/helper-go/main.go index 86c43088..1fe94e3c 100644 --- a/snap/local/helper-go/main.go +++ b/snap/local/helper-go/main.go @@ -19,6 +19,8 @@ import ( "os" ) +const app = "app-service-configurable" + func main() { subCommand := os.Args[1] switch subCommand {