Browse Source

Added mage, fixed linting errors

master
Darshil Chanpura 5 months ago
parent
commit
cd791f3f2a
  1. 13
      awsutils/cloudwatch_test.go
  2. 4
      awsutils/email_test.go
  3. 1
      go.mod
  4. 2
      go.sum
  5. 78
      mage.go
  6. 1
      templates/embed.go
  7. 19
      tracker/models_test.go

13
awsutils/cloudwatch_test.go

@ -1,8 +1,6 @@
package awsutils
import (
"fmt"
"log"
"testing"
"time"
)
@ -12,15 +10,18 @@ func TestGetMetrics(t *testing.T) {
profile = "personal"
tm, err := time.Parse("2006-01-02", "2020-06-19")
if err != nil {
log.Println(err)
t.FailNow()
t.Errorf("error parsing time %v", err)
}
fmt.Println(GetMetrics(tm, time.Now(),
d, err := GetMetrics(tm, time.Now(),
[]Metric{
{
InstanceID: "i-123",
Stats: []string{"Average"},
},
},
time.Hour))
time.Hour)
t.Logf("metrics: %v", d)
if err != nil {
t.Errorf("error getting metrics %v", err)
}
}

4
awsutils/email_test.go

@ -1,7 +1,6 @@
package awsutils
import (
"log"
"testing"
)
@ -11,7 +10,6 @@ func TestSendEmail(t *testing.T) {
region = "ap-south-1"
err := SendEmail("Test Email", "dtchanpura@gmail.com", []string{"dtchanpura@gmail.com"}, []string{}, `<!doctype html><html><body><p>hello <b>world</b></p></body></html>`, "hello world")
if err != nil {
log.Println(err)
t.FailNow()
t.Errorf("error sending email %v", err)
}
}

1
go.mod

@ -4,5 +4,6 @@ go 1.16
require (
github.com/aws/aws-sdk-go v1.38.51
github.com/magefile/mage v1.11.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)

2
go.sum

@ -6,6 +6,8 @@ github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9Y
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/magefile/mage v1.11.0 h1:C/55Ywp9BpgVVclD3lRnSYCwXTYxmSppIgLeDYlNuls=
github.com/magefile/mage v1.11.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=

78
mage.go

@ -0,0 +1,78 @@
// +build mage
package main
import (
"fmt"
"os"
"strings"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
var (
binary = "track-instances"
oslist = []string{"windows", "linux", "darwin"}
archlist = []string{"amd64", "arm64", "arm"}
version = os.Getenv("VERSION")
)
func dependencies() {
sh.RunV("go", "get", "-u", "-v", "golang.org/x/lint/...", "honnef.co/go/tools/cmd/staticcheck", "github.com/fzipp/gocyclo/...")
}
func Test() error {
mg.Deps(dependencies)
// packages, _ := sh.Output("go", "list", "./...")
// fmt.Println(packages)
// strings.Split(packages, " ")
// sh.RunV("go", "test", "-v", "-race", "./...")
output, err := sh.Output("find", ".", "-iname", "*.go", "-type", "f")
if err != nil {
return err
}
gofiles := strings.Split(output, "\n")
err = sh.RunV("go", "vet", "./...")
if err != nil {
return err
}
err = sh.RunV("staticcheck", "./...")
if err != nil {
return err
}
args := []string{"-over", "19"}
err = sh.RunV("gocyclo", append(args, gofiles...)...)
if err != nil {
return err
}
err = sh.RunV("golint", "-set_exit_status", "./...")
if err != nil {
return err
}
return nil
}
func Build() error {
if version == "" {
version = "latest"
}
err := os.Mkdir("release", 0755)
if err != nil && !os.IsExist(err) {
return err
}
for _, ops := range oslist {
for _, arch := range archlist {
if err := sh.RunWithV(map[string]string{"GOOS": ops, "GOARCH": arch}, "go", "build", "-ldflags", "-s -w", "-o", "release/"+binary, "./cmd/track-instances"); err != nil {
continue
}
sh.RunV("tar", "-czf", fmt.Sprintf("release/%s-%s-%s-%s.tar.gz", binary, version, ops, arch), "README.md", "-C", "release", binary)
os.Remove("release/" + binary)
}
}
return nil
}
func Clean() error {
return os.RemoveAll("release")
}

1
templates/embed.go

@ -2,5 +2,6 @@ package templates
import "embed"
// Content containing the static files
//go:embed email.*
var Content embed.FS

19
tracker/models_test.go

@ -1,8 +1,6 @@
package tracker
import (
"fmt"
"log"
"regexp"
"testing"
"time"
@ -17,10 +15,9 @@ func TestParseTime(t *testing.T) {
b, err := yaml.Marshal(job)
if err != nil {
t.FailNow()
log.Println(err)
t.Errorf("yaml.Marshal() = %v", err)
}
log.Println(string(b[:]))
t.Log(string(b[:]))
yamlstr := `version: 1.0.0
jobs:
@ -30,19 +27,19 @@ jobs:
match: ^Job1??
max_duration: 12h
`
fmt.Println(yamlstr)
t.Log(yamlstr)
var c Config
err = yaml.Unmarshal([]byte(yamlstr), &c)
if err != nil {
log.Println(err)
t.FailNow()
t.Errorf("yaml.Unmarshal() = %v", err)
}
fmt.Println(c)
t.Log(c)
r, err := regexp.MatchString(c.Jobs[0].SearchConfig.Match, "Job1")
if err != nil {
log.Println(err)
t.Errorf("regexp.MatchString(%v, Job1) = %v", c.Jobs[0].SearchConfig.Match, err)
}
fmt.Println(r)
t.Log(r)
}

Loading…
Cancel
Save