Skip to content

Commit

Permalink
Remove internal cli context and add CliConf in cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonBaeumer committed Jul 8, 2019
1 parent 8c9651b commit 70b9ec0
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 102 deletions.
38 changes: 22 additions & 16 deletions add.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,42 @@ package goss

import (
"fmt"
"github.com/SimonBaeumer/goss/internal/app"
"github.com/SimonBaeumer/goss/resource"
"github.com/SimonBaeumer/goss/system"
"github.com/SimonBaeumer/goss/resource"
"github.com/SimonBaeumer/goss/system"
"github.com/SimonBaeumer/goss/util"
"io"
"os"
"reflect"
"strconv"
"reflect"
"strconv"
"strings"
)

type Add struct {
Writer io.Writer
Ctx app.CliContext
Sys *system.System
Writer io.Writer
ExcludeAttr []string
Timeout int
AllowInsecure bool
NoFollowRedirects bool
Server string
Username string
Password string
Header string
Sys *system.System
}

// AddResources is a sSimple wrapper to add multiple resources
func (a *Add) AddResources(fileName, resourceName string, keys []string) error {
OutStoreFormat = getStoreFormatFromFileName(fileName)
header := extractHeaderArgument(a.Ctx.Header)
header := extractHeaderArgument(a.Header)

config := util.Config{
IgnoreList: a.Ctx.ExcludeAttr,
Timeout: a.Ctx.Timeout,
AllowInsecure: a.Ctx.AllowInsecure,
NoFollowRedirects: a.Ctx.NoFollowRedirects,
Server: a.Ctx.Server,
Username: a.Ctx.Username,
Password: a.Ctx.Password,
IgnoreList: a.ExcludeAttr,
Timeout: a.Timeout,
AllowInsecure: a.AllowInsecure,
NoFollowRedirects: a.NoFollowRedirects,
Server: a.Server,
Username: a.Username,
Password: a.Password,
Header: header,
}

Expand Down
5 changes: 0 additions & 5 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package goss

import (
"fmt"
"github.com/SimonBaeumer/goss/internal/app"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -18,10 +17,6 @@ type GossRunTime struct {
Package string //this does not belong here imho
}

func NewGossRunTime(ctx app.CliContext) *GossRunTime {
return &GossRunTime{}
}

func (g *GossRunTime) Serve() {
//Serve()
}
Expand Down
116 changes: 94 additions & 22 deletions cmd/goss/goss.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
app2 "github.com/SimonBaeumer/goss/internal/app"
"github.com/SimonBaeumer/goss/system"
"github.com/fatih/color"
"github.com/patrickmn/go-cache"
Expand All @@ -14,7 +13,6 @@ import (
"github.com/SimonBaeumer/goss"
"github.com/SimonBaeumer/goss/outputs"
"github.com/urfave/cli"
//"time"
)

var version string
Expand All @@ -28,6 +26,58 @@ func main() {
}
}

// CliConfig represents all configurations passed by the cli app
type CliConfig struct {
FormatOptions []string
Sleep time.Duration
RetryTimeout time.Duration
MaxConcurrent int
Vars string
Gossfile string
ExcludeAttr []string
Timeout int
AllowInsecure bool
NoFollowRedirects bool
Server string
Username string
Password string
Header string
Endpoint string
ListenAddr string
Cache time.Duration
Format string
NoColor bool
Color bool
}

// NewCliConfig creates an object from the cli.Context. It is used as a constructor and will do simple type conversions
// and validations
func NewCliConfig(c *cli.Context) CliConfig {
return CliConfig{
FormatOptions: c.StringSlice("format-options"),
Sleep: c.Duration("sleep"),
RetryTimeout: c.Duration("retry-timeout"),
MaxConcurrent: c.Int("max-concurrent"),
Vars: c.GlobalString("vars"),
Gossfile: c.GlobalString("gossfile"),
ExcludeAttr: c.GlobalStringSlice("exclude-attr"),
Timeout: int(c.Duration("timeout") / time.Millisecond),
AllowInsecure: c.Bool("insecure"),
NoFollowRedirects: c.Bool("no-follow-redirects"),
Server: c.String("server"),
Username: c.String("username"),
Password: c.String("password"),
Header: c.String("header"),
Endpoint: c.String("endpoint"),
ListenAddr: c.String("listen-addr"),
Cache: c.Duration("cache"),
Format: c.String("format"),
NoColor: c.Bool("no-color"),
Color: c.Bool("color"),
}
}

// Create the cli.App object
func createApp() *cli.App {
app := cli.NewApp()
app.EnableBashCompletion = true
Expand Down Expand Up @@ -71,10 +121,19 @@ func createApp() *cli.App {
Aliases: []string{"aa"},
Usage: "automatically add all matching resource to the test suite",
Action: func(c *cli.Context) error {
conf := NewCliConfig(c)

a := goss.Add{
Ctx: app2.NewCliContext(c),
Writer: app.Writer,
Sys: system.New(),
Username: conf.Username,
Password: conf.Password,
Server: conf.Server,
Timeout: conf.Timeout,
NoFollowRedirects: conf.NoFollowRedirects,
AllowInsecure: conf.AllowInsecure,
ExcludeAttr: conf.ExcludeAttr,
Header: conf.Header,
}
return a.AutoAddResources(c.GlobalString("gossfile"), c.Args())
},
Expand Down Expand Up @@ -126,37 +185,50 @@ func createServeCommand() cli.Command {
},
},
Action: func(c *cli.Context) error {
ctx := app2.NewCliContext(c)
conf := NewCliConfig(c)

gossRunTime := getGossRunTime(ctx)
gossRunTime := goss.GossRunTime{
Gossfile: c.GlobalString("gossfile"),
Vars: c.GlobalString("vars"),
}

h := &goss.HealthHandler{
Cache: cache.New(ctx.Cache, 30*time.Second),
Outputer: outputs.GetOutputer(ctx.Format),
Cache: cache.New(conf.Cache, 30 * time.Second),
Outputer: outputs.GetOutputer(conf.Format),
Sys: system.New(),
GossMu: &sync.Mutex{},
MaxConcurrent: ctx.MaxConcurrent,
MaxConcurrent: conf.MaxConcurrent,
GossConfig: gossRunTime.GetGossConfig(),
FormatOptions: conf.FormatOptions,
}

if ctx.Format == "json" {
if conf.Format == "json" {
h.ContentType = "application/json"
}

h.Serve(ctx.Endpoint)
h.Serve(conf.Endpoint)
return nil
},
}
}

func createAddHandler(app *cli.App, resourceName string) (func(c *cli.Context) error) {
return func(c *cli.Context) error {
conf := NewCliConfig(c)

a := goss.Add{
Sys: system.New(),
Writer: app.Writer,
Username: conf.Username,
Password: conf.Password,
Server: conf.Server,
Timeout: conf.Timeout,
NoFollowRedirects: conf.NoFollowRedirects,
AllowInsecure: conf.AllowInsecure,
ExcludeAttr: conf.ExcludeAttr,
Header: conf.Header,
}

a.Ctx = app2.NewCliContext(c)
a.AddResources(c.GlobalString("gossfile"), resourceName, c.Args())
return nil
}
Expand Down Expand Up @@ -348,27 +420,27 @@ func createValidateCommand(app *cli.App) cli.Command {
},
},
Action: func(c *cli.Context) error {
ctx := app2.NewCliContext(c)
conf := NewCliConfig(c)

runtime := getGossRunTime(ctx)
runtime := getGossRunTime(conf.Gossfile, conf.Vars)

v := &goss.Validator{
OutputWriter: app.Writer,
MaxConcurrent: ctx.MaxConcurrent,
Outputer: outputs.GetOutputer(ctx.Format),
FormatOptions: ctx.FormatOptions,
MaxConcurrent: conf.MaxConcurrent,
Outputer: outputs.GetOutputer(conf.Format),
FormatOptions: conf.FormatOptions,
GossConfig: runtime.GetGossConfig(),
}

//TODO: ugly shit to set the color here, tmp fix for the moment!
if ctx.NoColor {
if conf.NoColor {
color.NoColor = true
}
if ctx.Color {
if conf.Color {
color.NoColor = false
}

if ctx.Gossfile == "testing" {
if conf.Gossfile == "testing" {
v.Validate(startTime)
return nil
}
Expand All @@ -379,10 +451,10 @@ func createValidateCommand(app *cli.App) cli.Command {
}
}

func getGossRunTime(ctx app2.CliContext) goss.GossRunTime {
func getGossRunTime(gossfile string, vars string) goss.GossRunTime {
runtime := goss.GossRunTime{
Gossfile: ctx.Gossfile,
Vars: ctx.Vars,
Gossfile: gossfile,
Vars: vars,
}
return runtime
}
2 changes: 1 addition & 1 deletion integration/TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Fail and Success case for every resource.

- [x] addr
- [x] command
- [ ] dns
- [x] dns
- [x] file
- [x] gossfile
- [x] group
Expand Down
54 changes: 0 additions & 54 deletions internal/app/context.go

This file was deleted.

7 changes: 3 additions & 4 deletions serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package goss

import (
"bytes"
"github.com/SimonBaeumer/goss/internal/app"
"log"
"net/http"
"sync"
Expand All @@ -15,10 +14,9 @@ import (
"github.com/patrickmn/go-cache"
)

//TODO: Maybe seperating handler and server?
//TODO: Maybe separating handler and server?
type HealthHandler struct {
RunTimeConfig GossRunTime
C app.CliContext
GossConfig GossConfig
Sys *system.System
Outputer outputs.Outputer
Expand All @@ -27,6 +25,7 @@ type HealthHandler struct {
ContentType string
MaxConcurrent int
ListenAddr string
FormatOptions []string
}

func (h *HealthHandler) Serve(endpoint string) {
Expand All @@ -46,7 +45,7 @@ type res struct {
//health check request.
func (h HealthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
outputConfig := util.OutputConfig{
FormatOptions: h.C.FormatOptions,
FormatOptions: h.FormatOptions,
}

log.Printf("%v: requesting health probe", r.RemoteAddr)
Expand Down

0 comments on commit 70b9ec0

Please sign in to comment.