hooks

package module
v3.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 13, 2023 License: Apache-2.0 Imports: 5 Imported by: 10

README

edgex-snap-hooks

Go Reference

Snap hooks library used by EdgeX Foundry Go service snaps.
It provides utilites to implement snap hooks, including some wrappers for the snapctl commands.

Usage

Download or upgrade to the latest version:

go get github.com/canonical/edgex-snap-hooks/v3

Please refer to go get docs for details.

The jakarta branch is split from master as of v2.1.3. It would only receive patch updates but no minor or major releases. The jakarta-only tags follow v2.1.X+jakarta scheme, where X is greater than 3.

Example
package main

import (
	"fmt"
	"os"

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

func main() {
	var err error

	if err = hooks.Init(false, "edgex-device-example"); err != nil {
		fmt.Printf("initialization failure: %s", err)
		os.Exit(1)
	}

	// copy file from $SNAP to $SNAP_DATA
	if err = hooks.CopyFile(hooks.Snap+"/config.json", hooks.SnapData+"config.json"); err != nil {
		hooks.Error(err.Error())
		os.Exit(1)
	}
  
	// read env var override configuration
	cli := hooks.NewSnapCtl()
	envJSON, err := cli.Config(hooks.EnvConfig)
	if err != nil {
		hooks.Error(fmt.Sprintf("Reading config 'env' failed: %v", err))
		os.Exit(1)
	}
	hooks.Debug(fmt.Sprintf("envJSON: %s", envJSON))
}

Testing

The tests need to run in a snap environment:

Build and install:

snapcraft
sudo snap install --dangerous ./edgex-snap-hooks_test_amd64.snap

The tests files are read relative to project source inside the snap. The edgex-snap-hooks.test command runs go test -v --cover internally and accepts all other go test arguments.

Run all tests:

make test

Run top-level tests:

sudo edgex-snap-hooks.test

Run tests in one package, e.g. snapctl:

sudo edgex-snap-hooks.test ./snapctl

Run one unit test, e.g. TestGet:

sudo edgex-snap-hooks.test ./snapctl -run TestGet
Development
make try

You can now edit the files locally, copy them to prime directory, and re-run the tests without rebuilding the project. E.g.:

make sync
sudo edgex-snap-hooks.test ./snapctl

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyDir

func CopyDir(srcPath string, dstPath string) error

CopyDir copies a whole directory recursively snippet from https://blog.depa.do/post/copy-files-and-directories-in-go

func CopyFile

func CopyFile(srcPath, destPath string) error

CopyFile copies a file within the snap

func CopyFileReplace

func CopyFileReplace(srcPath, destPath string, rStrings map[string]string) error

CopyFileReplace copies a file within the snap and replaces strings using the string/replace values in the rStrings parameter.

Types

This section is empty.

Directories

Path Synopsis
* Copyright (C) 2021 Canonical Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License.
* Copyright (C) 2021 Canonical Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL