Skip to content

Commit

Permalink
Added flag list
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleimp committed Jul 29, 2020
1 parent 01806bb commit fc580ba
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/apex/log v1.6.0
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/fatih/color v1.9.0 // indirect
github.com/gookit/color v1.2.7
github.com/mattn/go-colorable v0.1.7 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/stretchr/testify v1.6.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gookit/color v1.2.7 h1:4qePMNWZhrmbfYJDix+J4V2l0iVW+6jQGjicELlN14E=
github.com/gookit/color v1.2.7/go.mod h1:AhIE+pS6D4Ql0SQWbBeXPHw7gY0/sjHoA4s/n1KB7xg=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jpillora/backoff v0.0.0-20180909062703-3050d21c67d7/go.mod h1:2iMrUgbbvHEiQClaW2NsSzMyGHqN+rDFqY705q49KG0=
Expand Down
23 changes: 23 additions & 0 deletions internal/check/check.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package check

import (
"fmt"

"github.com/apex/log"
"github.com/gookit/color"
"github.com/wesleimp/unleash-checkr/internal/flag"
"github.com/wesleimp/unleash-checkr/pkg/context"
)

Expand All @@ -14,5 +18,24 @@ func Start(ctx *context.Context) error {
"slack-token": ctx.Config.SlackToken,
}).Info("Start check")

ff, err := flag.Get(ctx)
if err != nil {
return err
}

log.Info("Flags list")
fmt.Println()

for _, f := range ff {
name := color.New(color.Bold).Sprintf("Name:")
descripion := color.New(color.Bold).Sprint("Description:")
createdAt := color.New(color.Bold).Sprint("Created at:")

log.Info(fmt.Sprintf("%s %s", name, f.Name))
log.Info(fmt.Sprintf("%s %s", descripion, f.Description))
log.Info(fmt.Sprintf("%s %v", createdAt, f.CreatedAt))
fmt.Println()
}

return nil
}
57 changes: 57 additions & 0 deletions internal/flag/flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package flag

import (
"encoding/json"
"fmt"
"net/http"
"time"

"github.com/apex/log"
"github.com/wesleimp/unleash-checkr/pkg/context"
)

var (
all = "all"
active = "actives"
inactive = "inactive"
)

// Get all flags
func Get(ctx *context.Context) ([]Flag, error) {
log.Info("Getting flags")

url := fmt.Sprintf("%s/api/client/features", ctx.Config.URL)
res, err := http.Get(url)
if err != nil {
return nil, err
}
defer res.Body.Close()

var flags Flags
err = json.NewDecoder(res.Body).Decode(&flags)
if err != nil {
return nil, err
}

ff := filter(ctx, flags.Features)

return ff, nil
}

func filter(ctx *context.Context, flags []Flag) []Flag {
const day = 24 * time.Hour
dueDate := time.Now().Add(-time.Duration(ctx.Config.Expires) * day)
dueDateUnix := dueDate.Unix()

log.WithField("due-date", dueDate).
Info("Filtering flags")

var ff []Flag
for _, f := range flags {
if f.CreatedAt.Unix() < dueDateUnix {
ff = append(ff, f)
}
}

return ff
}
16 changes: 16 additions & 0 deletions internal/flag/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package flag

import "time"

// Flags struct
type Flags struct {
Features []Flag `json:"features"`
}

// Flag struct
type Flag struct {
Name string `json:"name"`
Description string `json:"description"`
CreatedAt time.Time `json:"createdAt"`
IsEnabled bool `json:"enabled"`
}

0 comments on commit fc580ba

Please sign in to comment.