Skip to content

Commit

Permalink
Improvement/add go doc (#20)
Browse files Browse the repository at this point in the history
* added docs

* added docs

* fixed tests
  • Loading branch information
InbarPerry committed Jul 23, 2020
1 parent f103bee commit 137361c
Show file tree
Hide file tree
Showing 18 changed files with 83 additions and 29 deletions.
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use hot-reload features which auto-build and run upon new change detected on the
}
)

// Execute executes the root cobra command
func Execute() error {
return rootCmd.Execute()
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"gopkg.in/yaml.v2"
)

// Config contains the fields of gebug configuration
type Config struct {
Name string `yaml:"name"`
OutputBinaryPath string `yaml:"output_binary"`
Expand All @@ -34,6 +35,7 @@ func updateBuildCommand(buildCommand string, debuggerEnabled bool) string {
return strings.TrimSpace(goBuildPrefix + ` -gcflags="all=-N -l"` + commandArgs)
}

// Load loads a configuration to a Config struct
func Load(input []byte) (*Config, error) {
c := &Config{}
err := yaml.Unmarshal(input, c)
Expand Down
21 changes: 16 additions & 5 deletions pkg/config/generate.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
package config

import (
"github.com/pkg/errors"
"go.uber.org/zap"
"io"
"os"
"path"

"github.com/pkg/errors"
"go.uber.org/zap"
)

const (
RootDir = ".gebug"
Path = "config.yaml"
DockerfileName = "Dockerfile"

// RootDir contains the name of the directory where gebug internals files and configurations are stored
RootDir = ".gebug"

// Path contains the name of gebug's configuration file
Path = "config.yaml"

// DockerfileName contains the name of the dockerfile that is used for building and running the project
DockerfileName = "Dockerfile"

// DockerComposeFileName contains the name of the docker-compose configuration that is used to configure the contianer
DockerComposeFileName = "docker-compose.yml"
)

// FilePath returns the path of a file inside gebug's directory
func FilePath(workDir string, fileName string) string {
return path.Join(workDir, RootDir, fileName)
}
Expand All @@ -41,6 +51,7 @@ func createConfigFile(fileName string, workDir string, renderFunc func(io.Writer
return nil
}

// Generate generates the required files to run the docker
func (c *Config) Generate(workDir string) error {
for fileName, renderFunc := range map[string]func(io.Writer) error{
DockerComposeFileName: c.RenderDockerComposeFile,
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func (c *Config) renderedWrite(template string, writer io.Writer) error {
return nil
}

// RenderDockerComposeFile writes the docker-compose.yml configuration to writer
func (c *Config) RenderDockerComposeFile(writer io.Writer) error {
return c.renderedWrite(`version: '3'
services:
Expand Down Expand Up @@ -66,6 +67,7 @@ networks:
`, writer)
}

// RenderDockerfile writes the Dockerfile to writer
func (c *Config) RenderDockerfile(writer io.Writer) error {
return c.renderedWrite(`FROM {{.RuntimeImage}}
RUN go get github.com/githubnemo/CompileDaemon
Expand Down
4 changes: 3 additions & 1 deletion pkg/input/app_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import (
"github.com/moshebe/gebug/pkg/config"
)

// PromptName handles the prompt that asks for application name
type PromptName struct {
*config.Config
}

// Run asks the user for application name
func (p *PromptName) Run() error {
prompt := &promptui.Prompt{
Label: "Application Name",
Validate: RegexValidator{`^[A-Za-z0-9]([A-Za-z0-9_-]*[A-Za-z0-9])?$`, &p.Name}.validate,
Validate: regexValidator{`^[A-Za-z0-9]([A-Za-z0-9_-]*[A-Za-z0-9])?$`, &p.Name}.validate,
Default: p.Name,
}
_, err := prompt.Run()
Expand Down
4 changes: 3 additions & 1 deletion pkg/input/build_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import (
"github.com/moshebe/gebug/pkg/config"
)

// PromptBuildCommand handles the prompt that asks for build command
type PromptBuildCommand struct {
*config.Config
}

// Run asks the user for build command
func (p *PromptBuildCommand) Run() error {
prompt := &promptui.Prompt{
Label: "Build Command",
Validate: NonEmptyValidator{field: &p.BuildCommand}.validate,
Validate: nonEmptyValidator{field: &p.BuildCommand}.validate,
Default: p.BuildCommand,
}
_, err := prompt.Run()
Expand Down
4 changes: 3 additions & 1 deletion pkg/input/debugger_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
"github.com/moshebe/gebug/pkg/config"
)

// PromptDebuggerOptions handles the prompt that asks for debugger options
type PromptDebuggerOptions struct {
*config.Config
}

// Run asks the user for debugger options
func (p *PromptDebuggerOptions) Run() error {
selectPrompt := promptui.Select{
Label: "Select debugging method",
Expand All @@ -25,7 +27,7 @@ func (p *PromptDebuggerOptions) Run() error {
if p.DebuggerEnabled {
prompt := &promptui.Prompt{
Label: "Debugger Port",
Validate: NumericRangeValidator{
Validate: numericRangeValidator{
min: 1024,
max: 65535,
field: &p.DebuggerPort,
Expand Down
2 changes: 2 additions & 0 deletions pkg/input/expose_ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"github.com/moshebe/gebug/pkg/config"
)

// PromptExposePort handles the prompt that asks for expose ports
type PromptExposePort struct {
*config.Config
}

// Run asks the user for expose ports
func (p *PromptExposePort) Run() error {
prompt := NewSelectWithAddAndRemove(&p.ExposePorts, &promptui.SelectWithAdd{
Label: "Define ports to expose. Press existing choices to delete (e.g: 8080[:8080])",
Expand Down
5 changes: 5 additions & 0 deletions pkg/input/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ import (
"go.uber.org/zap"
)

// ConfigPrompt asks for fields for the configuration
type ConfigPrompt interface {

// Run asks for configuration field and saves it in configuration
Run() error
}

// LoadOrDefault loads gebug's configuration file from the disk. Loads a default configuration in case of failure
func LoadOrDefault(workDir string) (*config.Config, bool) {
fallback := &config.Config{
OutputBinaryPath: "/app",
Expand Down Expand Up @@ -65,6 +69,7 @@ func save(workDir string, currentConfig *config.Config) error {
return nil
}

// Setup runs a list of prompts and saves the user's input as config
func Setup(currentConfig *config.Config, prompts []ConfigPrompt, workDir string) error {
for _, p := range prompts {
err := p.Run()
Expand Down
2 changes: 2 additions & 0 deletions pkg/input/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"github.com/moshebe/gebug/pkg/config"
)

// PromptNetworks handles the prompt that asks for networks
type PromptNetworks struct {
*config.Config
}

// Run asks the user for networks to use
func (p *PromptNetworks) Run() error {
prompt := NewSelectWithAddAndRemove(&p.Networks, &promptui.SelectWithAdd{
Label: "Add networks or leave empty to create a new network",
Expand Down
4 changes: 3 additions & 1 deletion pkg/input/output_binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import (
"github.com/moshebe/gebug/pkg/config"
)

// PromptOutputBinary handles the prompt that asks for output binary
type PromptOutputBinary struct {
*config.Config
}

// Run asks the user for the name of the output binary
func (p *PromptOutputBinary) Run() error {
prompt := &promptui.Prompt{
Label: "Output Binary Path (inside the container, referenced by: {{.output_binary}})",
Validate: NonEmptyValidator{field: &p.OutputBinaryPath}.validate,
Validate: nonEmptyValidator{field: &p.OutputBinaryPath}.validate,
Default: p.OutputBinaryPath,
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/input/run_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import (
"github.com/moshebe/gebug/pkg/config"
)

// PromptRunCommand handles the prompt that asks for run command
type PromptRunCommand struct {
*config.Config
}

// Run asks the user for the run command
func (p *PromptRunCommand) Run() error {
prompt := &promptui.Prompt{
Label: "Run Command",
Validate: NonEmptyValidator{field: &p.RunCommand}.validate,
Validate: nonEmptyValidator{field: &p.RunCommand}.validate,
Default: p.RunCommand,
}
_, err := prompt.Run()
Expand Down
4 changes: 3 additions & 1 deletion pkg/input/runtime_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import (
"github.com/moshebe/gebug/pkg/config"
)

// PromptRuntimeImage handles the prompt that asks for runtime image
type PromptRuntimeImage struct {
*config.Config
}

// Run asks the user for the runtime image
func (p *PromptRuntimeImage) Run() error {
prompt := &promptui.Prompt{
Label: "Runtime Docker Image",
Validate: NonEmptyValidator{field: &p.RuntimeImage}.validate,
Validate: nonEmptyValidator{field: &p.RuntimeImage}.validate,
Default: p.RuntimeImage,
}
_, err := prompt.Run()
Expand Down
6 changes: 5 additions & 1 deletion pkg/input/select_add_remove.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package input

import (
"github.com/manifoldco/promptui"
"strings"

"github.com/manifoldco/promptui"
)

const (
defaultDoneItem = `✔ Done`
defaultRemovePrefix = `✖️ `
)

// SelectWithAddAndRemove provides a ui prompt for a list that supports both adding to and removing from a list
type SelectWithAddAndRemove struct {
prompt *promptui.SelectWithAdd
field *[]string
doneItem string
removePrefix string
}

// NewSelectWithAddAndRemove constructs SelectWithAddAndRemove
func NewSelectWithAddAndRemove(field *[]string, prompt *promptui.SelectWithAdd) *SelectWithAddAndRemove {
return &SelectWithAddAndRemove{
prompt: prompt,
Expand Down Expand Up @@ -56,6 +59,7 @@ func (s *SelectWithAddAndRemove) cleanResults(items []string) []string {
return items
}

// Run runs the prompt
func (s *SelectWithAddAndRemove) Run() error {
items := append(*s.field, s.doneItem)
s.appendRemovePrefix(items)
Expand Down
17 changes: 9 additions & 8 deletions pkg/input/validation.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package input

import (
"github.com/pkg/errors"
"regexp"
"strconv"
"strings"

"github.com/pkg/errors"
)

type Validator interface {
type validatorIface interface {
validate(string) error
}

type NonEmptyValidator struct {
type nonEmptyValidator struct {
field *string
}

func (v NonEmptyValidator) validate(input string) error {
func (v nonEmptyValidator) validate(input string) error {
input = strings.TrimSpace(input)
if len(input) <= 0 {
return errors.New("empty command")
Expand All @@ -24,12 +25,12 @@ func (v NonEmptyValidator) validate(input string) error {
return nil
}

type RegexValidator struct {
type regexValidator struct {
pattern string
field *string
}

func (v RegexValidator) validate(input string) error {
func (v regexValidator) validate(input string) error {
input = strings.TrimSpace(input)
if len(input) <= 0 {
return errors.New("empty input")
Expand All @@ -42,13 +43,13 @@ func (v RegexValidator) validate(input string) error {
return nil
}

type NumericRangeValidator struct {
type numericRangeValidator struct {
min int
max int
field *int
}

func (v NumericRangeValidator) validate(input string) error {
func (v numericRangeValidator) validate(input string) error {
input = strings.TrimSpace(input)
if len(input) <= 0 {
return errors.New("empty input")
Expand Down
11 changes: 6 additions & 5 deletions pkg/input/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package input

import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

type testScenario struct {
input string
wantErr bool
}

func testValidator(t *testing.T, validator Validator, tests []testScenario) {
func testValidator(t *testing.T, validator validatorIface, tests []testScenario) {
for i, test := range tests {
t.Run(fmt.Sprintf("%s#%d", t.Name(), i), func(t *testing.T) {
err := validator.validate(test.input)
Expand All @@ -26,7 +27,7 @@ func testValidator(t *testing.T, validator Validator, tests []testScenario) {

func TestNonEmptyValidator(t *testing.T) {
var dummy string
testValidator(t, NonEmptyValidator{field: &dummy}, []testScenario{
testValidator(t, nonEmptyValidator{field: &dummy}, []testScenario{
{input: "", wantErr: true},
{input: " ", wantErr: true},
{input: " \t", wantErr: true},
Expand All @@ -39,7 +40,7 @@ func TestNonEmptyValidator(t *testing.T) {

func TestRegexValidator(t *testing.T) {
var dummy string
testValidator(t, RegexValidator{pattern: `^[A-Za-z0-9]([A-Za-z0-9_-]*[A-Za-z0-9])?$`, field: &dummy}, []testScenario{
testValidator(t, regexValidator{pattern: `^[A-Za-z0-9]([A-Za-z0-9_-]*[A-Za-z0-9])?$`, field: &dummy}, []testScenario{
{input: "", wantErr: true},
{input: " ", wantErr: true},
{input: " \t", wantErr: true},
Expand All @@ -55,7 +56,7 @@ func TestRegexValidator(t *testing.T) {

func TestNumericRangeValidator(t *testing.T) {
var dummy int
testValidator(t, NumericRangeValidator{min: 1, max: 10, field: &dummy}, []testScenario{
testValidator(t, numericRangeValidator{min: 1, max: 10, field: &dummy}, []testScenario{
{input: "", wantErr: true},
{input: " ", wantErr: true},
{input: " \t", wantErr: true},
Expand Down
Loading

0 comments on commit 137361c

Please sign in to comment.