Initial commit

Signed-off-by: jolheiser <john.olheiser@gmail.com>
main
jolheiser 2021-05-21 21:28:03 -05:00
parent fcbcf885e9
commit 89769cff6d
Signed by: jolheiser
GPG Key ID: B853ADA5DA7BBF7A
7 changed files with 147 additions and 1 deletions

18
.drone.yml 100644
View File

@ -0,0 +1,18 @@
---
kind: pipeline
name: compliance
trigger:
event:
- pull_request
steps:
- name: test
pull: always
image: golang:1.16
commands:
- make test
- name: check
pull: always
image: golang:1.16
commands:
- make vet
- diff <(gofmt -d .) <(echo -n)

View File

@ -19,3 +19,8 @@ func (e Epoch) Time() time.Time {
func Now() Epoch {
return Epoch(time.Now().Unix())
}
// Time returns an Epoch based on the given time.Time
func Time(t time.Time) Epoch {
return Epoch(t.Unix())
}

View File

@ -21,4 +21,11 @@ func TestEpochZero(t *testing.T) {
t.Log("epoch did not match zero-time")
t.FailNow()
}
var z time.Time
e := Time(z)
if !e.Time().Equal(Zero.Time()) {
t.Log("zero-time did not match zero-epoch")
t.FailNow()
}
}

36
file/copy.go 100644
View File

@ -0,0 +1,36 @@
package file
import (
"fmt"
"io"
"os"
)
// Copy copies a file from src to dest
func Copy(src, dest string) error {
fi, err := os.Lstat(src)
if err != nil {
return err
}
srcFi, err := os.Open(src)
if err != nil {
return fmt.Errorf("could not open src: %w", err)
}
defer srcFi.Close()
destFi, err := os.Create(dest)
if err != nil {
return fmt.Errorf("could not create dest: %w", err)
}
defer destFi.Close()
if err := os.Chmod(dest, fi.Mode()); err != nil {
return err
}
if _, err := io.Copy(destFi, srcFi); err != nil {
return fmt.Errorf("could not copy %s to %s: %w", src, dest, err)
}
return nil
}

57
file/copy_test.go 100644
View File

@ -0,0 +1,57 @@
package file
import (
"fmt"
"os"
"path/filepath"
"testing"
)
func TestCopy(t *testing.T) {
contents := "COPY TEST"
tmpDir, err := os.MkdirTemp(os.TempDir(), "go-common")
if err != nil {
t.Logf("could not create temp dir: %v", err)
t.FailNow()
}
defer func() {
if err := os.RemoveAll(tmpDir); err != nil {
t.Logf("could not remove temp dir at %s: %v", tmpDir, err)
}
}()
tmp1, err := setupCopy(tmpDir, contents)
if err != nil {
t.Log(err)
t.FailNow()
}
tmp2 := filepath.Join(tmpDir, "tmp2")
if err := Copy(tmp1, tmp2); err != nil {
t.Logf("could not copy file: %v", err)
t.FailNow()
}
b, err := os.ReadFile(tmp2)
if err != nil {
t.Logf("could not read tmp2: %v", err)
t.FailNow()
}
if string(b) != contents {
t.Logf("contents did not match")
t.FailNow()
}
}
func setupCopy(tmpDir, contents string) (string, error) {
tmp, err := os.CreateTemp(tmpDir, "go-common")
if err != nil {
return "", fmt.Errorf("could not create temp file 1: %v", err)
}
if _, err := tmp.WriteString(contents); err != nil {
return "", fmt.Errorf("could not write to temp file 1: %v", err)
}
return tmp.Name(), tmp.Close()
}

2
go.mod
View File

@ -1,3 +1,3 @@
module go.jolheiser.com/go-common
module git.jojodev.com/jolheiser/go-common
go 1.16

23
random/random.go 100644
View File

@ -0,0 +1,23 @@
package random
import "math/rand"
// Intn returns a random number between min and max.
// It panics if min <= max.
// Remember to seed rand first!
func Intn(min, max int) int {
return RandIntn(nil, min, max)
}
// RandIntn returns a random number between min and max for the given rand.Rand.
//
// It panics if min <= max.
func RandIntn(r *rand.Rand, min, max int) int {
if min < max {
panic("min cannot be less than or equal to max")
}
if r == nil {
return rand.Intn(max-min) + min
}
return r.Intn(max-min) + min
}