Compare commits

...

5 Commits
exit ... main

5 changed files with 92 additions and 40 deletions

13
.gitlab-ci.yml Normal file
View File

@ -0,0 +1,13 @@
# You can override the included template(s) by including variable overrides
# SAST customization: https://docs.gitlab.com/ee/user/application_security/sast/#customizing-the-sast-settings
# Secret Detection customization: https://docs.gitlab.com/ee/user/application_security/secret_detection/#customizing-settings
# Dependency Scanning customization: https://docs.gitlab.com/ee/user/application_security/dependency_scanning/#customizing-the-dependency-scanning-settings
# Container Scanning customization: https://docs.gitlab.com/ee/user/application_security/container_scanning/#customizing-the-container-scanning-settings
# Note that environment variables can be set in several places
# See https://docs.gitlab.com/ee/ci/variables/#cicd-variable-precedence
stages:
- test
sast:
stage: test
include:
- template: Security/SAST.gitlab-ci.yml

View File

@ -1,7 +1,7 @@
[![GoDoc](http://godoc.org/gitlab.com/CRThaze/sugar?status.svg)](http://godoc.org/gitlab.com/CRThaze/sugar)
# sugar
[![GoDoc](http://godoc.org/gitlab.com/CRThaze/sugar?status.svg)](http://godoc.org/gitlab.com/CRThaze/sugar)
## Syntactic Sugar for Golang
This package provides some convenience functions that help keep code clean.

51
errors.go Normal file
View File

@ -0,0 +1,51 @@
package sugar
import (
"fmt"
"os"
)
var (
ErrorWriter = os.Stderr
ErrorPrefix = "Error:"
ErrorFmt = "%s %+v"
StandardExitCode = 1
PanicFunc = func(err error) {
panic(err)
}
ErrorPrintFunc = func(err error) {
fmt.Fprintf(ErrorWriter, ErrorFmt, ErrorPrefix, err)
}
ExitFunc = func(code int) {
os.Exit(code)
}
)
// Check takes an error and prints it if it is not nil; but will continue.
func Check(err error) {
if err != nil {
ErrorPrintFunc(err)
}
}
// CheckPanic takes an error and will panic if it is not nil.
func CheckPanic(err error) {
if err != nil {
PanicFunc(err)
}
}
// CheckExit takes an error and will print it and exit if it is not nil.
// If no exit code is provided, the StandardExitCode will be used.
// Any exit codes beyond the first one one provided will be discarded.
func CheckExit(err error, code ...int) {
if err != nil {
ErrorPrintFunc(err)
exitCode := StandardExitCode
if len(code) > 0 {
exitCode = code[0]
}
ExitFunc(exitCode)
}
}

View File

@ -1,11 +1,6 @@
// Package sugar provides convenience functions for keeping code clean and less repetitive.
package sugar
import (
"fmt"
"os"
)
/*
* Literal Addressers
*/
@ -104,36 +99,3 @@ func StrPtr(v string) *string {
func BoolPtr(v bool) *bool {
return &v
}
/*
* Error Handling
*/
// Check takes an error and prints it if it is not nil; but will continue.
func Check(err error) {
if err != nil {
fmt.Printf("%+v", err)
}
}
// CheckLog takes an error and a format function for leveraging a custom printer like a logger.
func CheckLog(err error, fn func(format string, a ...interface{})) {
if err != nil {
fn("%+v", err)
}
}
// CheckPanic takes an error and will panic if it is not nil.
func CheckPanic(err error) {
if err != nil {
panic(err)
}
}
// CheckExit takes an error and will print it and exit with status 1 if it is not nil.
func CheckExit(err error) {
if err != nil {
fmt.Printf("%+v", err)
os.Exit(1)
}
}

26
sets.go Normal file
View File

@ -0,0 +1,26 @@
package sugar
// StrSet a set type for strings.
type StrSet map[string]struct{}
// Add adds a new key to the set if it does not already exist.
func (s *StrSet) Add(str string) {
(*s)[str] = struct{}{}
}
// Has checks if the given string is present in the set.
func (s *StrSet) Has(str string) bool {
if _, ok := (*s)[str]; ok {
return true
}
return false
}
// AddHas behaves like Add, but returns true if the key already existed.
func (s *StrSet) AddHas(str string) bool {
if ok := s.Has(str); ok {
return ok
}
s.Add(str)
return false
}