From 4b324934897bc1f30182085033a15dfca003b5a7 Mon Sep 17 00:00:00 2001 From: Geoffroy Vallee Date: Mon, 16 Dec 2019 12:38:56 -0500 Subject: [PATCH] Modifications to get an actual Go module Signed-off-by: Geoffroy Vallee --- .github/workflows/go.yml | 33 +++++++++++++++ go.mod | 3 ++ pkg/syserror/syserror.go | 40 +++++++++++++++++++ .../syserror/syserror_test.go | 0 syserror.go | 38 ------------------ 5 files changed, 76 insertions(+), 38 deletions(-) create mode 100644 .github/workflows/go.yml create mode 100644 go.mod create mode 100644 pkg/syserror/syserror.go rename syserror_test.go => pkg/syserror/syserror_test.go (100%) delete mode 100644 syserror.go diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 0000000..7907fb0 --- /dev/null +++ b/.github/workflows/go.yml @@ -0,0 +1,33 @@ +name: Go +on: [push, pull_request] +jobs: + + build: + name: Build + runs-on: ubuntu-latest + steps: + + - name: Set up Go 1.12 + uses: actions/setup-go@v1 + with: + go-version: 1.12 + id: go + + - name: Check out code into the Go module directory + uses: actions/checkout@v1 + + - name: Get dependencies + run: | + go get -v -t -d ./... + if [ -f Gopkg.toml ]; then + curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh + dep ensure + fi + + - name: Build and test + run: | + go build ./... + go test + env: + GOPATH: /tmp/go + diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..f747659 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/gvallee/syserror + +go 1.12 diff --git a/pkg/syserror/syserror.go b/pkg/syserror/syserror.go new file mode 100644 index 0000000..21327a1 --- /dev/null +++ b/pkg/syserror/syserror.go @@ -0,0 +1,40 @@ +// Copyright (c) 2019, Geoffroy Vallee, All rights reserved +// Copyright (c) 2019, Sylabs Inc. All rights reserved. +// This software is licensed under a 3-clause BSD license. Please consult the +// LICENSE.md file distributed with the sources of this project regarding your +// rights to use or distribute this software. + +package syserror + +import ( + "fmt" +) + +type SysError struct { + msg string // message associated to the error + code int // error code +} + +// Predefined errors +var NoErr = SysError{"Success", 0} // Success +var ErrFatal = SysError{"Fatal error", -1} // Generic fatal error +var ErrOutOfRes = SysError{"Out of resources", -2} // Out of resources (e.g., out of memory) +var ErrNotAvailable = SysError{"Not available", -3} // The target is not available (e.g., a file does not exist) +var ErrDataOverflow = SysError{"Data overflow", -4} // Data overflow +var ErrInvalidArg = SysError{"Invalid argument", -5} // Invalid function argument + +func (err *SysError) Error() string { + if err.code != 0 { + return fmt.Sprintf("ERROR: %d: %s", err.code, err.msg) + } else { + return fmt.Sprintf("%d: %s", err.code, err.msg) + } +} + +func (_error SysError) getMsg() string { + return _error.msg +} + +func (_error SysError) getCode() int { + return _error.code +} diff --git a/syserror_test.go b/pkg/syserror/syserror_test.go similarity index 100% rename from syserror_test.go rename to pkg/syserror/syserror_test.go diff --git a/syserror.go b/syserror.go deleted file mode 100644 index b1b4e55..0000000 --- a/syserror.go +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright(c) Geoffroy Vallee - * All rights reserved - */ - -package syserror - -import ("fmt") - -type SysError struct { - msg string // message associated to the error - code int // error code -} - -// Predefined errors -var NoErr = SysError{"Success", 0} // Success -var ErrFatal = SysError{"Fatal error", -1} // Generic fatal error -var ErrOutOfRes = SysError{"Out of resources", -2} // Out of resources (e.g., out of memory) -var ErrNotAvailable = SysError{"Not available", -3} // The target is not available (e.g., a file does not exist) -var ErrDataOverflow = SysError{"Data overflow", -4} // Data overflow -var ErrInvalidArg = SysError{"Invalid argument", -5} // Invalid function argument - -func (err *SysError) Error() string { - if err.code != 0 { - return fmt.Sprintf ("ERROR: %d: %s", err.code, err.msg) - } else { - return fmt.Sprintf ("%d: %s", err.code, err.msg) - } -} - -func (_error SysError) getMsg() string { - return _error.msg -} - -func (_error SysError) getCode() int { - return _error.code -} -