gotoolbox

package module
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2024 License: MIT Imports: 9 Imported by: 1

README

gotoolbox

A kitchen sink of Go tools that I've found useful. Uses only the standard library, no external dependencies.

contents
example usage
go get github.com/jritsema/gotoolbox
utilities
package main

import "github.com/jritsema/gotoolbox"

func main() {

	s := []string{"a", "b", "c"}
	if gotoolbox.SliceContainsLike(&s, "b") {
		fmt.Println("b exists")
	}

	err := gotoolbox.Retry(3, 1, func() error {
		return callBrittleAPI()
	})
	if err != nil {
		fmt.Println("callBrittleAPI failed after 3 retries: %w", err)
	}

	f := "config.json"
	if !gotoolbox.IsDirectory(f) && gotoolbox.FileExists(f) {
		config, err := gotoolbox.ReadJSONFile(f)
		if err != nil {
			fmt.Println("error reading json file: %w", err)
		}
	}

	value := gotoolbox.GetEnvWithDefault("MY_ENVVAR", "true")

	command := exec.Command("docker", "build", "-t", "foo", ".")
	err = gotoolbox.ExecCmd(command, true)
	if err != nil {
		fmt.Println("error executing command: %w", err)
	}

	var data interface{}
	err = gotoolbox.HttpGetJSON("https://api.example.com/data.json", &data)

	err = gotoolbox.HttpPutJSON("https://api.example.com/data.json", data)

	var res Response
	err = gotoolbox.HttpPostJSON("https://api.example.com/data.json", data, &res, http.StatusCreated)
}
web package
package main

import (
	"embed"
	"html/template"
	"net/http"
	"github.com/jritsema/gotoolbox/web"
)

var (
	//go:embed all:templates/*
	templateFS embed.FS
	html *template.Template
)

type Data struct {
	Hello string `json:"hello"`
}

func index(r *http.Request) *web.Response {
	return web.HTML(http.StatusOK, html, "index.html", Data{Hello: "world"}, nil)
}

func api(r *http.Request) *web.Response {
	return web.DataJSON(http.StatusOK, Data{Hello: "world"}, nil)
}

func main() {
	html, _ = web.TemplateParseFSRecursive(templateFS, ".html", true, nil)
	mux := http.NewServeMux()
	mux.Handle("/api", web.Action(api))
	mux.Handle("/", web.Action(index))
	http.ListenAndServe(":8080", mux)
}
development

Choose a make command to run

vet vet code
test run unit tests
build build a binary
autobuild auto build when source files change
start build and run local project

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExecCmd

func ExecCmd(cmd *exec.Cmd, output bool) error

ExecCmd executes a command and waits

func FileExists

func FileExists(filename string) bool

FileExists returns true if a file exists

func GetEnvWithDefault added in v0.2.0

func GetEnvWithDefault(key, defaultValue string) string

GetEnvWithDefault returns the value of an enviornment variable or a default value if the environment variables is not set

func HttpGetJSON added in v0.6.0

func HttpGetJSON(url string, result interface{}) error

HttpGetJSON fetches the contents of the given URL and decodes it as JSON into the given result, which should be a pointer to the expected data. returns an error if http response code is not 200

func HttpPostJSON added in v0.7.0

func HttpPostJSON(url string, request, response interface{}, httpStatusCodeToCheck int) error

HttpPostJSON encodes a struct as JSON and HTTP POSTs it to the specified url. If the HTTP response's content-type is JSON, then the response is decoded into the given result. Returns an error if http response code does not match specified `httpStatusCodeToCheck`. Set `httpStatusCodeToCheck` == 0 to opt out of the check.

func HttpPutJSON added in v0.7.0

func HttpPutJSON(url string, o interface{}) error

HttpPutJSON encodes a struct as JSON and HTTP PUTs it to the specified url returns an error if http response code is not 200

func IsDirectory

func IsDirectory(path string) (bool, error)

IsDirectory returns true if a path is a directory

func ReadJSONFile

func ReadJSONFile(fileName string) (map[string]string, error)

ReadJSONFile reads JSON from a file

func Retry

func Retry(attempts int, sleep time.Duration, fn func() error) error

Retry retries a function with exponential backoff return a Stop error to abort

func SliceContains

func SliceContains(s *[]string, e string) bool

SliceContains returns true if a slice contains a string

func SliceContainsInt added in v0.4.0

func SliceContainsInt(i *[]int, e int) bool

SliceContainsInt returns true if a slice contains an int

func SliceContainsLike

func SliceContainsLike(s *[]string, e string) bool

SliceContainsLike returns true if a slice contains a string that contains another string

Types

type Stop

type Stop struct {
	// contains filtered or unexported fields
}

Stop represents a stop error

Directories

Path Synopsis
Package web is a super minimalistic web/http server library
Package web is a super minimalistic web/http server library

Jump to

Keyboard shortcuts

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